PowerShell: Search all property values

Viewed 320

How can I search all the properties of a single object for a given string?

Say I have the following command output:

get-aduser jtest -properties *

DistinguishedName : CN=jtest,CN=Users,DC=confederationc,DC=on,DC=ca
Enabled           : True
GivenName         : Justus
Name              : jtest
ObjectClass       : user
ObjectGUID        : f4d31d45-0505-433e-9442-152419e75d26
SamAccountName    : jtest
SID               : S-1-5-21-2138664166-620177494-281947949-184391
Surname           : Test
UserPrincipalName : jtest@confederationcollege.ca
...output truncated

How can I search for any property that contains the string "jtest"?

I feel like I must be missing something obvious.

2 Answers

There's always findstr (case insensitive), but the result is only text. This is a common question.

get-aduser jtest | findstr /i jtest

Name              : jtest
SamAccountName    : jtest
UserPrincipalName : jtest@confederationcollege.ca

You can use the hidden psobject memberset on any object in PowerShell to access its underlying properties programmatically:

foreach($user in Get-ADUser jtest){
  foreach($property in $user.psobject.Properties){
    if($_.Value -like '*jtest*'){
      "Property '$($_.Name)' has value '$($_.Value)'"
    }
  }
}
Related