How to Find Out What Group a Given User Has

How to find out what group a given user has?

groups

or

groups user

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

Get-ADPrincipalGroupMembership will do this.

Get-ADPrincipalGroupMembership username | select name

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

List all groups and their descriptions for a specific user in Active Directory using PowerShell

From Get-ADPrincipalGroupMembership manual:

The Get-ADPrincipalGroupMembership cmdlet returns a default set of ADGroup property values. To retrieve additional ADGroup properties pass the ADGroups objects produced by this cmdlet through the pipline to Get-ADGroup. Specify the additional properties required from the group objects by passing the -Properties parameter to Get-ADGroup.

So, let’s do it!

import-module activedirectory
$username = Read-Host 'Please enter Username!'
Get-ADPrincipalGroupMembership $username | Get-ADGroup -Properties * | select name, description

Also, in this case it should be enough to specify name,description instead of asterisk (*). If this is a performance issue, replace it. I am leaving it at asterisk because you might later change your mind about which properties you need.

Finding what Groups/Distribution lists a specific user belongs to in active directory

This returns all the roles (Groups) that a user belongs to.

public string[] GetRolesForUser(DirectoryEntry user)
{
user.RefreshCache(new string[] { "tokenGroups" });

var irc = new IdentityReferenceCollection(user.Properties["tokenGroups"].Count);
foreach (byte[] sidBytes in user.Properties["tokenGroups"])
irc.Add(new SecurityIdentifier(sidBytes, 0));

var coll = new StringCollection();
irc = irc.Translate(typeof(NTAccount));

foreach (var ir in irc)
{
if (ir is NTAccount)
{
coll.Add(ir.ToString());
}
}
var accounts = new string[coll.Count];

coll.CopyTo(accounts, 0);
return accounts;
}


Related Topics



Leave a reply



Submit