You're looking to extract the field values from a DN (Distinguished Name).
postanote's answer somewhat does that, but, due to returning a single string with spaces as the separator, the boundaries between the field values are lost.
If you want to retrieve the field values as an array of strings, use the regex::Matches()
.NET method; you can convert the array to a single-line representation with a separator of choice using -join
later:
# Outputs the field values as an *array*
[regex]::Matches(
(Get-ChildItem Cert:\LocalMachine\CA | Where-Object Subject -Match Windows).Subject,
'(?<==)[^,]+'
).Value
A simplified example, using -join
with a custom separator:
[regex]::Matches(
'CN=common name, OU=org unit 1, OU=org unit 2',
'(?<==)[^,]+'
).Value -join '|'
Verbatim output:
common name|org unit 1|org unit 2
If separator-based single-line output is the only goal, you can simplify to a single -replace
operation (same output as above):
(
'CN=common name, OU=org unit 1, OU=org unit 2' -replace
'(?:^|, )[a-z]+=', '|'
).Substring(1)
Caveat: like postanote's answer, this assumes that no field value contains escaped ,
or =
characters in the form of \,
or \=
.
A more complex regex would be required to account for that, such as shown in this answer.