There is a lot to learn. Think of a basic object as an array of hashtables (it is more complex than this, but for simplistic visuals):
#Create Array
$object = @()
#Add to hashtable to array
$object += [PSCustomObject]@{
Name = 'Sam'
Hobby = 'Swimming'
}
$object += [PSCustomObject]@{
Name = 'Alice'
Hobby = 'Volleyball'
}
There is an array called object, we are adding a hash table. A hash table has unique keys, so for instance you cannot have two Name keys in the same hashtable. We want to collect information on multiple things usually, users, computers, etc., so we have a PSObject that allows us to have an array of hashtables. The above example is flat, one key with one value. There are two keys to filter on, Name and Hobby and then there are comparison operators (-eq, -like, -ne, etc.) and logical operators (-and, -or, etc.). This allows you to search with Where-Object to find exactly what you are looking for:
#Find an hashtable in the array that has a key hobby that equals Volleyball
$object | Where-Object -FilterScript {$_.Hobby -eq 'VolleyBall'}
#Find an hashtable in the array that has a key hobby that equals Swimming and Name is like <anything>a<anything>
$object | Where-Object -FilterScript {$_.Hobby -eq 'Swimming' -and $_.Name -like '*a*'}
When you get into things like MemberOf, that is a multi-value attribute (there are several AD Properties like otherMail that are multi-value), which is an array typically. So, our folks above have more than one hobby just like someone has more that one AD Group:
#Create Array
$object = @()
#Add to hashtable to array
$object += [PSCustomObject]@{
Name = 'Sam'
Hobby = 'Swimming', 'BasketBall','Dancing'
}
$object += [PSCustomObject]@{
Name = 'Alice'
Hobby = 'Volleyball', 'Coding','Chess'
}
This allows you to use the same operators as above to filter result. This of course can get more complicated with object nested in objects, but it’s important to understand the basic structure of data to ensure you are filtering correctly. The Hobby values are now surrounded by curly brackets to indicate an array:
Name Hobby
---- -----
Sam {Swimming, BasketBall, Dancing}
Alice {Volleyball, Coding, Chess}
But Powershell makes it easy to use the same filters:
$object | Where-Object -FilterScript {$_.Hobby -eq 'VolleyBall'}
$object | Where-Object -FilterScript {$_.Hobby -like '*c*'}