Table of Contents
💡 Quick Answer
You can get filename without extension in PowerShell using
BaseName
property withGet-Item
cmdlet.
123 (Get-Item C:\temp\sample.txt).BaseNameOutput:
123 sample
While working on a Windows operating system, you may often encounter the requirement of getting filename without extension.
For example:
FileName without extension: sample
In this post, we will see various ways to get FileName without extension in PowerShell.
Get FileName without extension for single file
Using BaseName property with Get-Item cmdlet
To get filename without extension in powershell, you can use Get-Item
cmdlet to get item at specified location and then use BaseName
attribute to get filename without extension.
1 2 3 |
PS> (Get-Item C:\temp\sample.txt).BaseName |
Output:
1 2 3 |
sample |
Here, Get-Item cmdlet is use to get item at specified location. This displays Name, Length, Mode, LastWriteTime and Directory of a particular file.
Here the command will be;
1 2 3 |
PS> Get-Item sample.txt |
Output:
1 2 3 4 5 6 7 |
Directory: C:\Users\Arpit Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 25-08-2021 13:11 33 sample.txt |
BaseName property along with the Get-Item helps you to get filename without extension.
If file does not exist, then you will get error as below:
1 2 3 |
PS> (Get-Item sample3.txt).BaseName |
Output:
1 2 3 4 5 6 7 8 |
Get-Item : Cannot find path 'C:\Users\Arpit\sample3.txt' because it does not exist. At line:1 char:2 + (Get-Item sample3.txt).BaseName + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Users\Arpit\sample3.txt:String) [Get-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand |
Using System.IO.Path ‘s GetFileNameWithoutExtension() method
To remove extension from the filename, you can use System.IO.Path
‘s GetFileNameWithoutExtension()
method. In this method, you will get the filename without extension in your PowerShell.
For that, run the following command to get your file name location at C:\User\rhtnm\test.txt.
1 2 3 |
PS> [System.IO.Path]::GetFileNameWithoutExtension('C:\User\rhtnm\test.txt') |
Output:
1 2 3 |
test |
If file doesn’t exist, you won’t get error in this method.
Get FileName without extension for multiple files
If you are looking to get your filename without extension in the case of your multiple files, use Get-ChildItem
cmdlet with GetFileNameWithoutExtension()
in PowerShell.
Here is PowerShell command to get FileName without extension for multiple files
1 2 3 |
PS> Get-ChildItem -Path C:\temp -Filter *.txt | ForEach-Object -Process {[System.IO.Path]::GetFileNameWithoutExtension($_)} |
Output:
1 2 3 4 5 6 |
Python sample sample1 text |
In above PowerShell script, Get-ChildItem
as cmdlet help you get specified path files while using a filter for getting .txt extension. Then the output is passed to the second command.
Second command uses ForEach-Object
to iterate over list of files and uses System.IO.Path’s GetFileNameWithoutExtension()
method on each file to get filename without extension.
Conclusion
Removing a filename without extension is easier than ever before. We discussed the BaseName
property with Get-Item
cmdlet, .Net Class System.IO.Path
‘s GetFileNameWithoutExtension()
method. We have also discussed how to get filename without extension for multiple files. You can apply these frameworks in an efficient way and get your filename without extension.