上次转载的一篇文章《PowerShell查看打开文件的默认应用程序》,它的亮点是调用了windows api,思路很独特,但是未必实用。后来@J在原文中评论,说assoc + ftype更方便。那么assoc 和 ftype是个神马东西,我简单描述下。
assoc 和 ftype是windows系统上cmd.exe中两个命令:
前者assoc 可以返回文件扩展名(比如.mp3)和系统对该扩展名本身的命名(比如WMP11.AssocFile.MP3)。
后者 ftype 可以返回系统的扩展名命名(比如WMP11.AssocFile.MP3)和默认的打开程序(比如:windows媒体播放器的程序路径,及可接受的参数)。
有了上面的知识和工具我们就可以大言不惭地在PowerShell中调用cmd中的命令,并显示所有的扩展名,和扩展名文件对应的默认打开程序。体现PowerShell强大兼容性的时刻又一次到来了:
$ftypeMap = @{}
cmd /c ftype | foreach {
$FileType,$Executable= $_ -split '='
$ftypeMap.Add($FileType,$Executable)
}
cmd /c assoc | foreach {
$Extension,$FileType = $_ -split '='
[pscustomobject]@{
Extension = $Extension
FileType = $FileType
Executable=$ftypeMap[$FileType]
}
} | Out-GridView
Remove-Variable -Name ftypeMap
</div>

PowerShell查看文件关联和默认打开程序
所以我们在和PowerShell这个新欢相处时,也不要把旧爱完全忘记啊。
我把上面的脚本稍加改动,在自己机器上生成了一个html表格:
| 文件后缀 | 文件扩展名 | 默认打开程序 |
|---|---|---|
| .386 | vxdfile | |
| .3g2 | WMP11.AssocFile.3G2 | “%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe” /prefetch:6 /Open “%L” |
| .3gp | WMP11.AssocFile.3GP | “%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe” /prefetch:6 /Open “%L” |
| .3gp2 | WMP11.AssocFile.3G2 | “%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe” /prefetch:6 /Open “%L” |
| .3gpp | WMP11.AssocFile.3GP | “%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe” /prefetch:6 /Open “%L” |
| .aac | WMP11.AssocFile.ADTS | “%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe” /prefetch:6 /Open “%L” |
| .accda | Access.ACCDAExtension.15 | D:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE /NOSTARTUP “%1″ |
| .accdb | Access.Application.15 | “D:\Program Files\Microsoft Office\Office15\MSACCESS.EXE” /NOSTARTUP “%1″ %2 %3 %4 %5 %6 %7 %8 %9 |
| .accdc | Access.ACCDCFile.15 | “D:\Program Files\Microsoft Office\Office15\MSACCESS.EXE” /NOSTARTUP “%1″ |
| .accde | Access.ACCDEFile.15 | “D:\Program Files\Microsoft Office\Office15\MSACCESS.EXE” /NOSTARTUP “%1″ %2 %3 %4 %5 %6 %7 %8 %9 |
| .accdr | Access.ACCDRFile.15 | “D:\Program Files\Microsoft Office\Office15\MSACCESS.EXE” /RUNTIME “%1″ %2 %3 %4 %5 %6 %7 %8 %9 |
| .accdt | Access.ACCDTFile.15 | “D:\Program Files\Microsoft Office\Office15\MSACCESS.EXE” /NOSTARTUP “%1″ |
| .accdu | Access.WizardUserDataFile.15 | “D:\Program Files\Microsoft Office\Office15\MSACCESS.EXE” /NOSTARTUP “%1″ |
| .accdw | Access.WebApplicationReference.15 | “D:\Program Files\Microsoft Office\Office15\MSACCESS.EXE” /NOSTARTUP “%1″ %2 %3 %4 %5 %6 %7 %8 %9 |
| .accessor | VisualStudio.accessor.12.0 | “D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe” /dde |
| .accft | Access.ACCFTFile.15 | |
| .accountpicture-ms | accountpicturefile | |
| .acl | ACLFile | |
| .actions4 | .actions4 | |
| .activitydiagram | VisualStudio.activitydiagram.12.0 | “D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe” /dde “%1″ |
| .AddIn | VisualStudio.AddIn.12.0 | “D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe” /dde |
| .ade | Access.ADEFile.15 | “D:\Program Files\Microsoft Office\Office15\MSACCESS.EXE” /NOSTARTUP “%1″ %2 %3 %4 %5 %6 %7 %8 %9 |
| .adn | Access.BlankProjectTemplate.15 | “D:\Program Files\Microsoft Office\Office15\MSACCESS.EXE” /NOSTARTUP /NEWDB “%1″ |
| .adp | Access.Project.15 | “D:\Program Files\Microsoft Office\Office15\MSACCESS.EXE” /NOSTARTUP “%1″ %2 %3 %4 %5 %6 %7 %8 %9 |
| .adt | WMP11.AssocFile.ADTS | “%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe” /prefetch:6 /Open “%L” |
| .adts | WMP11.AssocFile.ADTS | “%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe” /prefetch:6 /Open “%L” |
| .AIF | WMP11.AssocFile.AIFF | “%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe” /Open “%L” |
| .AIFC | WMP11.AssocFile.AIFF | “%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe” /Open “%L” |
| .AIFF | WMP11.AssocFile.AIFF | “%ProgramFiles(x86)%\Windows Media Player\wmplayer.exe” /Open “%L” |
| .ani | anifile | |
| .apk | apkfile | d:\Program Files (x86)\ROMasterLab\ApkInstaller.exe %1 |
| .appcontent-ms | ApplicationContent | |
| .application | Application.Manifest | “C:\Windows\System32\rundll32.exe” “C:\Windows\System32\dfshim.dll”,ShOpenVerbApplication %1 |
| .appref-ms | Application.Reference | “C:\Windows\System32\rundll32.exe” “C:\Windows\System32\dfshim.dll”,ShOpenVerbShortcut %1|%2 |
| .appxmanifest | VisualStudio.appxmanifest.12.0 | “D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe” “
您可能想查找下面的文章: |

