How to Get File Name Without Extension in PowerShell?

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 $fileNameWithoutExtension

This 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:

Get File Name Without Extension in PowerShell

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 $fileNameWithoutExtension

This 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 $fileNameWithoutExtension

Once 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 $fileNameWithoutExtension

This 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.