PowerShell and gpg on Windows

I am having strange error with my PowerShell script. The first time I run the script shown below I get the following error (in both VSCode and PowerShell directly): Failed to decrypt X:name_of_encrypted_file

SCRIPT snippet:

    Get-ChildItem -Path $encryptedFilesPath -Filter "r$region*.gpg" | ForEach-Object {
        $encryptedFile = $_.FullName
        $decryptedFile = Join-Path -Path $decryptedFilesPath -ChildPath ($_.BaseName -replace '\.gpg

The $passphraseFile has the password enclosed as ‘password$’. I do this because there are special characters in the password.

When I go directly into PowerShell and run the gpg command using the password option instead of the password file with the parameter ‘password$’ (which matches the $passphraseFile), and supplying the name of one of the encrypted files along with the decrypted file name, PowerShell decrypts the file.

I can then run the PowerShell script successfully in VSCode (I am using VSCode until the program behaves properly regularly).

Another development is that when I call the gpg decryption code in VSCode above for two sets of files the first set of files work, but the second set of files gets the “Failed to decrypt X:name_of_encrypted_file error.”

Please note I have tried running the full PowerShell script in PowerShell and get the decrypt error.

Any suggestions what I am doing wrong or why I am getting this behavior?

Thanks, David

As a follow-up the exit code from the gpg call is 2. I saw some posts suggesting to add the – adjust and --homedir C:\Users\xxx\Documents but this didn’t fix the issue.

Hi @David,

does the information in the wiki about command line operations help?

I used the options but they didn’t help. When I switched back to using the -passphrase option instead of passphrasefile the script started working.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.