How to Extract the Directory from a File Path in PowerShell

To extract the directory from a file path in PowerShell, pass the path to Split-Path. Its default -Parent switch drops the file name and returns the folder that holds it:

Split-Path -Path 'C:\Shared\Finance\Invoice-1042.pdf' -Parent    # C:\Shared\Finance

Below I’ll show the folder name on its own, parent folders, Get-Item, the .NET method, relative and network paths, the running script’s folder, and where Windows PowerShell 5.1 answers differently.

Set up a sample folder to test file paths

Most examples run against real files, so let’s create a few. Open PowerShell 7 (pwsh) or Windows PowerShell, paste this script and press Enter:

$root = 'C:\psfaqs\t\extractdir'
$null = New-Item "$root\Finance\2026\Q4", "$root\Scripts" -ItemType Directory -Force
'Invoice 1042'      | Set-Content "$root\Finance\2026\Q4\Invoice-1042.pdf"
'Vendor,City,State' | Set-Content "$root\Finance\2026\Q4\Vendor-List.csv"
'Item,Amount'       | Set-Content "$root\Finance\2026\Budget-2026.xlsx"

Get-ChildItem $root -Recurse -File | Sort-Object FullName |
    ForEach-Object { $_.FullName.Substring($root.Length + 1) }

Output:

Finance\2026\Budget-2026.xlsx
Finance\2026\Q4\Invoice-1042.pdf
Finance\2026\Q4\Vendor-List.csv

You now have a Finance folder with a 2026 subfolder and a Q4 folder inside it. Change $root to any folder you like; the examples below use the same layout.

This post works backward from a full path you already have. If you still need that path, start with how to get the full path of a file first.

Get the directory of a file with Split-Path -Parent

Split-Path cuts a path string into parts. With -Parent, or with no switch at all, it returns everything before the last backslash:

$file   = 'C:\psfaqs\t\extractdir\Finance\2026\Q4\Invoice-1042.pdf'
$folder = Split-Path -Path $file -Parent

"Folder:  $folder"
"Default: $(Split-Path $file)"
"Type:    $($folder.GetType().Name)"

Output:

Folder:  C:\psfaqs\t\extractdir\Finance\2026\Q4
Default: C:\psfaqs\t\extractdir\Finance\2026\Q4
Type:    String
PowerShell Split-Path -Parent returning the folder of a file path
Split-Path returns the folder of Invoice-1042.pdf, with or without -Parent (PowerShell 7)

Both lines print the same folder because -Parent is the default. The result is a plain String, not a folder object, so you can join it, compare it or pass it to any cmdlet.

Split-Path also takes paths from the pipeline, and it doesn’t care whether they exist. Here are three at once, including a bare file name:

$paths = 'C:\Shared\Finance\Invoice-1042.pdf',
         'D:\Data\Denver\Sales-2026.csv',
         'Report.docx'
$paths | Split-Path | ForEach-Object { "[$_]" }

Output:

[C:\Shared\Finance]
[D:\Data\Denver]
[]

The bare file name returned an empty string, because it has no folder part. Need the other half of the path? Here’s how to get the file name from a path with -Leaf.