Home » PowerShell » PowerShell Get File Extension

PowerShell Get File Extension

File extension or filename extension is a suffix to the computer file that comes after the period eg.. (.ps1,.txt,.exe). In PowerShell to get a file extension, use Get-ChildItem, Split-Path, or .net framework [System.IO.Path] method.

Get-ChildItem cmdlet gets one or more items and has an Extension property that returns the file name extension.

In this article, we will discuss how to get file extension in PowerShell using Get-ChildItem, Split-Path, or .net framework method.

Get File Extension using Get-ChildItem in PowerShell

Use the Get-ChildItem cmdlet to get the file item and using its Extension property it returns the file name extension.

The Get-ChildItem command accepts the file path as input and gets the file item. It then passes the output to the Select command to get file extension using the Extension property.

 Get-ChildItem "D:\LogTest\FTP-02\PowerShell_Books.csv" | select Extension         

In the above PowerShell script, the Get-ChildItem command takes the file path as input and returns the file extension as .csv file.

The output of the above PowerShell script to get file name extension is

PowerShell Get File Extension
PowerShell Get File Extension

Get FileName Extension using Split-Path in PowerShell

Split-Path cmdlet in PowerShell is used to split the path and returns the specified part of the path. You can get a file name extension using the Split-Path command with additional logic to find an extension after the period.

Let’s understand with an example to get file name extension using Split-Path.

$filePath variable in PowerShell stores the file path. The Split-Path command takes $filePath as the input parameter and uses the Leaf parameter to split the file path to the last item.

The last item returned by the Split-Path command is further split by “.” to separate filename and extension. The index[1] returns the file extension as csv.

$filePath = "D:\LogTest\FTP-02\PowerShell_Books.csv" 

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

The output of the above PowerShell script to get file extension using the Split-Path command is:

PS D:\> $filePath = "D:\LogTest\FTP-02\PowerShell_Books.csv"   
                                              
PS D:\> (Split-Path -Path $filePath -Leaf).Split(".")[1]                                                     
csv

PS D:\> 

Cool Tip: How to get a file owner in PowerShell!

Using [System.IO.Path] to get filename extension

.Net Framework contains the classes to work with files and folders.

Using the [System.IO.Path] class static method GetExtension, you can get a file extension.

$filePath = "D:\LogTest\FTP-02\PowerShell_Books.csv" 

[System.IO.Path]::GetExtension($filePath)

In the above PowerShell script, $filePath variable contains the file path.

Use.net framework [System.IO.Path] class GetExtesnion method that accepts file path and returns the file extension.

The output of the above PowerShell script returned .csv file extension as given below:

PS D:\> $filePath = "D:\LogTest\FTP-02\PowerShell_Books.csv" 

PS D:\> [System.IO.Path]::GetExtension($filePath)
.csv

PS D:\>                                                                                                                 

Cool Tip: How to get file attributes in PowerShell!

Conclusion

I hope the above article on how to get file extension in PowerShell using Get-ChildItem, Split-Path, or .net framework class is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.