Join paths in PowerShell without confusing joining and existence checks

  • Published: 2026.10.16
  • Updated: 2026.10.16
  • Windows
Join paths in PowerShell without confusing joining and existence checks

When a folder and a file name come from different variables, joining them with string addition makes separator handling easy to overlook. PowerShell’s Join-Path expresses the operation directly and lets the current provider supply the separator.

These examples use the Windows file system and work in Windows PowerShell 5.1 and PowerShell 7. A combined path is a name; it does not by itself create the file or its parent folder.


Combine a folder and file name

$base = 'reports'
$file = Join-Path -Path $base -ChildPath 'daily.txt'
$file
reports\daily.txt

-Path supplies the parent path and -ChildPath supplies the next component. Store the result in a variable when a later read or write needs the same location. Relative paths are interpreted from the current working location when used.

Handle separators at the join

Join-Path -Path 'reports\' -ChildPath '\daily.txt'

This also produces reports\daily.txt. The separator between the two components is handled without leaving a doubled separator at that boundary. The syntax also makes the purpose clearer than ad hoc string concatenation.

Join more than two components

$year = Join-Path -Path 'reports' -ChildPath '2026'
Join-Path -Path $year -ChildPath 'daily.txt'
reports\2026\daily.txt

Joining in stages keeps this example compatible with both 5.1 and 7. -AdditionalChildPath was introduced in PowerShell 6, and accepting an array for -ChildPath is a PowerShell 7.6 addition. Avoid relying on those newer forms in a script that must also run in 5.1.

Check existence separately

Run this in a fresh empty working folder to see that an ordinary join does not require the resulting file to exist.

Join-Path -Path 'reports' -ChildPath 'daily.txt'
Test-Path -LiteralPath 'reports'
reports\daily.txt
False

The False result assumes that reports does not already exist there. If it does, the existence result changes. Use Test-Path when existence matters; use a separate file-system operation when the folder or file actually needs to be created.

Resolve an existing path

try {
    Join-Path -Path 'reports' -ChildPath 'daily.txt' -Resolve -ErrorAction Stop
} catch {
    'Path could not be resolved'
}

With the sample path absent, the example prints Path could not be resolved. -Resolve asks the provider to resolve the joined path, which is different from assembling a name for a future file. Leave it off when naming a destination that will be created later.

An absolute path that names a drive also requires that drive to be available. Keep provider paths separate from web addresses: file-system path joining is not a general URL construction method. Choosing between joining a name, checking existence and resolving an existing location makes the next operation easier to reason about.

References

このブラウザからの自分のアクセスを解析から除外できます。