Get Filename without Extension in PowerShell

💡 Quick Answer

You can get filename without extension in PowerShell using System.IO.Path ‘s GetFileNameWithoutExtension().
Syntax:

Output:

While working on a Windows operating system, you may often encounter the requirement of getting filename without extension.

For example:

FileName : C:\temp\sample.txt
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.

Output:

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;

Output:

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:

Output:

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.

Output:

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

Output:

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-Itemcmdlet, .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.

Was this post helpful?

Related Posts

  • Count files in folder in PowerShell
    01 January

    PowerShell – Count Files in Folders

    Table of ContentsCount Files in Folders in PowerShellCount Files in multiple Folders in PowerShellCount Files in Folders and Subfolders in PowerShellCount only folders inside folder in PowerShellCount Files by Extension in Folder in PowerShell Count Files in Folders in PowerShell We can use the Measure-Object cmdlet in different ways to meet our project requirements. However, […]

  • Check if Object contains property in PowerShell
    31 December

    Check if Object has Property in PowerShell

    Table of ContentsUsing the -match ParameterUsing -contains ParameterUsing if-else BlockUsing .Match() Function with if-else BlockUsing Get-Member Cmdlet Using the -match Parameter Use the -match parameter to check if an object has a property in PowerShell. [crayon-63b3bf2b5c81c758114948/] [crayon-63b3bf2b5c81e048722217/] Here, we used the -match parameter to check if the given object, which is $result in our case, […]

  • Create Empty File in PowerShell
    28 December

    Create Empty File in PowerShell

    Table of ContentsUsing New-Item CmdletUse the ni AliasUsing Out-File CmdletUsing the fsutil file CommandUsing Text EditorUsing echo CommandUsing for Loop Using New-Item Cmdlet Use the New-Item cmdlet to create an empty .txt file in PowerShell. [crayon-63b3bf2b5c3e7827206965/] [crayon-63b3bf2b5c3ed790393809/] We used the New-Item cmdlet with -Path, -Name, and -ItemType parameters to create an empty .txt file using […]

  • 27 December

    Run PowerShell as Another User

    Table of ContentsUsing the runas CommandOpen the PowerShell as an AdministratorRun the runas commandUsing the Start-Process cmdlet with the Credential ParameterUI-Based SolutionScript-Based Solution Using the runas Command The runas command in PowerShell launches programs using credentials different from the current user. It provides users with limited privileges to execute commands or access resources available only […]

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63b3bf2b61aa1408511151/] [crayon-63b3bf2b61aa5921281573/] We used numpy.empty() […]

  • 27 December

    Call Function from Another Function in Python

    Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63b3bf2b61c08066796850/] [crayon-63b3bf2b61c0b072145249/] To call a function, […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.