Browse All Articles
> 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 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:")
$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)
As you can see it's pretty straightforward and simple
- lines 1 to 3
declare three variables:
* Source Path
* Destination Path
- 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.