Often you may want combine a path and a file name in PowerShell.
The easiest way to do so is by using the Join-Path cmdlet, which was built for this specific task.
You can use the following basic syntax to join a specific path with a specific file name:
Join-Path $my_path $my_filename
This particular example will combine the path specified by the my_path variable with the file name specified by the my_filename variable.
Also note that we can use the New-Item cmdlet along with the Join-Path cmdlet to create a new file by combining a file path and file name:
New-Item -ItemType File (Join-Path $my_path $my_filename)
The following example shows how to use this syntax in practice.
Note: You can use the Join-Path cmdlet to join as many variables together as you’d like.
Example: How to Combine Path and File Name in PowerShell
Suppose that we would like to combine the following two variables in PowerShell in which one variable contains a path and one contains a file name:
- $my_path = “c:/users/bobbi/data2”
- $my_filename = “new_data.txt”
We can use the following syntax to do so:
$my_path = "c:/users/bobbi/data2" $my_filename = "new_data.txt" Join-Path $my_path $my_filename
The following screenshot shows how to use this syntax in practice:

We can see that the Join-Path cmdlet has successfully joined the path variable and the file name variable into the following string:
- c:\users\bobbi\data2\new_data.txt
We can also use the New-Item cmdlet along with the Join-Path cmdlet to create a new file by combining a file path and file name:
$my_path = "c:/users/bobbi/data2" $my_filename = "new_data.txt" New-Item -ItemType File (Join-Path $my_path $my_filename)
The following screenshot shows how to use this syntax in practice:

We can see that a new file named new_data.txt has been created in the directory located at c:\users\bobbi\data2, as shown in the output.
Note: You can find the complete documentation for the Join-Path cmdlet here.
Related Tutorials
The following tutorials explain how to perform other common tasks in PowerShell:
PowerShell: How to Combine Two Arrays
PowerShell: How to Get File Name from Path
PowerShell: How to Get Last Folder in Path
