PowerShellで複数のExcelファイルを一括検索する方法【Microsoft Officeインストール前提】

eye-catching image of search excel files by powershell 仕事の工夫

この記事で扱う内容

この記事では、Microsoft Excel(以降、Excel)がインストールされた環境を前提に、PowerShellで複数のExcelファイルの中身を一括検索する方法をご紹介します。

想定読者

  • 複数の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で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」の「スクリプトを実行」ボタンを押して実行します。

スクリプトの実行結果

スクリプトの実行結果のアイキャッチ画像
  • 検索結果Excelファイルが出力先フォルダに出力され、自動で開きます。

補足・バリエーション

タイトルとURLをコピーしました