Skip to content

Instantly share code, notes, and snippets.

@ignas-sakalauskas
Created January 30, 2019 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ignas-sakalauskas/28dd064019f7d0797d90d5d7e74343ea to your computer and use it in GitHub Desktop.
Save ignas-sakalauskas/28dd064019f7d0797d90d5d7e74343ea to your computer and use it in GitHub Desktop.
# Based on # https://social.technet.microsoft.com/wiki/contents/articles/28753.powershell-trick-copy-certificates-from-one-store-to-another.aspx
$SourceStoreScope = 'LocalMachine'
$SourceStorename = 'My'
$CertificateSubjectPattern = '*mytestdomain'
Write-Host 'Looking for certificates with subject' $CertificateSubjectPattern 'in' $SourceStoreScope'\'$SourceStorename
$SourceStore = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $SourceStorename, $SourceStoreScope
$SourceStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
$cert = $SourceStore.Certificates | Where-Object -FilterScript {
$_.subject -like $CertificateSubjectPattern
}
Write-Host 'Certificate(s) found:'
Write-Host $cert.subject
$DestStoreScope = 'LocalMachine'
$DestStoreName = 'Root'
$DestStore = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $DestStoreName, $DestStoreScope
$DestStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$DestStore.Add($cert)
$SourceStore.Close()
$DestStore.Close()
Write-Host 'Certificate(s) added to:' $DestStoreScope'\'$DestStoreName
# https://ignas.me/tech/self-signed-multi-domain-ssl-certificate/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment