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 $directoryPathIn 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:

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 $parentDirectoryThis 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 $fileNameThis will output File.txt, which is the leaf element of the path. You can see the output in the screenshot below:

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 $directoryPathThis 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 $directoryPathThe [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:
- How to Get the Size of a File in PowerShell?
- How to Check if a File Contains a String in PowerShell?
- How to Read CSV File Line by Line in PowerShell?
- How to Read the First Line of a File in PowerShell?
- How to Encrypt a File with a Password in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.