Skip to content

Instantly share code, notes, and snippets.

@jabez007
Created February 12, 2018 15:28
Show Gist options
  • Save jabez007/ca6b7ac9e925fed50ed95ef81f885747 to your computer and use it in GitHub Desktop.
Save jabez007/ca6b7ac9e925fed50ed95ef81f885747 to your computer and use it in GitHub Desktop.
A PowerShell script to mass export client certificates from a server
param(
[Parameter(Mandatory = $true)]
[String[]]$Thumbprints,
[Parameter(Mandatory = $true)]
[String]$Password
)
#### Usage ####
# .\Export-ClientCertificates.ps1 -Thumbprints "array","of","thumbprints","and","subject","common","names" -Password "PrivateKeyPassword"
####
$passwd = ConvertTo-SecureString -String $Password -Force -AsPlainText
$certPath = "Cert:\LocalMachine\My"
ForEach ($certprint in $Thumbprints) {
$cert = Get-ChildItem $certPath -Recurse | ? { $_ -is [System.Security.Cryptography.X509Certificates.X509Certificate2] } | ? {$_.Thumbprint -eq $certprint.Replace(" ","").ToUpper()}
if ($cert -eq $null) {
$cert = Get-ChildItem $certPath -Recurse | ? { $_ -is [System.Security.Cryptography.X509Certificates.X509Certificate2] } | ? {$_.SubjectName.Name.ToUpper() -like "*CN=$certprint,*".ToUpper()}
}
if (-Not ($cert -eq $null)) {
$certSubjectName = $cert.SubjectName.Name
#Write-Host "Found Subject Name: $certSubjectName"
if ($cert.SubjectName.Name -match "CN=(?<commonName>[^,]*)") {
$certCommonName = $Matches['commonName']
#Write-Host "Found Common Name: $certCommonName"
$certThumbprint = $cert.Thumbprint
#Write-Host "Found Thumbprint: $certThumbprint"
$exportFile = "$certCommonName ($certThumbprint).pfx"
#Write-Host "Writing to $exportFile"
$cert | Export-PfxCertificate -FilePath $exportFile -Password $passwd -ChainOption BuildChain
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment