How to Get File Name from Path in PowerShell?

When working with file system operations in PowerShell, a common task that arises is extracting the file name from a given path. This can be particularly useful in scripting, where you need to manipulate files based on their names or extensions. PowerShell offers several ways to accomplish this, and in this blog post, we’ll explore some of the most efficient methods to get file name from a full file path in PowerShell.

To get the file name from a path in PowerShell, you can use the Split-Path cmdlet with the -Leaf parameter, which extracts the last element of the path, i.e., the file name. Alternatively, you can use the .NET Path class methods GetFileName() or GetFileNameWithoutExtension() for the same purpose. Here’s a quick example using Split-Path:

$fullPath = 'C:\Users\Example\Documents\File.txt'
$fileName = Split-Path $fullPath -Leaf

This will set $fileName to File.txt.

Get File Name from Path in PowerShell

A file path in PowerShell (and in Windows) typically consists of a drive letter, followed by a series of directories, and finally, the file name with an extension. For example, C:\Users\JohnDoe\Documents\Report.docx.

Let us now check different methods to get the file name from a path in PowerShell.

Method 1: Using the Split-Path Cmdlet

One of the built-in cmdlets in PowerShell that is designed for working with paths is Split-Path. This cmdlet can be used to split a path into its components and extract just the file name.

Here is an example.

$fullPath = 'C:\MyFolder\example.txt'
$fileName = Split-Path $fullPath -Leaf

The -Leaf parameter tells Split-Path to retrieve the last item in the path, which is the file name and extension. If you just want the file name without the extension, you can combine Split-Path with the BaseName property from the Get-Item cmdlet:

$fileNameWithoutExtension = (Get-Item $fullPath).BaseName

Here is the complete PowerShell script.

$fullPath = 'C:\MyFolder\example.txt'
$fileName = Split-Path $fullPath -Leaf
Write-Host 'File name- '$fileName
$fileNameWithoutExtension = (Get-Item $fullPath).BaseName
Write-Host 'File name without extension- ' $fileNameWithoutExtension

You can check out the output in the screenshot below; I executed the script using VS Code.

Get File Name from Path in PowerShell

Method 2: Using the Get-ChildItem Cmdlet

Another cmdlet that is useful for file manipulation is Get-ChildItem. This cmdlet can be used to list items in a directory, but it can also help extract file names from paths in PowerShell.

$fullPath = 'C:\Users\Bijay\Documents\Report.docx'
$fileInfo = Get-ChildItem $fullPath
$fileName = $fileInfo.Name

Here, Get-ChildItem retrieves an object representing the file, and the Name property gives you the file name with the extension.

Method 3: Using the .NET Path Class

PowerShell can also leverage the .NET Framework’s Path class to work with file paths. The Path class has a static method GetFileName() that returns the file name and extension from the path string.

Here is an example to get the file name from a path in PowerShell using .Net Framework methods.

$fullPath = 'C:\Users\Bijay\Documents\Report.docx'
$fileName = [System.IO.Path]::GetFileName($fullPath)

If you need to get the file name without the extension, you can use the GetFileNameWithoutExtension() method:

$fileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($fullPath)

Method 4: Using String Manipulation

You can also use traditional string manipulation methods in PowerShell to extract the file name from a path.

Here is the complete PowerShell script.

$fullPath = 'C:\Users\Bijay\Documents\Report.docx'
$fileName = $fullPath.Substring($fullPath.LastIndexOf('\') + 1)
$fileName

This script uses the Substring method to get a portion of the string starting from the character immediately after the last backslash, which is where the file name begins.

Conclusion

Extracting the file name from a path in PowerShell is a straightforward process that can be achieved using various techniques. I have explained here different methods to get the file name from a path in PowerShell using built-in cmdlets like Split-Path and Get-ChildItem, leveraging the .NET Path class methods, or performing string manipulation, etc.

You may also like: