Extract the Filename From a Path Using PowerShell

  1. Use the Split-Path Cmdlet to Extract the Filename From a Path in PowerShell
  2. Use the GetFileName Method to Extract the Filename From a Path in PowerShell
  3. Use the Get-Item Cmdlet to Extract the Filename From a Path in PowerShell

A file path tells the location of the file on the system. While working with files in PowerShell, you may need to get only the file name from a path.

There are multiple ways to get the path of the files in PowerShell. This tutorial will teach you to extract the filename from a file path with PowerShell.

Use the Split-Path Cmdlet to Extract the Filename From a Path in PowerShell

The Split-Path cmdlet displays the specified part of a given path in PowerShell. The part of a path can be the parent folder, subfolder, file name, or a file extension only.

To extract the filename with extension, use the Split-Path command with the -Leaf parameter.

Split-Path C:\pc\test_folder\hello.txt -Leaf

Output:

hello.txt

To get the filename without an extension, you can use the -LeafBase parameter. This parameter is available in PowerShell 6.0 or later versions only.

Split-Path C:\pc\test_folder\hello.txt -LeafBase

Output:

hello

Use the GetFileName Method to Extract the Filename From a Path in PowerShell

The GetFileName method of the .NET’s Path class returns the file name and extension of the specified path.

The following example displays the file name and extension from the path C:\pc\test_folder\hello.txt.

[System.IO.Path]::GetFileName('C:\pc\test_folder\hello.txt')

Output:

hello.txt

You can use the GetFileNameWithoutExtension method to extract only the filename without an extension.

[System.IO.Path]::GetFileNameWithoutExtension('C:\pc\test_folder\hello.txt')

Output:

hello

Use the Get-Item Cmdlet to Extract the Filename From a Path in PowerShell

The Get-Item cmdlet extracts the item at the specified location. If the item is present at the specified path, it returns the file’s Directory, Mode, LastWriteTime, Length, and Name.

Get-Item C:\pc\test_folder\hello.txt

Output:

Directory: C:\pc\test_folder


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        09-06-2022     21:43             18 hello.txt

You can add .Name to the end of the above command to return only the file name with an extension.

(Get-Item C:\pc\test_folder\hello.txt).Name

Output:

hello.txt

To get only the file name without an extension, specify the .BaseName property.

(Get-Item C:\pc\test_folder\hello.txt).BaseName

Output:

hello

This method also applies to the Get-ChildItem cmdlet.

(Get-ChildItem C:\pc\test_folder\hello.txt).Name
(Get-ChildItem C:\pc\test_folder\hello.txt).BaseName

Output:

hello.txt
hello
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - PowerShell File

  • Query for File's Last Write Time in PowerShell
  • Concatenate Files Using PowerShell
  • Get File Size in KB Using PowerShell