Skip to content

Instantly share code, notes, and snippets.

@MVKozlov
Created April 12, 2022 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MVKozlov/78173045299023310db8a44441107e27 to your computer and use it in GitHub Desktop.
Save MVKozlov/78173045299023310db8a44441107e27 to your computer and use it in GitHub Desktop.
Import self contained certificate into certificate store
#Cert content:
# PSv7 $cert = [Convert]::ToBase64String((Get-Content D:\Path\To\Certificate.pfx/cer -AsByteStream), 'InsertLineBreaks')
# PSv5.1 $cert = [Convert]::ToBase64String((Get-Content D:\Path\To\Certificate.pfx/cer -Encoding Byte), 'InsertLineBreaks')
$Cert = @"
MIILYgIBAzCCCx4GCSqGSIb3DQEHAaCCCw8EggsLMIILBzCCBjAGCSqGSIb3DQEHAaCCBiEEggYd
.....
/+1y1lZqkQICB9A=
"@
#Cdert location: #LocalMachine, CurrentUser
$location = [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser
#Storename:
#ls Cert:\CurrentUser\
#ls Cert:\LocalMachine\
$storeName = 'TrustedPublisher'
$store = [System.Security.Cryptography.X509Certificates.X509Store]::new($storeName, $location)
$store.Open('ReadWrite')
[byte[]]$content = [Convert]::FromBase64String($cert)
$certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($content)
#$certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($content, $password)
$store.Add($certificate)
$store.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment