PowerShell for Windows Admins

Jun 24 2013   3:23PM GMT

WMI association example



Posted by: Richard Siddaway
Tags:
PowerShell
PowerShell 3
WMI

A question came up on the Powershell.org forum about finding the groups of which local accounts are members

You can get account data using Win32_UserAccount

Group information is held in Win32_Group. You can see the relationship between users and groups by dumping the Win32_GroupUser instances. You will see a load of entries like this

GroupComponent : Win32_Group (Name = “Administrators”, Domain = “RSLAPTOP01″)
PartComponent : Win32_UserAccount (Name = “Administrator”, Domain = “RSLAPTOP01″)
PSComputerName : CimClass : root/cimv2:Win32_GroupUser
CimInstanceProperties : {GroupComponent, PartComponent}
CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties

WMI classes have associations – in this case there is an association between the Win32_User and the Win32_Group classes. The Win32_GroupUser can be thought of as the linking class. What we need to do to is to go from the individual instances of Win32_User (the users) to the associated groups.

Something like this should do it

$data = Get-CimInstance -ClassName Win32_UserAccount -Filter “LocalAccount = $true” |
foreach {
$groups = Get-CimAssociatedInstance -InputObject $PSItem -ResultClassName Win32_Group | select -ExpandProperty Name
$PSItem | Add-Member -MemberType NoteProperty -Name “Groups” -Value ($groups -join “;”) -PassThru
}
$data | select Caption, Groups

WMI – a bit convoluted but it always gets there

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: