Enumerate user memberships with Powershell
Enumerating user memberships is a common task to perform. Unlike other scripting languages, with Powershell it’s a one-line action.
First download/install Quest AD Tools, and add them:
add-pssnapin quest.activeroles.admanagement
I use a semicolon to combine two Powershell commands:
$sUser = get-qaduser -samaccountname <username>; $sUser.MemberOf
The problem is that the primary group is not included in the MemberOf attribute.
Here is the Powershell command to get the primary group of a user:
Get-QADUser | Select name, @{n=”PrimaryGroup”;e={(Get-QADGroup “$($_.Sid.AccountDomainSid)-$($_.PrimaryGroupId)”).name}}