Skip navigation
screenshot of PowerShell script

Use PowerShell To Find Password-protected Word Documents

PowerShell can come in handy when you need to find an elusive file on your network. Here’s an example PowerShell script that searches for password-protected Word documents.

Although it’s a bit of a long story, I recently needed to locate a particular file on my network. The file in question was several years old, and I couldn’t remember the path or the filename. The file folder hierarchy that I use will normally make it easy to find old files, but this file was elusive. Worse yet, because the file was a password-protected Word document, performing a search based on the file’s contents was not an option.

Ultimately, I identified how to use the password protection to my advantage.

There are many thousands of Word documents stored on my network, but only about half a dozen of them use password protection. The case being such, I got the idea that if I could search my network for password-protected Word documents, I could narrow the search and find the file in question.

As you have no doubt guessed, there is no built-in search function that can filter search results based on password-protected Word documents. However, you can use PowerShell to open each document, one at a time, and flag any of them that use password protection.

PowerShell Script: Searching for Password-protected Word Documents

I searched the web to see if anyone had already created a script that would meet my requirements. I found something on TechNet that didn’t exactly fit the bill. Even so, I used that script as a starting point, then adapted the script to my needs.

Here is what my script looks like:


$Files = Get-ChildItem "C:\pshell\*.doc*"

ForEach ($File in $Files){

Write-Host $File.Name

$MSWord = New-Object -COM "Word.Application"

Try {

  $MyDoc = $MSWord.Documents.Open($File.FullName, $null, $null, $null, "")


Catch {

  Write-Host "$File is password-protected!" -ForegroundColor "Yellow"




The script starts by using the Get-ChildItem cmdlet to retrieve a list of all the .doc and .docx files in a particular folder. In this case, I used C:\pshell for demonstration purposes. However, when I ran this script in my production environment, I used a different path and also the -Recurse parameter so that PowerShell would check the various subfolders. All the results were then written to a variable called $Files. This variable essentially contains a list of Word documents.

The next thing that my script does is set up a loop that allows it to examine each document ($File) in the list of documents ($Files) individually. Each time the loop looks at a new file, it outputs the name of the file and creates a Microsoft Word application object, which is assigned to the variable $MSWord.

Initially, I tried to create the Word.Application object outside of the loop but doing so caused Windows to open a copy of Word for every document that gets checked. That isn’t really a big deal for a small folder containing a few documents, but it causes a serious problem when you examine many documents at once. Incidentally, even the script I created has its limits, so it’s best to limit the script to checking a couple hundred documents at a time if possible.

After the new-object declaration, there is a Try statement and a Catch statement. In PowerShell, Try and Catch are used for error trapping. PowerShell attempts to run the command that is displayed within the Try statement. If the command succeeds, then it is allowed to run as normal. If the command fails, then the command listed within the Catch statement runs instead. In this case, the Try command is attempting to open the Word document that the loop is currently processing. If the document opens, then the script does not do anything (although the script’s third line of code lists the file name as a confirmation that the script is doing something).

If the script encounters a password-protected Word document, the script will open Word and prompt you to enter a password. Assuming you click Cancel, the PowerShell script will be unable to open the file. This will cause the Catch statement to execute, thereby causing PowerShell to tell you that the file is a password-protected Word document. You can see the script’s output in Figure 1.

Figure 1

Brien Poseyscreenshot of PowerShell script

PowerShell can identify which Word documents have password protection on.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.