LoginSignup
3
2

More than 5 years have passed since last update.

レジストリからExcelに関連付けられている拡張子群を取得する

Posted at

Windowsの関連付けを決めるレジストリの優先順位とassoc ftypeが効かない問題について - イム日記

上記記事を参考に、PowerShellを使ってレジストリからExcelに関連付けられている拡張子群を取得してみた。

自身の確認も兼ねてステップを踏んで挙動を確認していく。

ステップ1:拡張子の一覧の確認

以下の処理を実行するとMicrosoft.Win32.RegistryKey型が得られ、Name(プロパティとしてはPSChildName)に拡張子が入っている。

ListAllExtensions
[string]$regKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts'
Get-ChildItem -LiteralPath $regKey
console
Name                           Property                                                           
----                           --------
...
.xla
.xlam
.XLL
.xls
.xlsb
.xlsm
.xlsx
.xlt
.xltm
.xltx
...

ステップ2:ProgIdの確認

今回はExcelに関連する拡張子を取得したい。
ExcelはProgIdを使っているようなので、得られたRegistryKeyからProgIdを取得してみる。

ListProgIds
[string]$regKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts'
Get-ChildItem -LiteralPath $regKey |
    # サブキーに OpenWithProgids があるものだけを抽出
    ?{ $_.GetSubKeyNames() -contains 'OpenWithProgids'  } |
    %{ $_.OpenSubKey('OpenWithProgids').GetValueNames() }

こちらを実行すると、以下のようなProgIdの一覧(string)が得られる。

console
...
Excel.Addin
Excel.AddInMacroEnabled
Excel.XLL
Excel.Sheet.8
Excel.SheetBinaryMacroEnabled.12
Excel.SheetMacroEnabled.12
Excel.Sheet.12
Excel.Template.8
Excel.TemplateMacroEnabled
Excel.Template
...

ステップ3:該当する拡張子の抽出

ステップ2のProgIdの中でExcel.~となっているものが、Excelに関連しているものだと思われる。

しかし、今回欲しいのはProgIdではなく拡張子のため、ステップ2の結果を使って、ステップ1をフィルターする。

ListExcelExtension
Get-ChildItem -LiteralPath $regKey |
    ?{
        # サブキーに OpenWithProgids があり、
        ($_.GetSubKeyNames() -contains 'OpenWithProgids') -and
        # 値の名前の中に 'Excel.*'が含まれているもの
        ($_.OpenSubKey('OpenWithProgids').GetValueNames() -like 'Excel.*')
    } |
    %{$_.PSChildName}
console
.csv
.ods
.xla
.xlam
.XLL
.xls
.xlsb
.xlsm
.xlsx
.xlt
.xltm
.xltx

おまけ(Excelの拡張子に一致する正規表現文字列の作成)

Excelファイルのフィルターに使いたかったため、上記の結果を使って正規表現用の文字列を作成してみる。

[string]$regKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts'

[string[]]$escapedXlExtensions =
    Get-ChildItem -LiteralPath $regKey |
        ?{
            # サブキーに OpenWithProgids があり、
            ($_.GetSubKeyNames() -contains 'OpenWithProgids') -and
            # 値の名前の中に 'Excel.*'が含まれているもの
            ($_.OpenSubKey('OpenWithProgids').GetValueNames() -like 'Excel.*')
        } |
        %{ [regex]::Escape( $_.PSChildName.Substring(1) ) }

'\.(?:{0})' -f ($escapedXlExtensions -join '|')
Excel拡張子正規表現
\.(?:csv|ods|xla|xlam|XLL|xls|xlsb|xlsm|xlsx|xlt|xltm|xltx)
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2