Harshith R
Harshith R

Reputation: 448

Reading Object's Property Value in Powershell

I am trying to check if a particular certificate is installed on my machine. I am using the following powershell script

SET-LOCATION CERT:\LOCALMACHINE\my
$certificate = "CN=*XXXX"
$ListOfCertificate = GET-CHILDITEM | Select-Object -Property Subject
$ListOfCertificate.ForEach({if($_==$certificate) {Write-Output "Certficate Successfully Installed"} })

ListOfCertifiacte Object looks like this:

Thumbprint                                Subject
----------                                -------
yu39B5646D569XXXXXX
ui05F79VVVVVVVVVV                          CN=*XXXX
6kj6A3AAAAAAAAAAAA                          CN=XXXXXXXX
1ygfA1NNNNNNNNNNNN                          CN=XXXXXXXX

I just want to loop through this list and check if Subject Property Contains "*XXXX" value. But the above code is not working.its throwing error saying $certifiacte is not recognized as a cmdlet or script. what i am doing wrong?

Upvotes: 0

Views: 505

Answers (1)

Matt R
Matt R

Reputation: 428

This can be done in a much less complicated looking fashion in a single line:

If (Get-ChildItem Cert:\LocalMachine\My\ | Where-Object {$_.Subject -like CN=*XXXX}) {"Cert installed."}

Shortened:

If (Get-ChildItem Cert:\LocalMachine\My\ | ? Subject -like CN=*XXXX) {"Cert installed."}

Upvotes: 1

Related Questions