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 -LeafThis 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 -LeafThe -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).BaseNameHere 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- ' $fileNameWithoutExtensionYou can check out the output in the screenshot below; I executed the script using VS Code.

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.NameHere, 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)
$fileNameThis 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:
- How to Move a File to a Folder in PowerShell?
- How to Get the Path of a File in PowerShell?
- How to Write Variables to a File in PowerShell?
- PowerShell Write-Host to File
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.