How to Get Unique File Extensions in a Directory


In this blog post, we will walk through how to get list of all unique file extensions in a directory.

In this context, our directory will look as follows:

scripts-directory-display scripts-export-subdirectory-display

It contains various files and one subfolder named Export.


Recursing directory and use Unique property

To solve this problem, we must know a property that denotes file extension which is Extension.

Having known this property, we can pipe several commands that will traverse and recurse the directory to find all file extensions available. Then, we use Unique property to get distinct values.

Get-ChildItem -Path 'C:\Scripts' -File -Recurse 
| Select-Object -Property Extension -Unique

The output is as follows:


Recursing directory and use Group-Object

A little more verbose than above solutions, we can also use Group-Object cmdlet to get unique file extensions.

Get-ChildItem -Path 'C:\Scripts' -File -Recurse 
| Select-Object -Property Extension
| Group-Object -Property Extension

As you can see in the output below, the good thing using Group-Object is we get files associated with the extension or file type. We also know the number of files having that extension.


If we are only interested with the list of unique extensions, we can modify the script as follows:

Get-ChildItem -Path 'C:\Scripts' -File -Recurse 
| Select-Object -Property Extension
| Group-Object -Property Extension
| Select-Object -Property Name

The output will be similar to previous solution:



In order to get list of unique file extensions, we can recurse the directory then find the unique extensions using Unique property or Group-Object cmdlet.