Exporting Certificate

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]

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 *

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

Twitter This Tip! ReTweet this Tip!


Posted Oct 19 2009, 08:00 AM by ps1

Comments

Twitter Trackbacks for Exporting Certificate - Power Tips - PowerShell.com [powershell.com] on Topsy.com wrote Twitter Trackbacks for Exporting Certificate - Power Tips - PowerShell.com [powershell.com] on Topsy.com
on 10-19-2009 11:48 AM

Pingback from  Twitter Trackbacks for                 Exporting Certificate - Power Tips - PowerShell.com         [powershell.com]        on Topsy.com

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.