获取文件扩展名的文件类型说明

kek*_*ian 3 powershell file-association

我为我的磁盘上的数据使用情况做了报告,我从所有选定的属性获取信息,如名称,路径,大小...代表一个文件描述,对于每个扫描文件,此属性为空.例如,当您在Windows资源管理器中选择文件并在常规选项卡中选择属性时,您可以看到"文件类型",此处对于Excel文件,文件类型为"Microsoft Excel工作表(.xlsx)".

gci c:\file | select *
Run Code Online (Sandbox Code Playgroud)

我怎样才能获得这些信息?

Fro*_* F. 7

我想尽可能避免使用外部程序,所以我建议使用注册表.

$ext = ".xlsx"   
$desc = (Get-ItemProperty "Registry::HKEY_Classes_root\$((Get-ItemProperty "Registry::HKEY_Classes_root\$ext")."(default)")")."(default)"
$desc

Microsoft Excel-regneark   #Norwegian description
Run Code Online (Sandbox Code Playgroud)

要与它一起使用,Select-Object您可以像这样修改它:

#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 
Run Code Online (Sandbox Code Playgroud)


Vik*_*pta 5

让我们说$ext有文件的扩展名.

例如 -

$ext = ".bmp"
Run Code Online (Sandbox Code Playgroud)

以下代码将为您提供描述(如果已注册)(如果适合您的方案,您应该添加更好的错误处理) -

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

Write-Host $desc
Run Code Online (Sandbox Code Playgroud)

AFAIK,Powershell没有任何内置机制来获取这些信息,因此使用PowerShell中的cmd是最便宜和最简单的解决方案恕我直言.