複数のExcelファイルのシートを1つのExcelブックへ集約していく、PowerShellのサンプルコードを紹介します。
仕様
サンプルとして、以下のようなExcelファイル3つを用意します。
これら3ブックにあるシートを、新しいブックのAllSheets.xlsxにコピーします。
Book1.xlsx

Book2.xlsx

Book3.xlsx

実行結果のイメージは以下のとおりです。

絵心がなくてすみませんが・・
ソースコード
ソースはPowerShellで記述します。
createAllBookSheets.ps1
$excel = New-Object -ComObject Excel.Application
$book = $null
$excel.Visible = $false
$excel.DisplayAlerts = $false
# 出力先ファイル
$destFilePath = (Convert-Path .) + "\AllSheets.xlsx"
$destBook = $excel.Workbooks.add()
# 入力元ファイル
$sourceFiles = Get-Item *.xlsx
foreach($item in $sourceFiles){
$sourceBook = $excel.Workbooks.Open($item)
foreach($s in $sourceBook.sheets){
$sourceBook.Worksheets.item($s.Name).copy([System.Reflection.Missing]::Value,$destBook.Worksheets.item($destBook.worksheets.count))
}
[void]$sourceBook.Close($false)
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($sourceBook)
}
# AllSheetsの初期シートは削除
$destBook.Worksheets.item(1).delete()
[void]$destBook.SaveAs($destFilePath)
[void]$destBook.Close($false)
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($destBook)
[void]$excel.Quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Pause
1つ目のforeach文で、*.xlsxファイルを開いていきます。
2つ目のforeach文で、各シートを開きファイル「AllSheets.xlsx」にコピーしていきます。
実行
実行手順
では上記のPowerShellを実行します。
実行は右クリックメニューの「PowerShellで実行」をクリックします。

実行結果
実行後、ファイル「AllSheets.xlsx」が増えました。

ファイル「AllSheets.xlsx」を開くと、Book1~Book3にあったシートが全てコピーされています。

参考情報
環境
下記の環境で作成・実行しております。
No. | 環境 | バージョン |
---|---|---|
1 | OS | Windows10 |
2 | PowerShell | 5.1 |
以上です。