How to send OpenPGP encrypted email using Windows Powershell

This post assumes you have a existing SMTP mail server and a OpenPGP keyserver and you are trying to send encrypted emails using powershell scripts.

# For single recipient
function PgpEncrypt ($clearText, $emailTo, $emailFrom){
    $clearText | gpg --passphrase $passphrase --batch -se -r $emailTo -u $emailFrom -a -o - | Out-String

$encryptedText = PgpEncrypt $clearText $emailTo $emailFrom

$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = $emailSubject
$emailMessage.IsBodyHtml = $false
$emailMessage.Body = $encryptedText

$SMTPClient = New-Object System.Net.Mail.SmtpClient($emailSmtpServer)
#$SMTPClient.EnableSsl = $true
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )
# For multiple recipients delimited by comma: ','
function PgpEncrypt ($clearText, $emailTo, $emailFrom) {
    $passphrase = "--passphrase '*****'"
    $sender = "-u $emailFrom"
    $options = "--batch -se -a -o -"
    $clearText = "hello world"

    $receipients = ""
    $arrayR = $emailTo.split(',')
    foreach ($r in $arrayR){
        $receipients = $receipients + "-r $r "

    $expression = "'$clearText' | gpg $options" + $passphrase + "$recipients $emailFrom"
    Invoke-Expression $expression
# Batch file to search for a public key from keyserver and set trust for it:
gpg --seach-keys --keyserver ldap:// %1
gpg --edit-key %1 trust

When the build script is not appreciated

Reading the article The F5 Key Is Not a Build Process┬áreminds me of a time a couple of years ago when I was in the process of handing over a project’s source codes to the Operations and Support Team.

When asked to verify that the source codes were indeed the correct ones, I ran a Ant build script that built the web services, client plugins and the dependency libraries from the source codes before deploying it onto the staging servers and a test client.

To cut the long story short, the Operations and Support Team told me that the build script looks nice and cool but they cannot accept it. They insisted that I helped to configure their IDEs for the projects because that is the only way they build their projects, on the IDE…