rookie_b
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?
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?
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.
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
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
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.
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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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.