#PSTip Using CIM cmdlets to find the user group membership
Note: This tip requires PowerShell 3.0 or above.
Recently, on Windows Server 2012, I was trying to determine, both locally and remotely, if a user belongs to a specific group. For example, when I am running Hyper-V automation scripts, I need to determine if the currently logged-in user is part of Hyper-V administrators group or not.
In PowerShell 3.0, we can use CIM cmlets and associations to find this easily.
Get-CimInstance -Filter "Name='Jeff'" -ClassName Win32_UserAccount |
Get-CimAssociatedInstance -Association Win32_GroupUser |
Select-Object Name
Simple!
Now, to find the group memberships of the locally logged-in user on a remote machine, we just need to run:
Get-CimInstance -ClassName Win32_UserAccount -Filter "Name='$env:UserName'" -ComputerName Server-01 |
Get-CimAssociatedInstance -Association Win32_GroupUser |
Select-Object Name
Share on: