meme Log

August 20, 2013

Microsoft Word Encryption Password on Multiple Files using PowerShell

Filed under: Tech — Lynn Fikstad @ 10:01

Placing Microsoft Word Encryption Password on Multiple Files

This explains how to create a script to put passwords on every word file in a directory.

Run PowerShell ISE as administrator (From Windows 8: search apps for PowerShell ISO, RClick, Run as Administrator)

In the command window run this command:

PS C:\windows\system32> Set-ExecutionPolicy RemoteSigned

The bold PowerShell code at the end of this blog can be copied and pasted into the “untitled section” of the ISE or a PowerShell script using notepad and saved as [filename].ps1

Run the script (either from the ISE, or referencing the script file in the command window (be sure to enter the full path))

The script presents the following prompts:

Enter source path: (Enter the path containing a group of docx files; assumes some or all of these files have the same password)

Enter the common password of some of all the source files: (Enter the password, if any; otherwise just hit return)

Enter destination path: (Enter the destination path where the source files are copies…be sure to end with a trailing backslash)

Enter the new password for the destination files: (Enter the password you want to set for all the files)

The files are copied from the source to the destination and all destination files are given a password.

Before closing PowerShell enter the following command:

PS C:\windows\system32> Set-ExecutionPolicy RemoteSigned

Note the 24 WDSaveFormat parameter in the $WordSaveAs method. This writes the file in strict XML for the reasons given below:

From Wikipedia in June 2013:

Starting with Microsoft Office 2007, the Office Open XML file formats have become the default[3] file format of Microsoft Office.[4][5] However, due to the changes introduced in the Office Open XML standard, Office 2007 is not entirely in compliance with ISO/IEC 29500:2008.[38][39][40][41] Microsoft Office 2010 includes support for the ISO/IEC 29500:2008 compliant version of Office Open XML,[39] but it can only save documents conforming to the transitional schemas of the specification, not the strict schemas.[42][43] Note that the intent of the ISO/IEC is to allow the removal of the transitional variant from the ISO/IEC 29500 standard.[43]

I highlighted the section of Wikipedia that made me decide to write my documents out in strict XML. I change the default save format to strict open XML docx.

PowerShell script to copy:

$read_path = Read-Host(“Enter source path”)

$passwd = Read-Host(“Enter the common password of some or all the source files”)

$write_path = Read-Host(“Destination Path (INCLUDE TRAILING BACKSLASH)”)

$passwd2=Read-Host(“Enter the new password for the destination files”)

$counter=1

$WordObj = New-Object -ComObject Word.Application

foreach ($file in $count=Get-ChildItem $read_path -Filter *.docx)

{

$WordObj.Visible = $false

$WordDoc = $WordObj.Documents.Open($file.FullName, $null, $false, $null, $passwd, $passwd)

$WordDoc.Activate()

$WordDoc.Password=$passwd

$WordDoc.SaveAs($write_path+$file.name,24)

$WordDoc.Close()

Write-Host(“Finished: “+$counter+” of “+$count.Length)

$counter++

}

$WordObj.Application.Quit()

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: