Excelの各セルのフォント名(フォントファミリー)を変更するPowerShellのサンプルコードを紹介します。
仕様
ローカルの任意の場所にPowerShellの*.ps1ファイルと、Excelファイルを格納します。

「Book1.xlsx」のA列のセルのフォント名を変更してみます。
後でPowerShellの実行前後を比較するため、B列にもA列の内容をコピーしておきます。

PowerShell実行前のフォント名は游ゴシックです。
これを以下のような別のフォント名に変更させます。
セル位置 | 実行前 | 実行後 |
---|---|---|
A1 | 游ゴシック | MS Pゴシック |
A2 | 游ゴシック | MS ゴシック |
A3 | 游ゴシック | MS 明朝 |
A4 | 游ゴシック | メイリオ |
A5 | 游ゴシック | 游ゴシック |
ソースコード
以下のコマンドとps1ファイルを準備しておきます。
コマンド
powershell -ExecutionPolicy RemoteSigned -File "setExcelFontName.ps1"
setExcelFontName.ps1
$excel = New-Object -ComObject Excel.Application
$book = $null
$excel.Visible = $false
$excel.DisplayAlerts = $false
$fileName = "Book1.xlsx"
$filePath = (Convert-Path .) + "/" + $fileName
$book = $excel.Workbooks.Open($filePath)
$sheet = $book.Sheets("Sheet1")
$cell = $sheet.Cells(1,1)
$cell.font.Name = "MS Pゴシック"
$cell = $sheet.Cells(2,1)
$cell.font.Name = "MS ゴシック"
$cell = $sheet.Cells(3,1)
$cell.font.Name = "MS 明朝"
$cell = $sheet.Cells(4,1)
$cell.font.Name = "メイリオ"
$cell = $sheet.Cells(5,1)
$cell.font.Name = "游ゴシック"
[void]$book.Save()
[void]$book.Close($false)
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($book)
[void]$excel.Quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Pause
実行
実行手順
Windows PowerShellのターミナルからsetExcelFontName.ps1を実行するコマンドを打ちます。

実行結果
以下の通り、ExcelファイルのA列のフォント名が変わりました。

見た目だけでもフォントが変更されたことが分かります。
ただ本当に変更されているか、各セルにフォーカスを当てて確認しました。





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