Powershell: Define a x509 certificate in a script


the best way to store a certificate in a powershell script is in an byte array. But how to get such a byte array as string from an existing certificate?

First load the certificate from a file

PS D:\> $oMyCert=new-object System.Security.Cryptography.X509Certificates.X509Certificate2("D:\user\myCert.cer")

Then print out the array as string (Output shortend)

PS D:\> [string]::Join(",",$oMyCert.RawData)

Copy the output from the console to define a byte array variable in your script (certificate shortend). To select a text in powershell by lines hold the ALT key pressed while selecting.


In your script create a X509 object by

$oMyCert=new-object System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @(,$aMyCert)

Importend is to submit the byte array argument by the -ArgumentList switch otherwise the X509Certificate2 constructor recognizes each item of the array as argument. This would result in an error:

Cannot find an overload for “X509Certificate2” and the argument count: “1120”


Advertisment to support michlstechblog.info

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.