Windows – Remove PDF passwords with PowerShell (or CMD)

passwordspdfpowershellwindows 10

This is an exemplary duplicate of this Ubuntu question.

I have a number of PDF files that have been sent to me encrypted with a password, which happens to be the same for all PDFs for simplicity. I have no reason to keep the password when I store those files in my permanent archives, nor to type it every time I open those files.

I could use the linked Q&A to build a Docker-based solution to strip the password from the pdf files, or I could ask here if the same is possible with Windows 10.

I have access to choco if I need some packages.

Best Answer

I think I did it.

Step one: use qpdf.

As a choco user

choco install qpdf

Secondly, use the following PowerShell one-liner (I couldn't parallelize it because PS gave me an error). Takes a few seconds per file

$password = [THE_PASSWORD]
Get-ChildItem | ForEach-Object { qpdf --decrypt --replace-input --password=$password $_.Name}

I was extremely confident running the script wild into production because I had a strong backup, and I had to restore it a couple of times.

Everyone around has suggested to make a backup before running live.

qpdf won't do anything if the file is not encrypted.

I will amend the question accordingly, but the script assumes the password is the same for all files

Related Question