Mass remove known password from Word files

A post on a forum I visit frequently mobilized me to create a script to remove a password (the one required to open a file) from any number of Word files. Because nobody would want to open a 100 files and manually save them...
Of course, you have to know it. The following script doesn't do any cracking.

For starter - if you're not familiar with PoSh - you can visit sites like:
- TechNet Script Center
- or

Anyway - here's the code, which I'll explain below:  
$read_path = Read-Host("Source Path:")
                      $write_path = Read-Host("Destination Path (with trailing backslash!):")
                      $passwd = Read-Host("Type in the password:")
                      $WordObj = New-Object -ComObject Word.Application
                      foreach ($file in $count=Get-ChildItem $read_path -Filter *.doc)
                          $WordObj.Visible = $false
                          $WordDoc = $WordObj.Documents.Open($file.FullName, $null, $false, $null, $passwd, $passwd)
                          Write-Host("Finished: "+$counter+" of "+$count.Length)

Open in new window

As you can see it's pretty straightforward and simple
- lines 1 to 3 declare three variables:
 * Source Path
 * Destination Path
 * Password
- line 4 shows you the variable storing the password
- line 5 declares new COM object of the Word.Application type. It'll be used to process Word files. It's important to declare it here, not in the loop, as object creation requires some processing power.
- line 6 means something like that: "for each item called $file, matching the Get-ChildItem query, do the following...". The GCI query has been assigned to a variable so we can display item counter later.
- line 8 - this Property controls the visibility of the Word window.
- line 9 - we use the Documents.Open method with a couple of parameters:
 * - full name of a file (base name + extension)
 * - we don't care about document conversion window
 * - we don't want the document to become Read Only
 * - a password to open the file
 * - a password to open the file - probably not required
- line 10 - activation of the object
- line 11 - setting the document password to NULL
- line 12 - writing the document to the specified path. Both variables are being joined to form a full path (it doesn't check for the backslash to separate a path from a file name, so you must provide it in the destination path)  
- line 13 - closing of a file
- line 14 - message that uses the counter to let us know about the progress
- line 15 - counter incrementation
- line 17 - quits the Word application

As you can see is quite easy script. It isn't foolproof, the destination folder must exist too.
The main reason though is the way to remove the password in a batch. And I think it works great doing this.

Comments (4)

Arman KhodabandeFreelance Developer

An interesting article . . .
But what's the language that this code is written for?
You should mention that the "Script is in what language?" and "how to be used!".
The commands don't seem familiar to me.
please edit it.


Hello kpax.
As seen in the Zone tag it's written in the PowerShell. I'll edit the article but essentially - you'll have to learn the basics from the MSDN.
Arman KhodabandeFreelance Developer

I understood it after posting the comment!
Thanks anyway.
Thanks for this, which is really useful, but:

When I run your code I get:

Exception setting "Open": Cannot convert the "xxx!" value of type "string" to type "Object".
At ...:9

where my password is "xxx!".

It says the same if I give "xxx!" as a literal parameter to Open

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.