Skip to main content

How to Join Folder and File Names into a Path with Join-Path in PowerShell

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Join-Path puts folder and file names together into one path, with the right separator between them, so you never type a slash yourself. This guide joins two names, then three, builds a path from your home folder, and shows what joining with + gets wrong, with real output from PowerShell 7.6.10.

The Join-Path page: "The Join-Path cmdlet combines a path and child-path into a single path. The provider supplies the path delimiters."

Syntax​

Syntax
Join-Path <Path> <ChildPath> [<AdditionalChildPath> ...]
Join-Path -Path <Path> -ChildPath <ChildPath>
PartWhat it means
<Path>The first part of the path, often a folder, such as notes or $HOME
<ChildPath>The part to add after it, such as a file name
[<AdditionalChildPath> ...]Optional: more parts, each added after the one before
-Path, -ChildPathThe same values with their parameter names written out

Join-Path only builds the text of a path. The Join-Path page: "This cmdlet returns a string that contains the resulting path."

The Example​

The example builds paths to a to-do list: a todo.txt file in a notes folder. The folder and the file don't have to exist for this. The commands run in the PowerShell 7.6.10 console on a Mac, in a folder named join-path-demo.

Prerequisites​

  • PowerShell 7. This guide uses PowerShell 7.6.10: $PSVersionTable.PSVersion.ToString() printed 7.6.10.

Step 1: Join a Folder Name and a File Name​

PowerShell
Join-Path 'notes' 'todo.txt'
Join-Path -Path 'notes' -ChildPath 'todo.txt'

macOS-style code window titled PowerShell with two commands: Join-Path 'notes' 'todo.txt', and the same command with the parameter names -Path and -ChildPath written out.

Output
notes/todo.txt
notes/todo.txt

Both lines printed the same path: Join-Path put a / between the folder and the file. The first line gives the values by position, the folder first; the second names each one, which reads more clearly in a script.

Step 2: Join More Than Two Names​

PowerShell
Join-Path 'notes' '2026' 'todo.txt'
Output
notes/2026/todo.txt

Every name after the second went to the AdditionalChildPath parameter. The Join-Path page: "The AdditionalChildPath parameter allows the joining of an unlimited number of paths."

Step 3: Start the Path at Your Home Folder​

$HOME holds your home folder. The about_Automatic_Variables page: "Contains the full path of the user's home directory." So Join-Path can build a path inside it:

PowerShell
Join-Path $HOME 'notes.txt'
Output
/Users/you/notes.txt

macOS-style terminal window titled PowerShell where Join-Path 'notes' 'todo.txt' and the same command with -Path and -ChildPath both print notes/todo.txt, Join-Path 'notes' '2026' 'todo.txt' prints notes/2026/todo.txt, and Join-Path $HOME 'notes.txt' prints /Users/you/notes.txt.

Your own user name appears where this shows you. Because the result is a string, you can save it in a variable and use it in later commands:

PowerShell
$file = Join-Path $HOME 'notes.txt'
$file
Output
/Users/you/notes.txt

Step 4: Let Join-Path Handle Extra Slashes​

Names that already end or start with a separator don't get a double one:

PowerShell
Join-Path 'notes/' '/todo.txt'
Output
notes/todo.txt

The Join-Path page says the same of its own example: "Existing directory separators \ are handled so there is only one separator between Path and ChildPath."

Common Mistakes​

MistakeWhat happensDo this instead
Joining with +: 'notes' + 'todo.txt'notestodo.txt: + joins the text with nothing betweenUse Join-Path, which adds the separator
A name with a space and no quotes: Join-Path notes my todo.txtnotes/my/todo.txt: my and todo.txt became two separate partsQuote the name: Join-Path notes 'my todo.txt' printed notes/my todo.txt
Taking the printed path as proof that the file exists: Join-Path 'notes' 'missing.txt'It printed notes/missing.txt, though there's no such fileAdd -Resolve when the path must exist, as the FAQ shows

FAQ​

Does Join-Path check that the file exists?​

Not by default. Add -Resolve and it looks the path up: Join-Path 'notes' 'missing.txt' -Resolve failed with Join-Path: Cannot find path '/Users/you/join-path-demo/notes/missing.txt' because it does not exist.

Which separator does Join-Path use?​

The one the provider gives it. The Join-Path page: "The provider supplies the path delimiters." On this Mac's file system, that was /. The page's own examples show \, as in path\childpath.

Conclusion​

Join-Path 'notes' 'todo.txt' printed notes/todo.txt: Join-Path chose the separator, so you don't type one. Add more names for deeper paths, quote names that have spaces, and add -Resolve when the path must exist.

Additional Resources​

  • Join-Path — the parameters, including AdditionalChildPath and Resolve, with examples.
  • about_Automatic_Variables — $HOME and the other variables PowerShell sets for you.