Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all groups that a user is a member of?

People also ask

How do you find the list of all groups a user is member of?

To get (sorted) plain list of groups only, you can run (New-Object System. DirectoryServices. DirectorySearcher("(&(objectCategory=User)(samAccountName=$($env:username)))")). FindOne().

How do I get group membership in PowerShell?

The PowerShell Get-ADGroupMember cmdlet is used to list the members of an Active Directory group. You can just type the cmdlet in a PowerShell window and you'll be prompted to enter the name of the group you want to use.

Is there another way to check the group membership of a user?

Hit Windows+R, type “lusrmgr. msc” into the Run box, and then hit Enter. In the “Local Users and Groups” window, select the “Users” folder, and then double-click the user account you want to look at. In the properties window for the user account, switch to the “Member Of” tab.


Get-ADPrincipalGroupMembership will do this.

Get-ADPrincipalGroupMembership username | select name

name
----
Domain Users
Domain Computers
Workstation Admins
Company Users
Company Developers
AutomatedProcessingTeam

Single line, no modules necessary, uses current logged user:

(New-Object System.DirectoryServices.DirectorySearcher("(&(objectCategory=User)(samAccountName=$($env:username)))")).FindOne().GetDirectoryEntry().memberOf

Kudos to this vbs/powershell article: http://technet.microsoft.com/en-us/library/ff730963.aspx


A more concise alternative to the one posted by Canoas, to get group membership for the currently-logged-on user.

I came across this method in this blog post: http://www.travisrunyard.com/2013/03/26/auto-create-outlook-mapi-user-profiles/

([ADSISEARCHER]"samaccountname=$($env:USERNAME)").Findone().Properties.memberof

An even better version which uses a regex to strip the LDAP guff and leaves the group names only:

([ADSISEARCHER]"samaccountname=$($env:USERNAME)").Findone().Properties.memberof -replace '^CN=([^,]+).+$','$1'

More details about using the [ADSISEARCHER] type accelerator can be found on the scripting guy blog: http://blogs.technet.com/b/heyscriptingguy/archive/2010/08/24/use-the-powershell-adsisearcher-type-accelerator-to-search-active-directory.aspx


Old school way from CMD:

net user mst999 /domain