PowerShell: How to Use Join-Path with More Than Two Strings


You can use the Join-Path cmdlet in PowerShell to combine a path and a child path into a single path.

If you would like to combine more than two strings into a single path then you can pipe several Join-Path statements together:

Join-Path -Path "C:" -ChildPath "users" | Join-Path -ChildPath "bobbi"

Note that you can use this syntax to pipe together as many Join-Path statements as you would like.

The following example shows how to use this syntax in practice.

Example: How to Use Join-Path with More Than Two Strings in PowerShell

Suppose that we would like to join the strings “C:” and “users” and “bobbi” together to produce the following file path:

  • C:\users\bobbi

Suppose that we attempt to use a single Join-Path cmdlet with three strings:

Join-Path "C:" "users" "bobbi"

The following screenshot shows how to use this syntax in practice:

Notice that we receive an error because the Join-Path cmdlet only expects two strings: one for the path and another for the child path.

To avoid this error, we can instead pipe several Join-Path statements together:

Join-Path -Path "C:" -ChildPath "users" | Join-Path -ChildPath "bobbi"

The following screenshot shows how to use this syntax in practice:

PowerShell Join-Path with multiple strings

Notice that this produces the file path that we wanted:

  • C:\users\bobbi

Note that in this example we only piped together two Join-Path statements but you can use similar syntax to pipe together as many Join-Path statements as you would like.

Note: You can find the complete documentation for the Join-Path cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Test-Path with Wildcard Characters
PowerShell: How to Use Test-Path to Check Multiple Paths
PowerShell: How to Check if Folder Exists

Leave a Reply