Someone asked me whether I could pull the email address of a user from an inserted SmartCard. I knew I could dump the information in Windows 7 using the CertUtil command and wanted to experiment with parsing information with PowerShell. The code can be very compact:
$certInfo = certutil -scinfo -silent | Select-String -Pattern "RFC822" | Get-Unique
$UPN = $certInfo.ToString().Replace("SubjectAltName: RFC822 Name=","").trim()
$UPN
In the first line the command output from certutil -scinfo -silent is piped to Select-String. The pattern pulls out lines with “RFC822”. Since we all know that the RFC822 Name is the email address we are pulling out lines with that info from the output. Then we select unique results only. Happily that gives us one line with the email address together with the other information on the line. In line 2 we are using Replace to delete the extra text, and Trim to remove the leading and trailing spaces. All in all, a pretty neat demonstration of the power of PowerShell.