How to Efficiently Find and Remove Certificates by Thumbprint Using PowerShell

Managing system certificates is a critical task for IT professionals and Managed Service Providers (MSPs). Certificates are the backbone of secure communication, and ensuring they are correctly managed is essential for maintaining the integrity and security of IT systems. This comprehensive PowerShell script is designed to streamline the process of finding and removing system certificates by thumbprint, addressing a common need in the IT world.

Background

Certificates authenticate and encrypt data to ensure secure communication across networks. Over time, certificates may become invalid, expired, or compromised, necessitating their removal. IT professionals and MSPs often need a reliable method to locate and manage these certificates efficiently. This PowerShell script provides a robust solution for finding and managing certificates by thumbprint, ensuring that systems remain secure and compliant.

The Script

#Requires -Version 5.1

<#
.SYNOPSIS
    Returns a list of system certificates that have the specified thumbprints or Removes the certificates with matching thumbprints.
.DESCRIPTION
    Returns a list of system certificates that have the specified thumbprints or Removes the certificates with matching thumbprints.

.EXAMPLE
    (No Parameters)
    ## EXAMPLE OUTPUT WITHOUT PARAMS ##
    Does nothing.

.EXAMPLE
PARAMETER: -Thumbprint "AE68D0ADAD2345B48E507320B695D386080E5B25", "BE68D0ADAA2145B48E507320B695D386080E5B25"
    Returns the found thumbprints matching the input.
    ## EXAMPLE OUTPUT WITH Thumbprint ##
    [Alert] Found certificates:
    AE68D0ADAD2345B48E507320B695D386080E5B25
    BE68D0ADAA2145B48E507320B695D386080E5B25
    [Alert] Certificates found

.EXAMPLE
PARAMETER: -Thumbprint "BE68D0ADAA2145B48E507320B695D386080E5B25" -RemoveMatchingCertificates
    Returns the found thumbprints matching the input and Removes the certificates.
    ## EXAMPLE OUTPUT WITH RemoveMatchingCertificates ##
    [Alert] Found certificates:
    BE68D0ADAA2145B48E507320B695D386080E5B25
    [Info] Removing certificates
    [Info] Removing certificate with thumbprint: BE68D0ADAA2145B48E507320B695D386080E5B25
    [Info] Removed certificate with thumbprint: BE68D0ADAA2145B48E507320B695D386080E5B25
    [Alert] Certificates found
.EXAMPLE
PARAMETER: -Thumbprint "BE68D0ADAA2145B48E507320B695D386080E5B25" -CustomField "Thumbprints"
    Returns the found thumbprints matching the input and Removes the certificates.
    ## EXAMPLE OUTPUT WITH RemoveMatchingCertificates ##
    [Alert] Found certificates:
    BE68D0ADAA2145B48E507320B695D386080E5B25
    [Info] Saving thumbprints to custom field: Thumbprints
    [Alert] Certificates found
.OUTPUTS
    None
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Release Notes: Initial Release
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
.COMPONENT
    Generic-Security
#>

[CmdletBinding()]
param (
    [string[]]$Thumbprint,
    [switch]$RemoveMatchingCertificates,
    [string]$CertRevokeList,
    [string]$GetCrlFromCustomField,
    [string]$CustomField
)

begin {
    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }
    function Set-NinjaProperty {
        [CmdletBinding()]
        Param(
            [Parameter(Mandatory = $True)]
            [String]$Name,
            [Parameter()]
            [String]$Type,
            [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
            $Value,
            [Parameter()]
            [String]$DocumentName
        )

        # If we're requested to set the field value for a Ninja document we'll specify it here.
        $DocumentationParams = @{}
        if ($DocumentName) { $DocumentationParams["DocumentName"] = $DocumentName }

        # This is a list of valid fields we can set. If no type is given we'll assume the input doesn't have to be changed in any way.
        $ValidFields = "Attachment", "Checkbox", "Date", "Date or Date Time", "Decimal", "Dropdown", "Email", "Integer", "IP Address", "MultiLine", "MultiSelect", "Phone", "Secure", "Text", "Time", "URL"
        if ($Type -and $ValidFields -notcontains $Type) { Write-Warning "$Type is an invalid type! Please check here for valid types. https://ninjarmm.zendesk.com/hc/en-us/articles/16973443979789-Command-Line-Interface-CLI-Supported-Fields-and-Functionality" }

        # The below field requires additional information in order to set
        $NeedsOptions = "Dropdown"
        if ($DocumentName) {
            if ($NeedsOptions -contains $Type) {
                # We'll redirect the error output to the success stream to make it easier to error out if nothing was found or something else went wrong.
                $NinjaPropertyOptions = Ninja-Property-Docs-Options -AttributeName $Name @DocumentationParams 2>&1
            }
        }
        else {
            if ($NeedsOptions -contains $Type) {
                $NinjaPropertyOptions = Ninja-Property-Options -Name $Name 2>&1
            }
        }

        # If we received some sort of error it should have an exception property and we'll exit the function with that error information.
        if ($NinjaPropertyOptions.Exception) { throw $NinjaPropertyOptions }

        # The below type's require values not typically given in order to be set. The below code will convert whatever we're given into a format ninjarmm-cli supports.
        switch ($Type) {
            "Checkbox" {
                # While it's highly likely we were given a value like "True" or a boolean datatype it's better to be safe than sorry.
                $NinjaValue = [System.Convert]::ToBoolean($Value)
            }
            "Date or Date Time" {
                # Ninjarmm-cli is expecting the time to be representing as a Unix Epoch string. So we'll convert what we were given into that format.
                $Date = (Get-Date $Value).ToUniversalTime()
                $TimeSpan = New-TimeSpan (Get-Date "1970-01-01 00:00:00") $Date
                $NinjaValue = $TimeSpan.TotalSeconds
            }
            "Dropdown" {
                # Ninjarmm-cli is expecting the guid of the option we're trying to select. So we'll match up the value we were given with a guid.
                $Options = $NinjaPropertyOptions -replace '=', ',' | ConvertFrom-Csv -Header "GUID", "Name"
                $Selection = $Options | Where-Object { $_.Name -eq $Value } | Select-Object -ExpandProperty GUID

                if (-not $Selection) {
                    throw "Value is not present in dropdown"
                }

                $NinjaValue = $Selection
            }
            default {
                # All the other types shouldn't require additional work on the input.
                $NinjaValue = $Value
            }
        }

        # We'll need to set the field differently depending on if its a field in a Ninja Document or not.
        if ($DocumentName) {
            $CustomField = Ninja-Property-Docs-Set -AttributeName $Name -AttributeValue $NinjaValue @DocumentationParams 2>&1
        }
        else {
            $CustomField = Ninja-Property-Set -Name $Name -Value $NinjaValue 2>&1
        }

        if ($CustomField.Exception) {
            throw $CustomField
        }
    }
    # This function is to make it easier to parse Ninja Custom Fields.
    function Get-NinjaProperty {
        [CmdletBinding()]
        Param(
            [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
            [String]$Name,
            [Parameter()]
            [String]$Type,
            [Parameter()]
            [String]$DocumentName
        )
    
        if ($PSVersionTable.PSVersion.Major -lt 3) {
            throw "PowerShell 3.0 or higher is required to retrieve data from custom fields. https://ninjarmm.zendesk.com/hc/en-us/articles/4405408656013"
        }
    
        # If we're requested to get the field value from a Ninja document we'll specify it here.
        $DocumentationParams = @{}
        if ($DocumentName) { $DocumentationParams["DocumentName"] = $DocumentName }
    
        # These two types require more information to parse.
        $NeedsOptions = "DropDown", "MultiSelect"
    
        # Grabbing document values requires a slightly different command.
        if ($DocumentName) {
            # Secure fields are only readable when they're a device custom field
            if ($Type -Like "Secure") { throw "$Type is an invalid type! Please check here for valid types. https://ninjarmm.zendesk.com/hc/en-us/articles/16973443979789-Command-Line-Interface-CLI-Supported-Fields-and-Functionality" }
    
            # We'll redirect the error output to the success stream to make it easier to error out if nothing was found or something else went wrong.
            Write-Host "Retrieving value from Ninja Document..."
            $NinjaPropertyValue = Ninja-Property-Docs-Get -AttributeName $Name @DocumentationParams 2>&1
    
            # Certain fields require more information to parse.
            if ($NeedsOptions -contains $Type) {
                $NinjaPropertyOptions = Ninja-Property-Docs-Options -AttributeName $Name @DocumentationParams 2>&1
            }
        }
        else {
            # We'll redirect error output to the success stream to make it easier to error out if nothing was found or something else went wrong.
            $NinjaPropertyValue = Ninja-Property-Get -Name $Name 2>&1
    
            # Certain fields require more information to parse.
            if ($NeedsOptions -contains $Type) {
                $NinjaPropertyOptions = Ninja-Property-Options -Name $Name 2>&1
            }
        }
    
        # If we received some sort of error it should have an exception property and we'll exit the function with that error information.
        if ($NinjaPropertyValue.Exception) { throw $NinjaPropertyValue }
        if ($NinjaPropertyOptions.Exception) { throw $NinjaPropertyOptions }
    
        # This switch will compare the type given with the quoted string. If it matches, it'll parse it further; otherwise, the default option will be selected.
        switch ($Type) {
            "Attachment" {
                # Attachments come in a JSON format this will convert it into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Checkbox" {
                # Checkbox's come in as a string representing an integer. We'll need to cast that string into an integer and then convert it to a more traditional boolean.
                [System.Convert]::ToBoolean([int]$NinjaPropertyValue)
            }
            "Date or Date Time" {
                # In Ninja Date and Date/Time fields are in Unix Epoch time in the UTC timezone the below should convert it into local time as a datetime object.
                $UnixTimeStamp = $NinjaPropertyValue
                $UTC = (Get-Date "1970-01-01 00:00:00").AddSeconds($UnixTimeStamp)
                $TimeZone = [TimeZoneInfo]::Local
                [TimeZoneInfo]::ConvertTimeFromUtc($UTC, $TimeZone)
            }
            "Decimal" {
                # In ninja decimals are strings that represent a decimal this will cast it into a double data type.
                [double]$NinjaPropertyValue
            }
            "Device Dropdown" {
                # Device Drop-Downs Fields come in a JSON format this will convert it into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Device MultiSelect" {
                # Device Multi-Select Fields come in a JSON format this will convert it into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Dropdown" {
                # Drop-Down custom fields come in as a comma-separated list of GUIDs; we'll compare these with all the options and return just the option values selected instead of a GUID.
                $Options = $NinjaPropertyOptions -replace '=', ',' | ConvertFrom-Csv -Header "GUID", "Name"
                $Options | Where-Object { $_.GUID -eq $NinjaPropertyValue } | Select-Object -ExpandProperty Name
            }
            "Integer" {
                # Cast's the Ninja provided string into an integer.
                [int]$NinjaPropertyValue
            }
            "MultiSelect" {
                # Multi-Select custom fields come in as a comma-separated list of GUID's we'll compare these with all the options and return just the option values selected instead of a guid.
                $Options = $NinjaPropertyOptions -replace '=', ',' | ConvertFrom-Csv -Header "GUID", "Name"
                $Selection = ($NinjaPropertyValue -split ',').trim()
    
                foreach ($Item in $Selection) {
                    $Options | Where-Object { $_.GUID -eq $Item } | Select-Object -ExpandProperty Name
                }
            }
            "Organization Dropdown" {
                # Turns the Ninja provided JSON into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Organization Location Dropdown" {
                # Turns the Ninja provided JSON into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Organization Location MultiSelect" {
                # Turns the Ninja provided JSON into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Organization MultiSelect" {
                # Turns the Ninja provided JSON into a PowerShell Object.
                $NinjaPropertyValue | ConvertFrom-Json
            }
            "Time" {
                # Time fields are given as a number of seconds starting from midnight. This will convert it into a datetime object.
                $Seconds = $NinjaPropertyValue
                $UTC = ([timespan]::fromseconds($Seconds)).ToString("hh\:mm\:ss")
                $TimeZone = [TimeZoneInfo]::Local
                $ConvertedTime = [TimeZoneInfo]::ConvertTimeFromUtc($UTC, $TimeZone)
    
                Get-Date $ConvertedTime -DisplayHint Time
            }
            default {
                # If no type was given or not one that matches the above types just output what we retrieved.
                $NinjaPropertyValue
            }
        }
    }
    # Utility function for downloading files.
    function Invoke-Download {
        param(
            [Parameter()]
            [String]$URL,
            [Parameter()]
            [String]$Path,
            [Parameter()]
            [int]$Attempts = 3,
            [Parameter()]
            [Switch]$SkipSleep
        )
        Write-Host "URL given, Downloading the file..."

        $SupportedTLSversions = [enum]::GetValues('Net.SecurityProtocolType')
        if ( ($SupportedTLSversions -contains 'Tls13') -and ($SupportedTLSversions -contains 'Tls12') ) {
            [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol::Tls13 -bor [System.Net.SecurityProtocolType]::Tls12
        }
        elseif ( $SupportedTLSversions -contains 'Tls12' ) {
            [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
        }
        else {
            # Not everything requires TLS 1.2, but we'll try anyway.
            Write-Warning "TLS 1.2 and or TLS 1.3 are not supported on this system. This download may fail!"
            if ($PSVersionTable.PSVersion.Major -lt 3) {
                Write-Warning "PowerShell 2 / .NET 2.0 doesn't support TLS 1.2."
            }
        }

        $i = 1
        While ($i -le $Attempts) {
            # Some cloud services have rate-limiting
            if (-not ($SkipSleep)) {
                $SleepTime = Get-Random -Minimum 3 -Maximum 15
                Write-Host "Waiting for $SleepTime seconds."
                Start-Sleep -Seconds $SleepTime
            }
        
            if ($i -ne 1) { Write-Host "" }
            Write-Host "Download Attempt $i"

            try {
                # Invoke-WebRequest is preferred because it supports links that redirect, e.g., https://t.ly
                if ($PSVersionTable.PSVersion.Major -lt 4) {
                    # Downloads the file
                    $WebClient = New-Object System.Net.WebClient
                    $WebClient.DownloadFile($URL, $Path)
                }
                else {
                    # Standard options
                    $WebRequestArgs = @{
                        Uri                = $URL
                        OutFile            = $Path
                        MaximumRedirection = 10
                        UseBasicParsing    = $true
                    }

                    # Downloads the file
                    Invoke-WebRequest @WebRequestArgs
                }

                $File = Test-Path -Path $Path -ErrorAction SilentlyContinue
            }
            catch {
                Write-Warning "An error has occurred while downloading!"
                Write-Warning $_.Exception.Message

                if (Test-Path -Path $Path -ErrorAction SilentlyContinue) {
                    Remove-Item $Path -Force -Confirm:$false -ErrorAction SilentlyContinue
                }

                $File = $False
            }

            if ($File) {
                $i = $Attempts
            }
            else {
                Write-Warning "File failed to download."
                Write-Host ""
            }

            $i++
        }

        if (-not (Test-Path $Path)) {
            Write-Warning "Failed to download file!"
        }
        else {
            return $Path
        }
    }

    function Revoke-Certificate {
        param (
            $Object,
            $Loop = 0
        )
        $CrlPath = "$TEMP\CertRevokeListScript-$(Get-Date -Format FileDate).crl"
        Write-Host "[Info] Revoking certificates with CRL file from Path, URL, or custom field: $Object"
        if ($Object -like "http*") {
            Write-Host "[Info] Downloading CRL file"
            try {
                # Download the CRL file
                Invoke-Download -URL $Object -Path $CrlPath -SkipSleep -ErrorAction Stop
                Write-Host "[Info] Downloaded CRL file to $CrlPath"
                # Revoke the certificates
                certutil.exe -addstore CA $CrlPath
                Write-Host "[Info] Added CRL to the list of revoked certificates"
            }
            catch {
                Write-Host "[Error] Failed to download CRL file"
                exit 1
            }
            # Remove the temporary CRL file
            try {
                Remove-Item -Path $CrlPath -Force -Confirm:$false -ErrorAction Stop
            }
            catch {
                Write-Host "[Error] Failed to remove temporary CRL file"
                exit 1
            }
        }
        elseif ($(Test-Path -Path $Object -ErrorAction SilentlyContinue)) {
            # Revoke the certificates
            Write-Host "[Info] Adding CRL to the list of revoked certificates"
            try {
                $Object | Set-Content -Path $CrlPath -Force -ErrorAction Stop
                # Revoke the certificates
                certutil.exe -addstore CA $CrlPath
                Write-Host "[Info] Added CRL to the list of revoked certificates"
            }
            catch {
                Write-Host "[Error] Failed to revoke certificates with CRL file"
                exit 1
            }
            # Remove the temporary CRL file
            try {
                Remove-Item -Path $CrlPath -Force -Confirm:$false -ErrorAction Stop
            }
            catch {
                Write-Host "[Error] Failed to remove temporary CRL file"
                exit 1
            }
        }
        else {
            $ValueFromCf = Get-NinjaProperty -Name $Object
            if (
                # Check if Loop is 0 and the value from the custom field is a path or URL
                $Loop -eq 0 -and (
                    $(Test-Path -Path $ValueFromCf -ErrorAction SilentlyContinue) -or
                    $ValueFromCf -like "http*"
                )
            ) {
                # Call Revoke-Certificate if the Custom Field value is a path or URL
                # We'll only call Revoke-Certificate once to prevent an infinite loop via $Loop variable
                Revoke-Certificate -Object $ValueFromCf -Loop $($Loop + 1)
                return
            }
            $ValueFromCf | Set-Content -Path $CrlPath -Force -ErrorAction Stop
            
            # Revoke the certificates
            certutil.exe -addstore CA $CrlPath
            if ($LASTEXITCODE -ne 0) {
                Write-Host "[Error] Failed to revoke certificates with CRL file"
                exit 1
            }
            Write-Host "[Info] Added CRL to the list of revoked certificates"
            # Remove the temporary CRL file
            try {
                Remove-Item -Path $CrlPath -Force -Confirm:$false -ErrorAction Stop
            }
            catch {
                Write-Host "[Error] Failed to remove temporary CRL file"
                exit 1
            }
        }
        
    }
}
process {
    if (-not (Test-IsElevated)) {
        Write-Error -Message "Access Denied. Please run with Administrator privileges."
        exit 1
    }
    if ($PSSenderInfo) {
        Write-Host "[Error] This script cannot be run in a PSSession. Please run it locally or via Ninja RMM."
        exit 1
    }

    $CertificatesFound = $false
    $RemoveError = $false

    # Get a list of thumbprints from the environment variable
    if ($env:Thumbprints -and $env:Thumbprints -ne "null") {
        $Thumbprint = $env:Thumbprints -split ',' | ForEach-Object { "$_".Trim() }
    }
    elseif ($Thumbprint) {
        # Remove any commas from the thumbprint and trim any whitespace
        $Thumbprint = $Thumbprint | ForEach-Object { "$($_ -split ',')".Trim() }
    }
    if ($env:getCrlFromCustomField -and $env:getCrlFromCustomField -ne "null") {
        $GetCrlFromCustomField = $env:getCrlFromCustomField
    }

    # Get crl file path from the environment variable
    if ($env:certificateRevokeListPath -and $env:certificateRevokeListPath -ne "null") {
        $CertRevokeList = $env:certificateRevokeListPath
    }

    # Check that Thumbprint or CertRevokeList where specified
    if ($Thumbprint) {}
    elseif ($CertRevokeList) {}
    elseif ($GetCrlFromCustomField) {}
    else {
        Write-Host "[Error] Thumbprint or CertRevokeList or GetCrlFromCustomField where not specified. Please specify at least one of them."
        exit 2
    }

    # Check if the RemoveMatchingCertificates switch/checkbox was selected
    if ($env:removeMatchingCertificates -eq "true") {
        $RemoveMatchingCertificates = $true
    }

    # Get the custom field name from the Script Variable
    if ($env:customField) {
        $CustomField = $env:customField
    }

    if ($Thumbprint) {
        $Thumbprint = $Thumbprint | ForEach-Object {
            if ($_.Length -eq 40 -and $_ -match "^[0-9a-fA-F]{40}$") {
                Write-Host "[Info] Thumbprint($_) is valid and will be processed."
                $_
            }
            else {
                Write-Host "[Warn] Thumbprint($_) is not valid and will be skipped."
            }
        }
    
        # Loop through all certificates installed on the system
        $FoundCertificates = Get-ChildItem -Path Cert:\LocalMachine\ -Recurse | Where-Object { $_.Thumbprint -and $_.Thumbprint -in $Thumbprint }
    
        # Output the found certificates
        $OutputThumbprints = if ($FoundCertificates) {
            $CertificatesFound = $true
            Write-Host "[Alert] Found certificates:"
            $FoundCertificates = $FoundCertificates | ForEach-Object {
                [PSCustomObject]@{
                    Thumbprint = $_.Thumbprint
                    PSPath     = $_.PSPath
                    ExpiryDate = if ($_.NotAfter) { $_.NotAfter.ToShortDateString() }else { "No Expiry Date" }
                }
            }
            if ($FoundCertificates) {
                $thumbprint = "Thumbprint"
                $path = "Path"
                $padding = 40

                $centeredThumbprint = $thumbprint.PadLeft(($thumbprint.Length + $padding) / 2).PadRight($padding)
                $centeredPath = $path

                Write-Host "$centeredThumbprint - $centeredPath - Expires"
            }
            $FoundCertificates | ForEach-Object {
                $CertPath = $_
                $CertificatePath = $CertPath.PSPath
                # Convert PSPath to how certmgr.mmc formats the path
                $CertificatePath = $CertificatePath -replace 'LocalMachine\\', 'Local Computer\'
                $CertificatePath = $CertificatePath -replace '\\My\\', '\Personal\'
                $CertificatePath = $CertificatePath -replace '\\CA\\', '\Intermediate Certification Authorities\'
                $CertificatePath = $CertificatePath -replace '\\Root\\', '\Trusted Root Certification Authorities\'
                $CertificatePath = $CertificatePath -replace '\\Disallowed\\', '\Untrusted Certificates\'
                $CertificatePath = $CertificatePath -replace '\\AuthRoot\\', '\Third-Party Root Certification Authorities\'
                $CertificatePath = $CertificatePath -replace '\\TrustedPublisher\\', '\Trusted Publishers\'
                $CertificatePath = $CertificatePath -replace '\\ClientAuthIssuer\\', '\Client Authentication Issuers\'
                $CertificatePath = $CertificatePath -replace '\\Remote Desktop\\', '\Remote Desktop\'
                $CertificatePath = $CertificatePath -replace '\\SmartCardRoot\\', '\Smart Card Trusted Roots\'
                $CertificatePath = $CertificatePath -replace '\\TrustedPeople\\', '\Trusted People\'
                $CertificatePath = $CertificatePath -replace '\\Trust\\', '\Enterprise Trust\'
                $CertificatePath = $CertificatePath -replace '\\REQUEST\\', '\Certificate Enrollment Requests\'
                $CertificatePath = $CertificatePath -replace '\\AddressBook\\', '\Other People\'
                $CertificatePath = $CertificatePath -replace '\\UserdDS\\', '\Active Directory User Object\'
                # Output with the formatted path
                "$($CertPath.Thumbprint) - $($CertificatePath -replace 'Microsoft.PowerShell.Security\\Certificate::') - $($CertPath.ExpiryDate)"
            }
        }
        else {
            Write-Host "[Info] No certificates found"
        }
        if ($OutputThumbprints) {
            $OutputThumbprints | Out-String | Write-Host
        }
    
        # Remove the certificates if we should
        if ($RemoveMatchingCertificates) {
            Write-Host "[Info] Removing certificates"
            # Loop through all the found certificates
            $FoundCertificates | ForEach-Object {
                $Certificate = $_
                # Remove the certificate
                Write-Host "[Info] Removing certificate with path: $($Certificate.PSPath -replace 'Microsoft.PowerShell.Security\\Certificate::')"
                try {
                    # Remove the certificate and its private key
                    # More Info: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/about/about_certificate_provider?view=powershell-5.1#deleting-certificates-and-private-keys
                    if ($IsLinux) {
                        # Only used for testing purposes
                        Remove-Item -Path $Certificate.PSPath -Force -Confirm:$false -ErrorAction Stop
                    }
                    else {
                        Remove-Item -Path $Certificate.PSPath -DeleteKey -Force -Confirm:$false -ErrorAction Stop
                    }
                    Write-Host "[Info] Removed certificate with path: $($Certificate.PSPath -replace 'Microsoft.PowerShell.Security\\Certificate::')"
                }
                catch {
                    # Only error if there is only one certificate
                    # More than one certificate with the same thumbprint is likely already removed
                    if ($($FoundCertificates | Where-Object { $_ -like $Certificate.Thumbprint }).Count -eq 1) {
                        Write-Host "[Error] Failed to Remove certificate with thumbprint: $($Certificate.Thumbprint)"
                        $RemoveError = $true
                    }
                    else {
                        Write-Host "[Info] Removed certificate with path: $($Certificate.PSPath -replace 'Microsoft.PowerShell.Security\\Certificate::')"
                    }
                }
            }
        }
        else {
            Write-Host "[Info] Removing certificates is not enabled. Doing nothing."
        }
        if ($CustomField) {
            # Save the found thumbprints to a NinjaRMM custom field
            Write-Host "[Info] Saving thumbprints to custom field: $CustomField"
            try {
                if ($RemoveMatchingCertificates) {
                    Set-NinjaProperty -Name $CustomField -Value $($OutputThumbprints | ForEach-Object {
                            # Output just the path
                            "$("$_" -split ' - ' | Select-Object -Skip 1 -First 1) - Removed from system"
                        } | Out-String) -Type "MultiLine"
                }
                else {
                    Set-NinjaProperty -Name $CustomField -Value $($OutputThumbprints | ForEach-Object {
                            # Output just the path
                            "$("$_" -split ' - ' | Select-Object -Skip 1 -First 1)"
                        } | Out-String) -Type "MultiLine"
                }
            }
            catch {
                # If we ran into some sort of error we'll output it here.
                Write-Error -Message $_.ToString() -Category InvalidOperation -Exception (New-Object System.Exception)
                exit 1
            }
        }
    
        # Exit with an error when we failed to remove a certificate
        if ($RemoveError) {
            Write-Host "[Error] Failed to Remove one or more certificates"
            exit 1
        }
    
        # Exit with an error when we found certificates and we shouldn't remove them
        if ($CertificatesFound -and -not $RemoveMatchingCertificates) {
            Write-Host "[Alert] Certificates found"
            exit 1
        }
    }

    if ($CertRevokeList) {
        Revoke-Certificate -Object $CertRevokeList
    }
    if ($GetCrlFromCustomField) {
        Revoke-Certificate -Object $GetCrlFromCustomField
    }

    # Exit with a success when no certificates were found
    exit 0
}
end {
    
    
    
}

 

Access over 300+ scripts in the NinjaOne Dojo

Get Access

Detailed Breakdown

The script is a powerful tool that can find and optionally remove certificates based on specified thumbprints. Here’s a step-by-step explanation of how it works:

1. Initialization and Parameter Handling

  • The script begins by defining necessary parameters, such as Thumbprint, RemoveMatchingCertificates, CertRevokeList, GetCrlFromCustomField, and CustomField.
  • It includes utility functions like Test-IsElevated to check for administrative privileges and Set-NinjaProperty to set custom fields in NinjaRMM.

2. Checking Administrative Privileges

  • The Test-IsElevated function verifies if the script is running with administrative rights, which is crucial for accessing and modifying system certificates.

3. Thumbprint Processing

  • The script processes the provided thumbprints, ensuring they are valid and correctly formatted.
  • It retrieves certificates from the local machine’s certificate store and filters them based on the specified thumbprints.

4. Certificate Management

  • If the RemoveMatchingCertificates switch is enabled, the script attempts to remove the found certificates. It handles errors gracefully, ensuring that any issues are logged and managed appropriately.
  • The script can also save the results to a custom field in NinjaRMM, facilitating further management and reporting.

5. CRL Handling

  • The script can handle Certificate Revocation Lists (CRLs) from URLs, local paths, or custom fields. It downloads and processes these lists to revoke certificates as needed.

6. Output and Logging

  • Detailed output is provided at each step, ensuring transparency and ease of troubleshooting. The script logs found certificates, removal actions, and any encountered errors.

Potential Use Cases

Imagine an IT professional managing a large enterprise network. Certificates are used for securing internal communications and various services. The professional notices that some certificates are nearing expiration or have become compromised. Using this script, they can quickly find these certificates by their thumbprints and remove them, ensuring that the network remains secure.

Comparisons

Compared to manual methods of certificate management, this script offers significant advantages:

  • Efficiency: Automates the process of finding and removing certificates, saving time and reducing the risk of human error.
  • Accuracy: Ensures that only certificates with the specified thumbprints are affected, avoiding inadvertent changes to other certificates.
  • Integration: Can integrate with NinjaRMM, allowing for seamless management and reporting within a broader IT management framework.

FAQs

Q: What is a thumbprint, and why is it important?
A: A thumbprint is a unique identifier for a certificate, used to ensure that the correct certificate is being managed.

Q: Can this script be run on any version of Windows?
A: The script requires Windows 10 or Windows Server 2016 and higher.

Q: What happens if the script is not run with administrative privileges?
A: The script will fail to execute actions that require administrative rights, such as removing certificates.

Q: Can this script handle certificates from custom fields in NinjaRMM?
A: Yes, it can retrieve and process certificates using custom fields in NinjaRMM.

Implications

Using this script, IT professionals can ensure that certificates are correctly managed, reducing the risk of security breaches due to expired or compromised certificates. This proactive approach to certificate management enhances the overall security posture of the organization.

Recommendations

  • Regular Audits: Regularly run the script to audit and manage certificates, ensuring no expired or invalid certificates remain in the system.
  • Backup: Always back up the certificate store before making changes, allowing for recovery in case of errors.
  • Integration: Utilize the script’s integration with NinjaRMM for comprehensive management and reporting.

Final Thoughts

This PowerShell script is an invaluable tool for IT professionals and MSPs, streamlining the process of finding and managing system certificates by thumbprint. By integrating with NinjaRMM, it offers a powerful solution for maintaining security and compliance in an enterprise environment. NinjaOne’s robust platform, combined with this script, provides a comprehensive approach to IT management, ensuring that certificates are handled efficiently and securely.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service deliver tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, check out a live tour, or start your free trial of the NinjaOne platform.

Categories:

You might also like

×

See NinjaOne in action!

By submitting this form, I accept NinjaOne's privacy policy.

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).