How to get the file extension using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

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
raja
Published on 17-May-2021 12:44:32
Advertisements