Search code examples
excelvbapowershellpasswordspassword-protection

How to open Excel file with 'password to modify', using PowerShell


I am trying to open an Excel file with 'password to modify' using PowerShell.

I have tried the standard cord to open a password-protected file (see below), but it does not work for 'password to modify'.

$wb = $excel.Workbooks.Open($filepath, [Type]::Missing, [Type]::Missing, [Type]::Missing, $password)

I know that I can open such a file using the code below when I use Excel VBA, but is there any equivalent code in PowerShell?

Set openExcelFile = Workbooks.Open(filepath, WriteResPassword:=password)

Many thanks.


Solution

  • I'm not a PowerShell expert, however, the 'password to open' is the 5th param and the 'password to modify' (ie WriteResPassword) is the 6th so I would try with one more [Type]::Missing ie

    $wb = $excel.Workbooks.Open($filepath, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, $password)