Password-Protect an Excel Spreadsheet

July 04, 2011

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
Verified on the following platforms
 
Windows Server 2008 R2No
Windows Server 2008No
Windows Server 2003No
Windows 7No
Windows VistaNo
Windows XPNo
Windows 2000No
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.

Related Posts

Next Article
« Prev Post
Previous Article
Next Post »

No comments