A little background first.
I\'ve been tasked with encrypting files with a Powershell script using GPG (gnupg.org). The specific exe I\'m calling is simply gpg.exe. I
Stobor's answer is great. I am adding to his answer because I needed to perform additional actions if the exe had an error.
You can also store the output of the exe into a variable like this. Then you can do error handling based on the result of the exe.
$out = $gpgLocation --import "key.txt" 2>&1
if($out -is [System.Management.Automation.ErrorRecord]) {
# email or some other action here
Send-MailMessage -to me@example.com -subject "Error in gpg " -body "Error:`n$out" -from error@example.com -smtpserver smtp.example.com
}
$out | out-file gpgout.txt