I thought that this would be an easy task for Powershell: definitely a one-liner. How would I do it though? I’d start with Get-ChildItem, expose an extension property, do some piping, no problem. Yet I drew a blank on the details. Fortunately the Scripting Guy had it all worked out here, just like this:
Get-ChildItem C:\Scripts | Select-Object Extension | Sort-Object Extension | Get-Unique
Thanks Scripting Guy! But wait, I wanted to run it recursively so I just added a
-Recurse and lo and behold: no results! What in the binaries is going on here?
Going back to the article, I learned that Get-Unique is not doing what I thought it might do. It is comparing objects rather than strings. So I had to add an
-AsString parameter comme ça:
Get-ChildItem C:\Scripts -Include *.* -Recurse | Select-Object Extension | Sort-Object Extension | Get-Unique -AsString
That got me closer, except for one more tiny little detail. There were lowercase and uppercase extensions causing duplicates. I needed a case insensitive unique list. I had to ditch the Get-Unique cmdlet and go back to the Sort-Object since it had a
Get-ChildItem c:\Scripts -Include *.* -Recurse | Select-Object Extension | Sort-Object -Property Extension -Unique
Now we are back in business!