Recently, one of my clients asked me to write a PowerShell script to find file names without extensions in a folder. I tried different methods. In this PowerShell tutorial, I will explain how to get file name without extension in PowerShell using several methods.
To get the filename without the extension in PowerShell, you can use the BaseName property of a FileInfo object, which can be obtained by using the Get-Item cmdlet. Alternatively, employ the [System.IO.Path]::GetFileNameWithoutExtension() method from the .NET Framework for a more programmatic approach. Another option is to use the Split-Path cmdlet with the -LeafBase parameter, which directly outputs the file name without extension.
Using the Get-Item and BaseName Property
The simplest and easiest way to get a file name without its extension in PowerShell is to use Get-Item cmdlet followed by accessing the BaseName property. The BaseName property of a file object contains the name of the file without the extension. Here is the complete PowerShell script.
# Define the full path of the file
$filePath = "C:\MyFolder\Users.txt"
# Get the file name without the extension
$fileNameWithoutExtension = (Get-Item $filePath).BaseName
# Display the file name
Write-Output $fileNameWithoutExtensionThis will output Users, which is the file name without the .txt extension.
I executed the above script using VS code, and you can clearly see the output in the screenshot below:

Using System.IO.Path Class Methods
You can also use .NET Framework classes and methods to get file name without extension in PowerShell. You can use the System.IO.Path class and its method GetFileNameWithoutExtension. This .NET Framework method can be used directly in PowerShell like below
# Define the full path of the file
$filePath = "C:\example\sample.txt"
# Get the file name without the extension using System.IO.Path class
$fileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
# Display the file name
Write-Output $fileNameWithoutExtensionThis script will also return sample as the file name without the extension.
Using Split-Path Cmdlet
PowerShell’s Split-Path cmdlet can also be used to isolate the file name without the extension. This cmdlet is designed to manipulate path strings and can be used with the -LeafBase parameter.
# Define the full path of the file
$filePath = "C:\MyFolder\Users.txt"
# Use Split-Path with -LeafBase to get the file name without the extension
$fileNameWithoutExtension = Split-Path $filePath -LeafBase
# Display the file name
Write-Output $fileNameWithoutExtensionOnce you execute the above PowerShell script, it will show us the output like the first method.
Using Regular Expressions
You can also use regular expressions, PowerShell can match patterns and extract the file name without the extension using the -replace operator.
# Define the full path of the file
$filePath = "C:\example\sample.txt"
# Use regex to remove the extension from the file name
$fileNameWithoutExtension = $filePath -replace "\.[^.]+$"
# Extract just the file name without the path
$fileNameWithoutExtension = [System.IO.Path]::GetFileName($fileNameWithoutExtension)
# Display the file name
Write-Output $fileNameWithoutExtensionThis will output sample, stripping away both the directory path and the file extension.
Conclusion
In this tutorial, we’ve explored several methods to get the file name without the extension using PowerShell, like using native cmdlets like Get-Item and Split-Path, or .NET Framework methods like those from System.IO.Path.
The Get-Item and BaseName cmdlets are easy to read and use to get the file name without extension in PowerShell.
I hope now you get a complete idea of getting the file name without extension using PowerShell.
You may also like:
- Get File Creation Date in PowerShell
- Read JSON File into Array in PowerShell
- Delete File If Exists In PowerShell
- Copy and Rename Files in PowerShell
- How to Convert Files to Base64 in PowerShell?
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.