<

Mass remove known password from Word files

Published on
18,127 Points
12,027 Views
1 Endorsement
Last Modified:
Approved
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:
- MSDN
- TechNet Script Center
- or PowerShell.com

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:")
$counter=1
$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)
    $WordDoc.Activate()
    $WordDoc.Password=$null
    $WordDoc.SaveAs($write_path+$file.Name)
    $WordDoc.Close()
    Write-Host("Finished: "+$counter+" of "+$count.Length)
    $counter++
}
$WordObj.Application.Quit()

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.
1
Comment
Author:marek1712
  • 2
4 Comments
LVL 10

Expert Comment

by:Arman Khodabande
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.
Thanks
0
LVL 11

Author Comment

by:marek1712
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.
0
LVL 10

Expert Comment

by:Arman Khodabande
Ooops!
I understood it after posting the comment!
Thanks anyway.
0

Expert Comment

by:Pete Whitelock
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
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Join & Write a Comment

Loops Section Overview
Did you know PowerShell can save you time with SaaS platforms? Simply leverage RESTfulAPIs to build your own PowerShell modules. These will kill repetitive tickets and tabs, using the command Invoke-RestMethod. Tune into this webinar to learn how…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month