Extract the Filename From a Path Using PowerShell

Rohan Timalsina Dec 21, 2022 Jun 13, 2022
  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
Extract the Filename From a Path Using 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
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell File