3

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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
kekimian
  • 909
  • 3
  • 13
  • 21
  • That information is not in the file but is provided by the Shell. In regular development languages, one would use the [SHGetFileInfo](http://msdn.microsoft.com/en-us/library/windows/desktop/bb762179(v=vs.85).aspx) Shell function to retrieve that information but I can't for the life of me port it to powershell. A good start would be [this](http://stackoverflow.com/questions/22871595/clear-recycle-bin-programmatically-with-powershell) question – Lieven Keersmaekers Dec 25 '14 at 10:15

4 Answers4

7

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 
Frode F.
  • 52,376
  • 9
  • 98
  • 114
5

Lets say $ext has the extension of the file.

For example -

$ext = ".bmp"

Following code will get you the description, if registered (you should add better error handling if appropriate for your scenario) -

$desc = (cmd /c assoc $ext).Split("=")[1]
$desc = (cmd /c assoc $desc).Split("=")[1]

Write-Host $desc

AFAIK, Powershell does not have any built in mechanism to get this information, and hence using cmd from powershell is the cheapest and easiest solution IMHO.

Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
1

You could use the GetDetailsOf() method of the Shell.Application object:

$app = New-Object -COM 'Shell.Application'

$f   = Get-Item 'C:\path\to\your\file'
$dir = $app.NameSpace($f.Directory.FullName)

$description = $dir.GetDetailsOf($dir.ParseName($f.Name), 2)
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

I have just improvised on Frode F's solution by adding a sorted list with unique entries to make it easier to read.

$extensiondesc = @{n="ExtensionDescription";
e={(Get-ItemProperty "Registry::HKEY_Classes_root\$((Get-ItemProperty "Registry::HKEY_Classes_root\$($_.Extension)")."(default)")")."(default)" }}
Get-ChildItem |
Select-Object -unique Extension, $extensiondesc |Sort-Object @{e="Extension";Ascending=$true},@{e="ExtensionDescription";Ascending=$false}