
PowerShellで複数のExcelファイルを一括検索する方法【Microsoft Officeインストール前提】
/ 5 min read
View more blogs with the tag IT・AI活用 - 使い方・手順 , View more blogs with the tag ITスキル , View more blogs with the tag Microsoft Excel , View more blogs with the tag Microsoft Office , View more blogs with the tag Microsoft Windows 10 , View more blogs with the tag Microsoft Windows 11 , View more blogs with the tag PowerShell , View more blogs with the tag 実践 , View more blogs with the tag 業務効率化 , View more blogs with the tag 自動化
Table of Contents
この記事で扱う内容
この記事では、Microsoft Excel(以降、Excel)がインストールされた環境を前提に、**PowerShellで複数のExcelファイルの中身を一括検索する方法**をご紹介します。
前提
前提知識・前提スキル
- Windows OS の基本操作ができる人
前提環境
- 使用PC
- OS が Windows 10または 11であること
- PowerShell 5.1がインストールされていること(Windows 10または11標準搭載)
- PowerShellの実行ポリシーで、PowerShellスクリプトの実行が許可されていること
- PowerShell ISEがインストールされていること(Windows 10または11標準搭載)
- Microsoft Excelがインストールされていること
今回は、COM (Component Object Model)という仕組みを使用します。
Excelさんが「別のプログラムから自分を操作できるようにしている」ので、それを使うという感じです。
参考記事PowerShellスクリプトが実行できないときの原因と対処法【実行ポリシーに注意】
PowerShellでExcelファイルの中身を一括検索する方法
スクリプトの作成
PowerShell ISEを起動する
スタートメニューで「powershell ise」を検索後、「Windows PowerShell ISE」を選択し起動する
PowerShell ISEのスクリプトウィンドウにコードを貼りつけ、任意の場所に保存する
例)D:\PowerShellScript\Search-ExcelContentSimple.ps1
$searchDir = "検索対象のExcelファイルを格納しているフォルダパス(例:D:\PowerShellScript\excel)"$keyword = "検索対象のキーワード(例:1)"$output = "検索結果Excelファイルの出力先パス(例:D:\PowerShellScript\result\search_result.xlsx)"
$excel = New-Object -ComObject Excel.Application$excel.Visible = $false$excel.DisplayAlerts = $false
$results = @()
Get-ChildItem -Path $searchDir -Include *.xlsx, *.xls -Recurse | ForEach-Object { $file = $_.FullName $workbook = $excel.Workbooks.Open($file, $null, $true) foreach ($sheet in $workbook.Sheets) { foreach ($row in $sheet.UsedRange.Rows) { foreach ($cell in $row.Columns) { if ($cell.Text -like "*$keyword*") { $results += [PSCustomObject]@{ FilePath = $file Sheet = $sheet.Name CellAddress = $cell.Address() Value = $cell.Text } } } } } $workbook.Close($false)}
if ($results.Count -gt 0) { $resultWorkbook = $excel.Workbooks.Add() $sheet = $resultWorkbook.Sheets.Item(1) $sheet.Cells.Item(1,1).Value2 = "ファイルパス" $sheet.Cells.Item(1,2).Value2 = "シート名" $sheet.Cells.Item(1,3).Value2 = "セル位置" $sheet.Cells.Item(1,4).Value2 = "一致した値"
for ($i = 0; $i -lt $results.Count; $i++) { $sheet.Cells.Item($i+2,1).Value2 = $results[$i].FilePath $sheet.Cells.Item($i+2,2).Value2 = $results[$i].Sheet $sheet.Cells.Item($i+2,3).Value2 = $results[$i].CellAddress $sheet.Cells.Item($i+2,4).Value2 = $results[$i].Value }
$resultWorkbook.SaveAs($output) $resultWorkbook.Close($false) Invoke-Item -Path $output}
$excel.Quit()[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
スクリプトの実行手順
検索対象のExcelファイルを任意のフォルダへ格納する
例)D:\PowerShellScript\excel
今回は例として、2ブック×2シートのExcelデータを用意しました。
1.xlsx
2.xlsx
検索結果Excelファイルの出力先フォルダを準備する
例)D:\PowerShellScript\result
コード内の設定用変数に値を設定する
| No | 設定用変数 | 設定値 | 例 |
|---|---|---|---|
| 1 | $searchDir | 検索対象のExcelファイルを格納しているフォルダパス | D:\PowerShellScript\excel |
| 2 | $keyword | 検索対象のキーワード | 1 |
| 3 | $output | 検索結果Excelファイルの出力先パス | D:\PowerShellScript\result\search_result.xlsx |
スクリプトを実行する
スクリプトの実行方法には色々な方法がありますが、今回は「PowerShell ISE」の「スクリプトを実行」ボタンを押して実行します。
参考記事PowerShellスクリプトが実行できないときの原因と対処法【実行ポリシーに注意】
スクリプトの実行結果
- 検索結果Excelファイルが出力先フォルダに出力され、自動で開きます。
おすすめの関連記事
参考記事PowerShellで複数のExcelファイルを一括検索する方法【ログ出力・エラー処理付き版】
参考記事バッチファイルでPowerShellの実行ポリシーを設定する方法【自動化向け】