Digitally Sign an Office Document in PowerShell

#How to run the PowerShell scripts

#SignLib.dll must be placed on a folder and the path must be added on the .ps1 script
#$DllPath = 'd:\SignLib.dll'

#run the script file from command line, as below:
#powershell -executionPolicy bypass -file officeSign.ps1 "d:\test.docx" "d:\test[signed].docx"

#Digitally sign an Office file using a PFX certificate.
if ($args.Length -eq 0)
echo "Usage: office.ps1 <unsigned file> <signed file>"
$DllPath = 'd:\SignLib.dll'

$pfxFilePath = "d:\pfxcertificate.pfx"
$pFXFilePassword = "123456"

#digitally sign an Office document
$sign = new-object -typeName SignLib.OfficeSignature("")
$sign.DigitalSignatureCertificate = [SignLib.Certificates.DigitalCertificate]::LoadCertificate($pfxFilePath, $pFXFilePassword)

#Load the certificate from Microsoft Certificate Store (smart card certificates).
#$sign.DigitalSignatureCertificate = [SignLib.Certificates.DigitalCertificate]::LoadCertificate($false, "","","");

echo "Perform the digital signature..."

$sign.ApplyDigitalSignature($args[0], $args[1])

See also: