In my last post I showed how to relate find the groups of which a local user is a member. A comment was left asking about the inverse relationship.
In this case we can just turn the code round.
Get-CimInstance -ClassName Win32_Group |
foreach {
$users = Get-CimAssociatedInstance -InputObject $psitem `
-ResultClassName Win32_UserAccount |
select -ExpandProperty Caption
$psitem | Add-Member -MemberType NoteProperty -Name ‘Users’ `
-Value ($users -join ";") -PassThru
} | select Name, Users
Note: You can’t do this as a matter of course with all WMI relationships. Some relationships are one way in that A has a link to B but B has no way to link back to A unless you can find a property in common.