Search code examples
powershellfile-association

Get file type description for file extensions


I make a report for data usage on my disk, I get info from all selected property like name, path, size... behalf one filedescription, for each scanned file this property is empty. For example when you select a file in windows Explorer and you select property in general tab you can see "Type of file", here for an Excel file the type of file is "Microsoft Excel Worksheet (.xlsx)".

gci c:\file | select *

How can i get this info?


Solution

  • I like to avoid external programs when I can, so I would suggestion using the registry.

    $ext = ".xlsx"   
    $desc = (Get-ItemProperty "Registry::HKEY_Classes_root\$((Get-ItemProperty "Registry::HKEY_Classes_root\$ext")."(default)")")."(default)"
    $desc
    
    Microsoft Excel-regneark   #Norwegian description
    

    To use it with Select-Object you can modify it like this:

    #You could define this inside Select-Object too, but it's a bit long so I extracted it first to clean up the code.
    $extensiondesc = @{n="ExtensionDescription";e={ (Get-ItemProperty "Registry::HKEY_Classes_root\$((Get-ItemProperty "Registry::HKEY_Classes_root\$($_.Extension)")."(default)")")."(default)" }}
    
    Get-ChildItem |
    Select-Object Extension, $extensiondesc
    
    Extension ExtensionDescription
    --------- --------------------
    .oxps     XPS Document
    .lnk      Shortcut
    .txt      Text Document