0 0

Install Certificate in CurrentUser

Import:

function Import-P12Certificate {
 
param([String]$certPath,[String]$certRootStore = “CurrentUser”,[String]$certStore = “My”,$pfxPass = $null)
 $pfx = new-object System.Security.Cryptography.X509Certificates.X509Certificate2
 
 if ($pfxPass -eq $null) {$pfxPass = read-host “Enter the pfx password” -assecurestring}
 
 $pfx.import($certPath,$pfxPass,“Exportable,PersistKeySet”)
 
 $store = new-object System.Security.Cryptography.X509Certificates.X509Store($certStore,$certRootStore)
 $store.open(“MaxAllowed”)
 $store.add($pfx)
 $store.close()
}

Import-P12Certificate \\SERVER\share\CERTIFICATE.pfx CurrentUser My P@$$W0RD

$Thumbprint = "XXXXXX"
$Certificate = "C:\Cert\CERT.pfx"
$CertPass = ConvertTo-SecureString -String "P@$$W0RD" -Force -AsPlainText
$Checkcert = Get-ChildItem -Path cert:\CurrentUser\My | Where-Object {$_.Thumbprint -eq $Thumbprint}

if ($Checkcert -eq $null) { Get-ChildItem -Path $Certificate | Import-PfxCertificate -Password $CertPass -CertStoreLocation Cert:\CurrentUser\My -Exportable
    if ($importedCert -ne $null) {
        Write-Host "Certificaat succesvol geïnstalleerd."
    } else {
        Write-Host "Er is een probleem opgetreden bij het installeren van het certificaat."
    }
} else {
    Write-Host "Het gewenste certificaat is al aanwezig."
}

# Thumbprint:
$thumbprintToCheck = "123thumbprint"
# Path:
$pfxPath = "S:\Cert\CERT.pfx"
# Pass:
$pfxPassword = ConvertTo-SecureString -String "P@$$w0rd" -AsPlainText -Force

$existingCertificate = Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object {$_.Thumbprint -eq $thumbprintToCheck}
if (-not $existingCertificate) {
    Import-PfxCertificate -FilePath $pfxPath -CertStoreLocation Cert:\CurrentUser\My -Password $pfxPassword
} else {
    Write-Host "Certificate already exists in the store."
}

Extract Certificate Thumbprint:

Get-PfxCertificate -FilePath "C:\Certificate.p12" | Select-Object Thumbprint