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
1 |
[System.IO.Path]::GetFileNameWithoutExtension(<path>) |
For example,
1 |
[System.IO.Path]::GetFileNameWithoutExtension("C:\Users\kipro\OneDrive\Desktop\dir33\help1.txt") |
Output:
help1
This method works whether or not the file exists.
Method 2: Using Get-Item and get Basename
1 |
(Get-Item <filepath>).Basename |
For example,
1 |
(Get-Item 'C:\Users\kipro\OneDrive\Desktop\dir33\help.txt').Basename |
Output:
help
The method above only works if the file exists.
Method 3: Using the FileInfo method on System.IO Namespace
1 |
([System.IO.FileInfo] <filepath>).basename |
For example,
1 |
([System.IO.FileInfo] "c:\temp\dummyfile.txt").basename |
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:
1 |
dir <dir> | select basename |
Or
1 |
ls <dir> | select BaseName |
For example,
1 |
ls .\Desktop\dir33\ | select basename |
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.
1 |
Get-ChildItem -Path <path> | ForEach-Object -Process {[System.IO.Path]::GetFileNameWithoutExtension($_)} |
An example:
1 |
Get-ChildItem -Path .\Desktop\dir33/ | ForEach-Object -Process {[System.IO.Path]::GetFileNameWithoutExtension($_)} |
Output (truncated):
convert_xlsx_to_csv … SalaryData2-Copy SalaryData2.Copy(2) SalaryData2
Method 3: Using GetChildItem then, select Basename
1 |
Get-ChildItem -Path <path> | select basename |
For example,
1 |
Get-ChildItem -Path dir33\ | select basename |
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Define paramter(s). The path is a directory we want to get the listing # of files without the extension. # By default, it is ".\" - the current folder. param([string]$Path=".\") # Get all files in the directory. $files = Get-ChildItem $Path # Loop through the files found foreach($file in $files) { # It returns the directory concatenated with the filename without extension. $fullpath = $Path +"\"+$file.BaseName $fullpath # Provide the filename without the extension. $file.BaseName } |
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:
1 |
cd <path_to_directory_containing_the_script> ; .\get_filepath_witout_extension.ps1 -Path <path> |
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
1 |
.\get_filepath_witout_extension.ps1 -Path <path> |
For example;
1 |
cd C:\Users\kipro\OneDrive\Desktop\ ; .\get_filepath_without_extension.ps1 -Path "C:\Users\kipro\OneDrive\Desktop\dir33" |
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,
1 |
Get-ChildItem -Path .\Desktop\dir33/ | ForEach-Object -Process {[System.IO.Path]::GetFileNameWithoutExtension($_)}>output1.txt |
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.