Search code examples
powershellcertificate

Get thumbprint of a certificate


I want to store the thumbprint of a certificate in a variable like this:

$thumbprint = 0F273F77B77E8F60A8B5B7AACD032FFECEF4776D

But my command output is:

Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "XXXXX"}

Thumbprint                                Subject 
----------                                ------- 
0F273F77B77E8F60A8B5B7AACD032FFECEF4776D  CN=XXXXXXX, OU=YYYYYYY 

I need to remove everything but the thumbprint of that output

Any idea?


Solution

  • All you have to do is wrap the command in parentheses, and then use dot-notation to access the Thumbprint property.

    Try this out:

    $Thumbprint = (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "XXXXXXX"}).Thumbprint;
    Write-Host -Object "My thumbprint is: $Thumbprint";
    

    If you get multiple certificates back from your command, then you'll have to concatenate the thumbprints into a single string, perhaps by using the -join PowerShell operator.

    $Thumbprint = (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "XXXXXXX"}).Thumbprint -join ';';
    Write-Host -Object "My thumbprints are: $Thumbprint";