How to Extract Directory from File Path in PowerShell? [3 Methods]

Sometimes, you might need to extract the directory from the file path. In this PowerShell tutorial, I will explain how to extract a directory from a file path in PowerShell using various methods.

To get the directory from a file path in PowerShell, you can use the Split-Path cmdlet with the -Parent parameter. For example, $directoryPath = Split-Path -Path “C:\Users\ExampleUser\Documents\File.txt” -Parent will store the directory path C:\Users\ExampleUser\Documents in the $directoryPath variable. This method is efficient and widely used for path manipulation in PowerShell scripts.

1. Using the Split-Path Cmdlet

To get the directory from a file path in PowerShell, use the Split-Path cmdlet. This cmdlet is designed to split the path into different components, allowing you to extract the desired part, such as the directory.

Here is a simple example:

$filePath = "C:\Users\ExampleUser\Documents\File.txt"
$directoryPath = Split-Path -Path $filePath
Write-Output $directoryPath

In this script, the Split-Path cmdlet takes the $filePath variable as input and returns only the directory part of the path, which is then stored in the $directoryPath variable.

I executed the PowerShell script using VS code. You can see the output in the screenshot below:

Extract Directory from File Path in PowerShell

Retrieve the Parent Directory

If you want to retrieve the parent directory of the file path, you can use the -Parent parameter with the Split-Path cmdlet.

$filePath = "C:\Users\ExampleUser\Documents\File.txt"
$parentDirectory = Split-Path -Path $filePath -Parent
Write-Output $parentDirectory

This will output the path C:\Users\ExampleUser\Documents, which is the parent directory of the specified file.

Extract the Leaf Element

If you want to extract the file name or the last folder in the path, you can use the -Leaf parameter.

$filePath = "C:\Users\ExampleUser\Documents\File.txt"
$fileName = Split-Path -Path $filePath -Leaf
Write-Output $fileName

This will output File.txt, which is the leaf element of the path. You can see the output in the screenshot below:

Extract the Leaf Element powershell

2. Using the Get-Location Cmdlet

Here is another method to get the current directory in PowerShell is by using the Get-Location cmdlet. This is equivalent to the print working directory (pwd) command found in other shells.

$directoryPath = Get-Location
Write-Output $directoryPath

This will output the path of the current directory from which the script is being executed.

3. Using .NET Methods

For those who prefer using .NET methods, PowerShell can interact with the .NET framework, allowing you to use its methods to manipulate paths.

Here is a complete PowerShell script.

$filePath = "C:\Users\ExampleUser\Documents\File.txt"
$directoryPath = [System.IO.Path]::GetDirectoryName($filePath)
Write-Output $directoryPath

The [System.IO.Path]::GetDirectoryName method is a .NET method that takes a path as input and returns the directory part of the path.

Conclusion

I hope after going through this tutorial, you have learned how to extract directory from file Path in PowerShell using 3 different methods.

You may also like: