Skip to content

Instantly share code, notes, and snippets.

@acast15
Last active November 19, 2023 02:23
Show Gist options
  • Save acast15/1d5bf9c784cb5c4576bb90f23f9c92a2 to your computer and use it in GitHub Desktop.
Save acast15/1d5bf9c784cb5c4576bb90f23f9c92a2 to your computer and use it in GitHub Desktop.
PowerShell function to trust a certificate
function Trust-Certificate {
# Prompt the user to input the certificate's path
$certPath = Read-Host "Enter path to certificate"
# Create a new X.509 certificate object
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
try {
# Import the certificate from the specified path
$cert.Import($certPath)
# Create a new X.509 store for Trusted Root Certification Authorities on the Local Machine
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("Root", "LocalMachine")
try {
# Open the store, add the certificate to the store, and close the store
$store.Open("ReadWrite")
$store.Add($cert)
}
finally {
$store.Close()
}
Write-Host "Certificate imported successfully."
}
catch {
Write-Host "Error importing the certificate: $_"
}
}
# Uncomment the below if you wish to immediately call the function
# Trust-Certificate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment