We help IT Professionals succeed at work.

Basic Powershell Property Filtering Question

rookie_b
rookie_b asked
on
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?
Comment
Watch Question

AlexA lack of information provides a lack of a decent solution.

Commented:
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.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

Commented:
Thanks Alex, any chance I can feed the all properties into a psobject or variable, or something, and then apply filtering to that?
AlexA lack of information provides a lack of a decent solution.

Commented:
that'll be the one oBdA has done for you

Author

Commented:
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 JacobsCitrix Technology Professional / Director of TechDev Services, IPM

Commented:
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

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You can run that pretty much with anything, not only AD objects.
The script above will return objects with three properties:
SamAccountName (or DistinguishedName for below, since not all AD objects have a SamAccountName): the name of the object
Name: the name of the property
Value: the value of the property
So if John has a SamAccountName of jdoe, and "John" is found in givenName and displayName, it will return (in list format) something like
SamAccountName: jdoe
Name: displayName
Value: Doe, John

SamAccountName: jdoe
Name: givenName
Value: John

Open in new window


For computers, you can just replace Get-ADUser with Get-ADComputer, for groups accordingly with Get-ADGroup.
Here's the adjusted version for any object. Note that aside from the filter, you can use the -SearchBase argument to restrict the search to an OU, for example; SearchScope can be Base, OneLevel, Subtree. Try not to get every single AD object ...
$pattern = '*xxx*'
Get-ADObject -Filter * -Properties * -SearchBase 'OU=Domain Controllers,DC=domain,DC=com' -SearchScope Base | ForEach-Object {
	$adObject = $_
	$adObject.psobject.Properties |
		Where-Object {$_.BaseObject -and ($adObject.($_.Name) -like $pattern)} |
		Select-Object -Property `
			@{n='DistinguishedName'; e={$adObject.DistinguishedName}},
			@{n='Type'; e={$adobject.ObjectClass}}, 
			Name,
			Value
} | Sort-Object -Property DistinguishedName, Name

Open in new window

Author

Commented:
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!