Search code examples
powershellgnupg

GnuPG with Powershell and wildcard for encrypting


I am trying to encrypt all TXT files in a specific folder using the wildcard *.txt. When I run the PowerShell code in Admin rights I get this error message.

set-alias sz "$env:C:\Program Files (x86)\GNU\GnuPG\gpg.exe"  
sz --encrypt --recipient "AABB (2048-bit)" "C:\Users\AA\Desktop\AABB_Test\*.txt" 

Error:

**sz : gpg: can't open `C:\Users\AA\Desktop\AABB_Test\*.txt': No such file or directory**

But If I choose just one of the txt file:

set-alias sz "$env:C:\Program Files (x86)\GNU\GnuPG\gpg.exe"  
sz --encrypt --recipient "AABB (2048-bit)" "C:\Users\AA\Desktop\AABB_Test\1_test_file.txt" 

It works fine.


Solution

  • The code below should produce the result you desire.

    Set-Alias sz "$Env:SystemDrive\Program Files (x86)\GNU\GnuPG\gpg.exe"  
    $dirWithFiles = "$Env:SystemDrive\Users\AA\Desktop\AABB_Test"
    
    Get-ChildItem -Path $dirWithFiles | ForEach-Object {
        sz --encrypt --recipient "AABB (2048-bit)" "$($_.FullName)"
    }