Link to home
Start Free TrialLog in
Avatar of rookie_b
rookie_bFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Basic Powershell Property Filtering Question

Apologies for the basic question,my Googling skills obviously not up to the task!

So, I am trying to list all properties of an object, which include a certain string in the value.
For example, list any AD user properties. for e specific user,  that contain  "John" in the value.

so if I do get-aduser John -propetirs *| how do I filter this so it only returns properties that contain slthe string "John" anywhere in the value?
Avatar of Alex
Alex
Flag of United Kingdom of Great Britain and Northern Ireland image

You have to stipulate the attribute in order to get the value.

For example

Get-aduser -filter {(Name -like "*John*")}

or

Get-aduser -filter {(GivenName -like "*John*")}

You wouldn't for example use something like

Get-aduser -filter {(lastlogondate -like "*John*")}

since the last logon date is a, well, date... As such it wouldn't return an object.
Avatar of oBdA
oBdA

If I understood you correctly, this should do the trick.
Note that it's not restricted to a single user, you can obviously replace the "-Identity John" with any filter you want.
$pattern = '*john*'
Get-ADUser -Identity John -Properties * | ForEach-Object {
	$adUser = $_
	$adUser.psobject.Properties |
		Where-Object {$_.BaseObject -and ($adUser.($_.Name) -like $pattern)} |
		Select-Object -Property @{n='SamAccountName'; e={$adUser.SamAccountName}}, Name, Value
} | Sort-Object -Property SamAccountName, Name

Open in new window

Avatar of rookie_b

ASKER

Thanks Alex, any chance I can feed the all properties into a psobject or variable, or something, and then apply filtering to that?
that'll be the one oBdA has done for you
Hi oBdA, thank you for getting back to me.  Does this only look at name and samaccountname, or am I reading this wrong - which is very likely. I haven't had a chance to test it yet, but what if it is not a user at all. I am trying to find out if it is possible to do it for any object if that is at all possible.
oBdA's excellent script looks at ALL properties, but, as written. only for user objects (Get-ADUser).
You can easily modify it for all AD objects by substituting Get-ADObject instead.
I've added the object type (ObjectClass) to the output.
I've also changed the filter to search the entire AD tree, but you should narrow it down as much as possible.

$pattern = '*John*'
Get-ADObject -Filter * -Properties * | ForEach-Object {
	$adobject = $_
	$adobject.psobject.Properties |
		Where-Object {$_.BaseObject -and ($adobject.($_.Name) -like $pattern)} |
		Select-Object -Property @{n='SamAccountName'; e={$adobject.SamAccountName}},
		@{n='Type'; e={$adobject.ObjectClass}}, 
		Name, Value
} | Sort-Object -Property SamAccountName, Name 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks everyone. I was hoping I would simply be able to run something select-string and match a pattern, but this will also do for my particular purpose. Thanks lot!