Search code examples
powershellauthenticode

Extracting certificate issuer and subject common name using PowerShell


I want to use a PowerShell one-liner to extract the signing certificate issuer and subject's common names, but I am not able to get it working. Currently, I have:

Get-AuthenticodeSignature .\test.exe | ForEach-Object { ($_.SignerCertificate.Thumbprint, ($_.SignerCertificate.Issuer -split ',' | Where-Object { $_ -like 'CN=*' }), ($_.SignerCertificate.Subject -split ',' | Where-Object { $_ -like 'CN=*' }), $_.SignerCertificate.NotAfter) -join ',' }

This code does not work for cases when the common name itself contains a comma. For example, "MyCompany, Inc."


Solution

  • How about this?

    Get-AuthenticodeSignature .\test.exe | 
    ForEach-Object { 
    ((($_.SignerCertificate.Issuer -split ', O=')[0] -split '=')[1],  
    (($_.SignerCertificate.Subject -split ', O=')[0] -split '=')[1] 
    ) -join ', '
    }
    

    Of course you can remove the line breaks ¯\_(ツ)_/¯