Powershell

How do I Use Join-Path to Combine More than Two Strings into a File Path?

The cmdlet “Join-Path” is used to join or combine multiple strings in one meaningful file path or directory in PowerShell. Sometimes, we may need to append multiple child paths to create a one-parent path. That is where the “Join-Path” cmdlet’s role comes into play. It can combine two or more two strings into a single file path.

This article will overview a complete procedure to resolve the mentioned query.

How to Use “Join-Path” Command to Combine/Join More than two Strings into a File Path?

The two strings or more than two strings can be combined to create a single file path using the “Join-Path” cmdlet. The “Join-Path” cmdlet uses the “-Path” parameter to specify the root path to which other paths will be appended and the “-ChildPath” parameter to append the paths to the “-Path” parameter value. Moreover, it uses the “Delimiter \” to combine the paths/directory in the output.

Consider the below-mentioned examples to learn more about the usage of the “Join-Path” command.

Example 1: Create a File Path Using the Same Drive

This example will demonstrate the usage of the “Join-Path” command to combine/join two or more strings into a single file path/directory:

> Join-Path -Path "C:" -ChildPath "Doc" | Join-Path -ChildPath "New" | Join-Path -ChildPath "Files"

 
According to the above command:

    • First of all, add the “Join-Path” cmdlet, used the “-Path” parameter to add the root file path and then added the “-ChildPath” parameter to append the file path with the root directory.
    • The “Pipeline |” operator is used to combine these commands and send the output of one command as the input to the next command:

 

As it can be observed that two or more strings have been combined using the “Join-Path” command to create a file path.

Example 2: Create Multiple File Paths Using Different Drives

Here is another example to create a file path by combining two or more file paths using the “Join-Path” cmdlet:

> Join-Path -Path C:, D:, E: -ChildPath Doc | Join-Path -ChildPath New

 
In the above command:

    • First of all, add the “Join-Path” cmdlet with the “-Path” parameter and assigned multiple root paths separated by commas.
    • After that, use “-ChildPath” to add another path value and then use the “Pipeline |” to combine them:

 

It can be observed that the multiple file paths of different drives have been created using the “Join-Path” cmdlet.

Conclusion

The “Join-Path” cmdlet is used to combine various paths to make a complete directory address. This command uses the “-Path” parameter to specify the root path, and “-ChildPath” is used to append the multiple paths with the root path. This tutorial has illustrated a complete method to use the “Join-Path” cmdlet.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.