Get the File Extension Using PowerShell

Marion Paul Kenneth Mendoza Jan 30, 2023 Jan 31, 2022
  1. Get the File Extension Using Split-Path in PowerShell
  2. Get the File Extension Using Get-ChildItem Cmdlet in PowerShell
  3. Get the File Extension Using .NET Framework in PowerShell
Get the File Extension Using PowerShell

It is usually required to have a file name extracted from the full path while working on your PowerShell scripts. For example, your script received a full file path, and you want only to get the file extension.

This article will discuss several file extension methods using PowerShell scripting.

Get the File Extension Using Split-Path in PowerShell

To split the extension from the filename, we can use the -Leaf parameter to indicate where we will extract the extension. The leaf is the last element or part of a path.

$filePath = "C:\temp\subfolder\File1.txt";
$extension = (Split-Path -Path $filePath -Leaf).Split(".")[1];
Write-Output $extension

Output:

txt

You may notice that in our code snippet, we called the Split() function that will split the path provided.

We used the dot . as the separator because the filename and the extension are divided by a dot separator. We then called the array [1] the stored extension value.

Try calling array [0], and you will get the filename of the path.

Example Code:

(Split-Path -Path $filePath -Leaf).Split(".")[0];

Output:

File1

Since we use the dot character as our separator, this method will only work if your filenames do not contain any other dots. Remember that a dot character can be in a filename.

Get the File Extension Using Get-ChildItem Cmdlet in PowerShell

The Get-ChildItem command gets the items in one or more specified locations. For example, if the object is a container, it gets the things inside the said container, known as child items.

A location can be a file system, such as a directory, or a site exposed by a different Windows PowerShell provider. The Get-ChildItem command gets the directories, subdirectories, and files in a file system drive.

Since the Get-ChildItem cmdlet deals with files, it has a PowerShell property attribute that we can export to get the extension of the queried file.

Unlike the Split-Path cmdlet, this method can ship the extension properly even though there are dot characters in the filename.

Example Code:

Get-ChildItem 'C:\temp\file.1.txt' | Select Extension

Output:

Extension
---------
.txt

Get the File Extension Using .NET Framework in PowerShell

The following approach is based on .NET framework classes. Though it is commonly discouraged to use .NET framework classes on PowerShell scripts, especially if a native PowerShell command is available, it may be appropriate for this specific use case.

In the example below, if you are given the file name, we would use GetExtension static methods from the System.IO.Path class:

Example Code:

[System.IO.Path]::GetExtension("File1.txt")

Output:

.txt

We can also use the GetFileNameWithoutExtension static method instead if we want to get the filename.

Example Code:

[System.IO.Path]::GetFileNameWithoutExtension("File1.txt")

Output:

File1
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell File