Adds a password (%reTG54w) to a Microsoft Excel spreadsheet.
PowerShell
$comments = @'
Script name: Set-ExcelPassword.ps1
Created on: Friday, June 29, 2007
Author: Kent Finkle
Purpose: How can I use Windows Powershell to
Password-Protect an Excel Spreadsheet?
'@
#-----------------------------------------------------
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
#-----------------------------------------------------
$xlNormal = -4143
$xl = new-object -comobject excel.application
$xl.Visible = $True
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
$ws.Cells.Item(1, 1).Value() = get-date
$a = $wb.SaveAs("C:\Scripts\Test.xls",$xlNormal,"%reTG54w")
$a = $xl.Quit()
$a = Release-Ref($ws)
$a = Release-Ref($wb)
$a = Release-Ref($xl)
Windows Server 2008 R2 | No |
Windows Server 2008 | No |
Windows Server 2003 | No |
Windows 7 | No |
Windows Vista | No |
Windows XP | No |
Windows 2000 | No |
This script is tested on these platforms by the author. It is likely to work on other platforms as well. If you try it and find that it works on another platform, please add a note to the script discussion to let others know.