choco_linux
choco_linux

Reputation: 153

Powershell Select-object from cert store

Essentially I am trying to get the serial number of a certificate by using the subject so I can use it in a variable in CertUtil.

I am so close yet so far as you will see below can anybody help please?

cd cert:\localmachine\my

$serno = get-childitem | where { $_.subject -eq "XXXXXXXXXXXXXXXX" } | format-list     -property * | select -property SerialNumber

$serno

this displays

SerialNumber        
------------ 

not the actual serial number

Does anybody have any clues??

Upvotes: 10

Views: 21932

Answers (1)

Duncan
Duncan

Reputation: 95612

Don't use format-list, you already have all the properties. format-list will convert your nice X509Certificate2 object into a set of format objects which isn't what you want at all. Also use -expandproperty on the select:

PS>get-childitem | where { $_.subject -eq "CN=localhost" } | select -expandproperty SerialNumber
6191F4A438FF77A24763E6D427749CD7

Or with Powershell 3 or later, use the shorthand notation:

PS>$serno = (get-childitem | where { $_.subject -eq "CN=localhost" }).SerialNumber
PS>$serno
6191F4A438FF77A24763E6D427749CD7

Upvotes: 15

Related Questions