How to get the file extension using PowerShell?


We can retrieve the file extension using multiple ways. First, using the [System.IO.Path] class.

PS C:\> [System.IO.Path]::GetExtension("C:\temp\25Aug2020.txt")
.txt
PS C:\> [System.IO.Path]::GetExtension("C:\temp\azcopy.zip")
.zip

This is the easiest way to get the file extension. Otherways, Using programmatically,

PS C:\> ((Split-Path "C:\Temp\azcopy.zip" -Leaf).Split('.'))[1]
zip
PS C:\> ((Split-Path "C:\Temp\25Aug2020.txt" -Leaf).Split('.'))[1]
txt

Using Get-ChildItem,

PS C:\> (Get-ChildItem C:\Temp\azcopy.zip).Extension
.zip
PS C:\> (Get-ChildItem C:\Temp\25Aug2020.txt).Extension
.txt

Using Get-Item,

PS C:\> (Get-Item C:\Temp\azcopy.zip).Extension
.zip

Updated on: 17-May-2021

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements