Powershell

How to use PowerShell Join-Path

PowerShell Join-Path is a useful cmdlet that assists in joining multiple paths. The Join-Path cmdlet combines multiple paths to generate a path that is recognizable by PowerShell. Among these multiple paths, one path is considered as a parent path, and the others are referred to as child paths. The functionality of a Join-Path cmdlet refers to a parent-child relationship as one parent can contain multiple children. This article demonstrates the working and functionality of the PowerShell Join-Path cmdlet.

How PowerShell Join-Path cmdlet works

The syntax of the PowerShell Join-Path is given below:

> Join-Path -Path <parent-path> -ChildPath <child-path>

The -Path and -ChildPath parameters refer to the main path and sub-paths that you want to join. Moreover, both parameters accept only string data types. Apart from these two mandatory parameters, the Join-Path cmdlet offers the following additional parameters:

AdditionalChildPath: This parameter allows you to add additional paths other than a single ChildPath. Moreover, the functionality of this parameter depends on the existence of the ChildPath parameter. As it gets the existence of the ChildPath parameter the paths added in AdditionalChildPath are appended. Like -Path and -ChildPath parameters, this option accepts the string values.

Resolve: This parameter indicates that the current provider must resolve the joined path. If the wildcards are used, then the cmdlet returns all the paths, and if no path matches (the given path) then it would return an error.

How to use PowerShell Join-Path

This section describes a few examples that are practiced using Join-Path in different scenarios.

Example 1: This example contains a list of commands that uses Join-Path to generate a PowerShell’s readable path. The commands provided in this example are using linux as a parent path and linuxhint as a child path.

> Join-Path -Path linux -ChildPath linuxhint

Now, the same output can be obtained by utilizing the command written below.

> Join-Path -Path linux\ -ChildPath \linuxhint

Or the command written here can serve the same function.

> Join-Path linux linuxhint

Example 2: This example demonstrates the command that will join multiple parent paths to a single child path. For example, the command provided below associates multiple parent paths to a single child. The Downloads will be appended to two paths, E:\MS an F:

> Join-Path -Path E:\MS, F: -ChildPath Downloads

Example 3: The Join-Path can be used to link an unlimited number of paths. For this, the -AdditionalChildPath parameter of the Join-Path cmdlet is used. The command provided below joins multiple child paths to a parent path(E:)

> Join-path -Path E: -ChildPath MS -AdditionalChildPath new, test, viva1, viva2, result

Example 4: The Join-Path cmdlet can act as a Get-ChildItem as well, but the difference is that the Join-Path cmdlet shows the complete path. For example, the command written below will present all files/directories paths that have the eclipse directory as a child.

> Join-Path "E:\*" "eclipse*" -Resolve

Conclusion

The Join-Path cmdlet in PowerShell allows you to join multiple paths as a parent-child relationship. One path acts as a parent and other child paths are then appended to a parent path. We have provided a brief guide that demonstrates the working and usage of the Join-Path cmdlet of PowerShell. The extensive parameter support of Join-Path allows you to join an undefined number of paths. Apart from joining paths, the Join-Path command can be practiced by getting the complete path details of any directory’s content.

About the author

Adnan Shabbir