How to Get File Extensions from Filenames in PowerShell?

I was working on an automation project when I was required to extract file extensions from filenames using PowerShell. I tried different methods, and in this tutorial, I will explore various methods to retrieve the file extension from a filename in PowerShell with complete scripts.

To get the file extension from a filename in PowerShell, you can use the Get-Extension method from the [System.IO.Path] class. For example, $fileExtension = [System.IO.Path]::GetExtension(‘C:\example\report.xlsx’) will store the string ‘.xlsx’ in the $fileExtension variable. This method is straightforward and effective for extracting the suffix that indicates the file type.

Get File Extensions from Filenames in PowerShell

A file extension is a suffix at the end of a filename that indicates the file type. It is separated from the main file name by a dot. For example, in ‘document.pdf’, ‘.pdf’ is the file extension that suggests the file is a PDF document.

PowerShell provides different methods and cmdlets to get the file extension from a file name.

Method 1: Using System.IO.Path Class

One of the simplest ways to get a file extension in PowerShell is by using the [System.IO.Path] class. This class provides static methods for creating, moving, and enumerating through directories and files.

Here’s an example script that explains how to use the [System.IO.Path] class to extract the file extension:

# Define the full path of the file
$filePath = "C:\MyFolder\users.xlsx"

# Extract the extension from the file path
$fileExtension = [System.IO.Path]::GetExtension($filePath)

# Display the file extension
Write-Host "The file extension is: $fileExtension"

In the above PowerShell script, the GetExtension() method is used to obtain the file extension from the full path of the file.

You can see the output in the screenshot below after I executed the script using vs code.

Get File Extensions from Filenames in PowerShell

Method 2: Using Get-ChildItem and Select-Object

PowerShell’s Get-ChildItem cmdlet is another way to fetch details about files. Combined with the Select-Object cmdlet, you can isolate and display the file extension.

Here’s how you can use these cmdlets to get the file extension:

# Define the directory path
$directoryPath = "C:\MyFolder"

# Get all files in the directory and select only the extension property
$fileExtensions = Get-ChildItem -Path $directoryPath | Select-Object Extension

# Display the file extensions
$fileExtensions | ForEach-Object { Write-Host "Found file extension: $($_.Extension)" }

This script retrieves all files in the specified directory and then uses Select-Object to pick out just the extension property of each file.

Look at the output in the screenshot below; it displays all the file extensions in the mentioned folder. Where the extensions not mentioned are the subfolders.

How to Get File Extensions from Filenames in PowerShell

Method 3: Using Split-Path

The Split-Path cmdlet in PowerShell is designed to manipulate both the path and the leaf of a given location. You can use it to split the path and the file name, isolating the extension.

Example script using Split-Path:

# Define the full file path
$filePath = "C:\MyFolder\Customers.xlsx"

# Use Split-Path to separate the file name and extension
$fileNameAndExtension = Split-Path -Path $filePath -Leaf

# Extract the extension from the full file name
$fileExtension = $fileNameAndExtension -replace '.*\.'

# Display the file extension
Write-Host "The file extension is: $fileExtension"

In this script, Split-Path gets the leaf of the path (the file name and extension), and then a regular expression is used to remove everything before the dot, leaving just the extension.

You can see the output in the screenshot below after I executed the script using VS code.

powershell get file extension from filename

Method 4: Using Regular Expressions

PowerShell can also use regular expressions to extract the file extension from a filename.

Here’s a script that uses regex to find the extension:

# Define the filename
$fileName = "archive.tar.gz"

# Use regex to extract the file extension
if ($fileName -match "\.(?<Extension>[^.]+)$") {
    $fileExtension = $Matches['Extension']
    Write-Host "The file extension is: $fileExtension"
} else {
    Write-Host "No file extension found."
}

This script uses the -match operator to check if the filename matches the pattern for a file extension. If a match is found, it extracts the extension using the $Matches automatic variable.

Conclusion

I hope now you must learn how to get file extensions from filenames in PowerShell. I have explained different methods to get file extensions from file names, like using the [System.IO.Path] class, or the Get-ChildItem cmdlet, by using the Split-Path, or by using regular expressions, etc. I hope it helps.

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.