Get Filename Without Extension in PowerShell

This article will cover different ways of getting filenames without extensions in Powershell. We will see how to get a filename without an extension for a single file path or a directory listing without extensions.

Getting a filename without extension for a single path

This Section discusses methods to get the filename without extension for a single file path.

Method 1: Using GetFileNameWithoutExtension() method on use System.IO.Path namespace

For example,

Output:

help1

This method works whether or not the file exists.

Method 2: Using Get-Item and get Basename

For example,

Output:

help

The method above only works if the file exists.

Method 3: Using the FileInfo method on System.IO Namespace

For example,

Output:

dummyfile

Like Method 1, this method works whether or not <filepath> is a valid path.

Get filenames without extensions for files in a directory

This Section covers four methods of getting a directory listing without extensions.

Method 1: Using ls or dir then, select the Basename

The ls [options] <dir> or dir [options] <dir> commands return a listing of <dir> if <dir> is provided; otherwise, return the listing of the current working directory.

To get a listing without extensions, we can use either of those commands and then extract basenames only using:

Or

For example,

Output (truncated):

BaseName
--------
convert_xlsx_to_csv
…
SalaryData2-Copy
SalaryData2.Copy(2)
SalaryData2

Method 2: Using the Get-ChildItem method with an iterator and System.IO.Path namespace.

In this method, we use the Get-ChildItem method to get all file items in a given directory and then pipe the results to GetFileNameWithoutExtension() method on System.IO.Path to output filenames with no extension. The general syntax is given below.

An example:

Output (truncated):

convert_xlsx_to_csv
…
SalaryData2-Copy
SalaryData2.Copy(2)
SalaryData2

Method 3: Using GetChildItem then, select Basename

For example,

Output:

BaseName
--------
convert_xlsx_to_csv
help
help2
infile_image
SalaryData2-Copy
SalaryData2.Copy(2)
SalaryData2

Method 4: Having more control by using a PowerShell script

All we have covered in the previous methods can be turned into a PowerShell (.ps1) script. In this Subsection, we want to create a script that takes a path as a parameter and returns filenames (including full paths) without extensions.

Point A: Creating the script

Let’s start by saving the following content into get_filepath_witout_extension.ps1 (or any name, really).

Point B: What the script does

The script above allows us to pass the parameter Path from the PowerShell Terminal. If the Path value is not given, the default one is used – the current working directory (“.\”).

Next, the Get-ChildItem is used to get all files in the Path. Lastly, we loop through all the files fetched and print out the full path (without extension) and basenames.

Point C: Running the script

To execute the script we just created, run the following command on the PowerShell:

The line above chains two commands separated by a semi-colon (;). The first part changes the working directory to the folder containing the get_filepath_witout_extension.ps1, and the second part executes the actual script. If you are inside the directory containing the script, execute

For example;

Output (truncated):

C:\Users\kipro\OneDrive\Desktop\dir33\convert_xlsx_to_csv
convert_xlsx_to_csv
C:\Users\kipro\OneDrive\Desktop\dir33\help
Help
…
Note: You can pipe the results of any of the commands into another file using ">" or ">>" operators. The ">" redirects output to a file overwriting the file if a file of the same name already exists in the same location). Conversely, the ">>" appends the output to a file without overwriting. For example,

writes output into output1.txt in the current working directory.

Conclusion

In this article, we learned how to get filenames without extension in PowerShell. We discussed several methods, which can be divided into two categories – methods for extracting filenames without an extension given a single path and those which can fetch filenames without extensions for all files in a directory.