Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinct list of file types in Powershell

Tags:

powershell

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 :

  • foo .mp3
  • foo .jpeg
  • bar .flac
  • bar .txt

I guess I should use Get-Unique but how do I specify it on the Extension property and NOT on the Path property?

like image 271
Lupuss Avatar asked Dec 17 '12 20:12

Lupuss


People also ask

How do I get unique values in PowerShell?

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.

How do I get a list of files in PowerShell?

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.

What are the different file extensions in PowerShell?

Specific file types of interest in Windows PowerShell are script files ( . ps1 ), script data files ( . psd1 ), and script module files ( . psm1 ).


1 Answers

Just add -Unique to Select-Object:

Get-Childitem -Recurse | Select-Object PSParentPath,Extension -Unique 

(Also, DirectoryName might be better than PSParentPath here)

like image 200
Jimmeh Avatar answered Oct 15 '22 00:10

Jimmeh