Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Last active December 6, 2024 17:12
Show Gist options
  • Save steviecoaster/61b855a8360f940ddcb1763c44fb9573 to your computer and use it in GitHub Desktop.
Save steviecoaster/61b855a8360f940ddcb1763c44fb9573 to your computer and use it in GitHub Desktop.
Copy a x509 certificate from one windows store to another
function Copy-CertToStore {
<#
.SYNOPSIS
Copy a certificate from one store location to another
.DESCRIPTION
Long description
.PARAMETER Certificate
The certificate to copy
.PARAMETER Location
The destination location, either LocalMachine or CurrentUser
.PARAMETER Store
The store in which to copy the certificate. Defaults to TrustedPeople
.EXAMPLE
Copy-CertToStore -Certificate $cert
.EXAMPLE
CopyCertToStore -Certificate (gci Cert:\LocalMachine\My\12342141351534534254325) -Location CurrentUser -Store My
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[Alias('Cert')]
[System.Security.Cryptography.X509Certificates.X509Certificate2]
$Certificate,
[Parameter()]
[System.Security.Cryptography.X509Certificates.StoreLocation]
$Location = [System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine,
[Parameter()]
[System.Security.Cryptography.X509Certificates.StoreName]
$Store =[System.Security.Cryptography.X509Certificates.StoreName]::TrustedPeople
)
$trustedCertStore = [System.Security.Cryptography.X509Certificates.X509Store]::new($Store, $location)
try {
$trustedCertStore.Open('ReadWrite')
$trustedCertStore.Add($Certificate)
}
finally {
$trustedCertStore.Close()
$trustedCertStore.Dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment