Join Path to Combine More Than Two Strings Into a File Path in PowerShell

Rohan Timalsina Jan 31, 2022
Join Path to Combine More Than Two Strings Into a File Path in PowerShell

The Join-Path cmdlet allows the user to combine strings into a single path. Sometimes, you might need to combine paths to create a single path when working in PowerShell.

It is where the Join-Path cmdlet comes into action. Many child paths can be combined or appended to the main path for creating a single path.

The -Path parameter specifies the main path to which the child-path is appended. -Path value determines which provider joins the paths and adds the path delimiters.

It provides the \ delimiter to join the paths. The -ChildPath parameter specifies the paths to append to the -Path parameter value.

For example, the following command uses Join-Path to combine the main path hello and a child-path world.

Join-Path -Path "hello" -ChildPath "world"

Output:

hello\world

The Join-Path cmdlet accepts only two string inputs or one -ChildPath parameter. Using the single command, you cannot use Join-Path to combine more than two strings into a file path.

You will need to use multiple Join-Path statements together to combine more than two strings into a file path in PowerShell.

Use the Join-Path Value to Combine More Than Two Strings Into a File Path in PowerShell

Since the Join-Path path value can send down to the pipe, you can pipe multiple Join-Path statements together to combine more than two strings into a file path.

For example, the following code combines all four strings and creates a single path C:\content\software\PowerShell.

Join-Path -Path "C:" -ChildPath "content" | Join-Path -ChildPath "software" | Join-Path -ChildPath "PowerShell"

Output:

C:\content\software\PowerShell
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell String

Related Article - PowerShell Path