Avatar of rookie_b
rookie_b
Flag 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?
PowershellWindows Server 2012Windows Server 2008

Avatar of undefined
Last Comment
rookie_b

8/22/2022 - Mon
Alex

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.
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

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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Alex

that'll be the one oBdA has done for you
rookie_b

ASKER
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.
Sam Jacobs

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rookie_b

ASKER
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!