How to Get File Extension Using PowerShell
There are several ways in PowerShell to get file extensions.

Method 1: Using the Split-Path and Split() method
# specify the file path
$filePath = "C:\temp\log\my_log.txt"
# Get the filename with extension
$fileNameWithExtension = Split-Path -Path $filePath -Leaf
# Split the filename and extension using the Split() method
$fileExtension = $fileNameWithExtension.Split(".")[1]
# output the file extension
Write-Output $fileExtension
This example uses the Split-Path cmdlet and Split() method to extract the file extension and output to the console.
Method 2: Using the [System.IO.Path] .Net class
# specify the file path
$filePath = "C:\temp\log\my_log.txt"
# Get the extension
$fileExtension = [System.IO.Path]::GetExtension($filePath)
# output the file extension
Write-Output $fileExtension
This example uses the [System.IO.Path] .Net class and its method GetExtension() to get file extension.
Method 3: Using the Get-ChildItem cmdlet
# specify the file path
$filePath = "C:\temp\log\my_log.txt"
# Get the file extension
$fileExtension = (Get-ChildItem -Path $filePath -File).Extension
# output the file extension
Write-Output $fileExtension
This example uses the Get-ChildItem cmdlet to extract the file extension.
Method 4: Using the Get-Item cmdlet
# specify the file path
$filePath = "C:\temp\log\my_log.txt"
# Get the file extension
$fileExtension = (Get-Item -Path $filePath).Extension
# output the file extension
Write-Output $fileExtension
This example uses the Get-Item cmdlet to extract the file extension.
Get File Extension Using Split-Path and Split() in PowerShell
You can use the Split-Path cmdlet to get the filename with extension using its -Leaf parameter and then use the Split() method to extract the file extension.
In this script, the $filePath variable stores the path to the file. The Split-Path cmdlet in PowerShell gets the filename with extension using its -Leaf parameter and stores it in the $fileNameWithExtension variable.
Finally, we call the Split() method over the $fileNameWithExtension variable to extract the file extension and output to the console using the Write-Output cmdlet.
Conclusion
I hope the above article on getting file extension using PowerShell is helpful to you.
Related Articles
File Information & Properties
- PowerShell Get File Properties - File metadata
- PowerShell Get File Hash - File integrity checking
- PowerShell Get File Version - Version information
- PowerShell Get File Owner - File ownership
- PowerShell Get MD5 Hash - Hash calculation
File Path & Navigation
- PowerShell Test-Path - Check if path exists
- PowerShell Get-Item - Get item objects
- PowerShell Split-Path - Extract path components
File Management Operations
- PowerShell List Files - List directory contents
- PowerShell Directory Listing - Navigate directories
- PowerShell Delete All Files - Delete files in bulk
- PowerShell Rename Files - Rename files in bulk
- PowerShell Count Files - Count directory contents
Date & Time Operations
- PowerShell DateTime Format - Format date/time
- PowerShell List Files By Date - Sort by date
- PowerShell Compare File Dates - Date comparison
Data Selection & Filtering
- PowerShell Select-Object - Select file properties
- PowerShell Where-Object - Filter files by criteria
- PowerShell ForEach-Object - Process each file
- PowerShell Get-ChildItem Filter - Advanced filtering
Display & Formatting
- PowerShell Format Table - Format output
- PowerShell Output Table - Create tables
- PowerShell Write-Output - Display output
Control Flow & Logic
- PowerShell If-Else Statement - Conditional logic
- PowerShell Switch Statement - Multi-value conditions
- PowerShell For Loops - Loop through files
- PowerShell Try-Catch - Error handling
Variables & Collections
- PowerShell Variables - Store file data
- PowerShell Arrays - Work with file arrays
- PowerShell Strings - String manipulation
- PowerShell Add-Member - Add custom properties
Output & Export
- PowerShell Output to File - Write to files
- PowerShell Export CSV - Export to CSV
- PowerShell Import CSV - Import from CSV
Storage & Disk Operations
- PowerShell Get Folder Size - Calculate folder size
- PowerShell Get Partition Size - Disk operations
Functions & Automation
- PowerShell Functions - Create reusable functions
- PowerShell Measure-Object - Calculate statistics
Comprehensive Guides
- Complete PowerShell Guide - Full PowerShell reference
- Complete PowerShell Tutorial - Comprehensive course