Search code examples
powershellcertificateget-childitem

Powershell certificate issuer is wrong


I get a specific certificate using

$certificate = Get-Childitem -path $path - Recurse | where {$_.Subject -match $cn} 
| Sort-Object -Property NotAfter -Descending | Select-Object -first 1

Later I need the certificates issuer and subject:

($certificate).Subject
($certificate).Issuer

Here's my problem: Issuer does not return the expected value but the same value as Subject. The other columns work.

When I use "dir cert - Recurse" I can see the right values, but when I added "|Format-List - Property *" at the end of my Get-Childitem call I can see that Issuer is already the wrong value and the expected value is in non of the other columns.

This is my first time posting a question, I hope you can help me :)


Solution

  • Like Mathias suggested, I needed to filter out self-signed certificates:

    ... |Where-Object {$_.Subject -match $cn -and $_.Subject -ne $_.Issuer}