I'm trying to write a one-liner in Powershell that lists all filetypes in a folder (and its sub-folders).
I've got this so far:
Get-ChildItem D:\Audio -Recursive | Select-Object PSParentPath, Extension | Export-Csv D:\Test.csv
However, I'd like to do better and display, for each folder, every found extension only once.
For example, let's say I have ten mp3 files and 1 jpg file in D:\Audio\foo and 5 flac files and 1 txt file in D:\Audio\bar. I'd like the output to be :
I guess I should use Get-Unique but how do I specify it on the Extension property and NOT on the Path property?
The Get-Unique cmdlet compares each item in a sorted list to the next item, eliminates duplicates, and returns only one instance of each item. The list must be sorted for the cmdlet to work properly. Get-Unique is case-sensitive. As a result, strings that differ only in character casing are considered to be unique.
PowerShell utilizes the “Get-ChildItem” command for listing files of a directory. The “dir” in the Windows command prompt and “Get-ChildItem” in PowerShell perform the same function.
Specific file types of interest in Windows PowerShell are script files ( . ps1 ), script data files ( . psd1 ), and script module files ( . psm1 ).
Just add -Unique to Select-Object:
Get-Childitem -Recurse | Select-Object PSParentPath,Extension -Unique
(Also, DirectoryName might be better than PSParentPath here)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With