PowerShell has a cert: drive that lets you explore all certificates installed on your system. Once you locate a certificate, you can then export it to a file with just a couple of lines of code – provided that the certificate allows itself to be exported.
Dir cert:\ -recurse
Try this to grab the first certificate that has your username in its subject:
@(dir cert: -Recurse |
Where-Object { $_.subject -like "*$env:username*" })[0]
Where-Object { $_.subject -like "*$env:username*" })[0]
Store the certificate in a variable and then view all of its properties to view certificate details:
$cert = @(dir cert: -Recurse |
Where-Object { $_.subject -like "*$env:username*" })[0]
$cert | Format-List *
Where-Object { $_.subject -like "*$env:username*" })[0]
$cert | Format-List *
You should call the export() method, which gets you a byte array, to export the certificate. Next, use .NET to write the byte array to disk:
$bytes = $cert.Export("Cert")
[system.IO.file]::WriteAllBytes("$home\mycert.cer", $bytes)
Dir $home\*.cer
[system.IO.file]::WriteAllBytes("$home\mycert.cer", $bytes)
Dir $home\*.cer