指定したExcelファイルのシート名を全て取得するPowerShell
表題の件、スクリプトを作成しました。
業務をしていてあると便利だな、と。
# 指定したExcelのシート名を全て取得するPowerShell # 引数1:ファイル名 # ファイルのパスを取得 $scriptPF = $MyInvocation.MyCommand.Path $scriptPath = Split-Path -Parent $scriptPF $filePF = $scriptPath + "\" + $args[0] # ファイルが存在する場合に処理 if (Test-Path $filePF) { # Excelのオープン $objExcelAp = New-Object -ComObject Excel.Application $objExcelAp.Visible = $true $objExcelBook = $objExcelAp.Workbooks.Open($filePF) $objExcelBook.Sheets | ForEach-Object { $_.Name | Out-File ($scriptPath + "_AllSheetsName.txt") -Append -Encoding Default } # ファイルを閉じてExcelの終了 $objExcelAp.Quit() # 各変数を解放(.ブロックでOut-Nullしているのは実行すると「0 0 0」と表示されるため) .{ [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcelBook) $objExcelBook = $null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcelAp) $objExcelAp = $null } | Out-Null # ガベージコレクトの明示実行 [GC]::Collect() [GC]::WaitForPendingFinalizers() [GC]::Collect() }