2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Powershellでパスワード付きExcelファイル複数を開く

Last updated at Posted at 2020-05-20

概要

パスワード付きのExcelファイルたくさん(パスワードはすべて共通)をいっぺんに開きたいので、Poweshellスクリプトを書いた。

使い方

  1. 開きたいファイルを適当なフォルダにまとめておく。
  2. 同じフォルダに以下のスクリプトをopen_excels.ps1として保存(メモ帳を開きスクリプトをコピペ、open_excels.ps1と名前を付けて保存)。
  3. エクスプローラでopen_excel.ps1を右クリック、「Powershellで実行」を選択
  4. (「実行ポリシーを変更しますか?」などの警告が表示された場合)y + Enter
  5. パスワードを入力しEnter
  6. ファイルが順番に自動で開く(開けなかった場合、「開けませんでした」と表示)
  7. Enterを押して終了

スクリプト

open_excel.ps1
$password = Read-Host("パスワードを入力")
$filepaths = Get-Item *.xlsx

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $true

foreach($filepath in $filepaths){
    try {
        Write-Output $filepath
    $excel.Workbooks.Open($filepath, [Type]::Missing, [Type]::Missing, [Type]::Missing, $password) > $null
    } catch {
        Write-Host "ファイル" $filepath "は開けませんでした"
    }
}

$excel = $null
[System.GC]::Collect()

Read-Host "Enterで終了"
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?