Several methods to retrieve group membership for user and group:
Several methods to retrieve group membership for user and group:

Using Powershell:
============

Using powershell – for a user samaccountname:

Import-module Activedirectory
(Get-ADUser <samaccountname> -Properties MemberOf | Select-Object MemberOf).MemberOf        ; list of groups the user belongs

Get-QADUser -SamAccountName <samaccountname> | % {$_.MemberOf } | Get-QADGroup | select name    ; idem
(Get-ADUser <samaccountname> -Properties MemberOf | Select-Object MemberOf).MemberOf.Count     ; Display the number of the groups the user belongs

Other useful powershell cmdlet:
get-adprincipalgroupmembership <samaccountname>
(get-adprincipalgroupmembership <samaccountname>).count

Using powershell – for a Group samaccountname:

Import-module Activedirectory
(Get-ADGroup “domain users” -Properties MemberOf | Select-Object MemberOf).MemberOf        ; list of groups the “domain users” group belongs
(Get-ADGroup “domain users” -Properties MemberOf | Select-Object MemberOf).MemberOf.count    ; Display the number of the groups the group “domain users” belongs

Using powershell with Quest cmdlets for AD – for a user samaccountname:

Add-PsSnapin Quest.ActiveRoles.ADManagement

(get-QADGroup -containsmember <samaccountname>)                ; list of groups the user belongs
(get-QADGroup -containsmember <samaccountname>).count            ; Display the number of the groups the user belongs

(get-QADGroup -containsIndirectmember <samaccountname>)            ; list of all nested groups the user belongs
(get-QADGroup -containsIndirectmember <samaccountname>).count        ; Display the number of the groups the user belongs

Using powershell with Quest cmdlets for AD – for a group samaccountname:

Add-PsSnapin Quest.ActiveRoles.ADManagement

(get-QADGroupMember ‘administrators’)                ; list content of administrators group
(get-QADGroupMember ‘administrators’).count            ; count the content

(get-QADGroupMember ‘administrators’ -indirect)            ; list the content with nesting content!
(get-QADGroupMember ‘administrators’ -indirect).count        ; count the full content with nested content!

 

Using dsquery/dsget:
===============

To display only the MemberOf of a group:

dsquery group -name “domain users” -d <domain> | dsget group -memberof                ; list of groups the “domain users” group belongs

To display only the MemberOf of a user:

dsquery user -samid <samaccountname> | dsget user -memberof | dsget group -samid        ; list of groups the user belongs

WARNING: but this command does not list recursive groups!

dsquery user -samid <samaccountname> | dsget user -memberof -expand | dsget group -samid        ; list of groups the user belongs (including recursive groups)