157

Is there a command line way to list all the users in a particular Active Directory group?

I can see who is in the group by going to Manage Computer --> Local User / Groups --> Groups and double clicking the group.

I just need a command line way to retrieve the data, so I can do some other automated tasks.

9 Answers 9

39

try

dsget group "CN=GroupName,DC=domain,DC=name,DC=com" -members
5
  • 1
    You probably need to do a little more in order to resolve members and duplicate members in nested groups. Aug 9, 2009 at 20:49
  • 1
    dsget group "CN=GroupName,DC=domain,DC=name,DC=com" -members -expand gets you the members in the nested groups, though it may not help with duplicates, and it gives you the full AD query string for the member, not just their CN.
    – jonnybot
    Jul 1, 2014 at 19:08
  • 2
    Command not found on Win7 SP1. I'm guessing you need RSAT installed?
    – Iszi
    Nov 12, 2014 at 19:54
  • 20
    'dsget' is not recognized as an internal or external command,etc.. Nov 25, 2015 at 9:56
  • 1
    @adolfgarlic - stackoverflow.com/questions/8514599/…
    – bchilders
    Jul 19, 2017 at 19:24
300

Here's another way from the command prompt, not sure how automatable though since you would have to parse the output:

If group is "global security group":

net group <your_groupname> /domain

If you are looking for "domain local security group":

net localgroup <your_groupname> /domain
6
  • 28
    not sure why this was downvoted... the output might require a little parsing, but it has the advantage of depending only on utilities that are part of the base windows install.
    – G-Wiz
    Jul 14, 2011 at 16:07
  • 2
    But is there any way around the truncated group names?
    – deed02392
    Oct 14, 2013 at 11:19
  • 2
    Note that this isn't recursive and doesn't list groups that are in a group. Not very helpful if you have nested or hierarchical groups.
    – Mark
    Jun 19, 2015 at 20:05
  • This works perfectly for me (and goes nicely hand in hand with net user /domain). Thanks!
    – xan
    Mar 23, 2016 at 20:05
  • 1
    For some reason I am getting error "The group name could not be found." for a group that I know exists.
    – tbone
    Apr 26, 2016 at 17:45
51

Here's a version of the ds command I found more typically useful, especially if you have a complex OU structure and don't necessarily know the full distinguished name of the group.

dsquery group -samid "Group_SAM_Account_Name" | dsget group -members -expand

or if you know the CN of the group, usually the same as the SAM ID, quoted in case there are spaces in the name:

dsquery group -name "Group Account Name" | dsget group -members -expand

As stated in the comments, by default the ds* commands (dsquery, dsget, dsadd, dsrm) are only available on a Domain Controller. However, you can install the Admin Tools pack from the Support Tools on the Windows Server installation media or download it from the Microsoft Download site.

You can also perform these queries using PowerShell. PowerShell is already available as an installable feature for Server 2008, 2008 R2, and Windows 7, but you'll need to download the WinRM Framework to install it on XP or Vista.

To get access to any AD-specific cmdlets in PowerShell you will ALSO need to perform at least one of the following installs:

4
  • I use the first query all the time
    – Jim B
    Aug 3, 2009 at 20:29
  • 1
    Be aware that you have to run this command on the domain controler.
    – skolima
    Mar 24, 2011 at 11:49
  • 9
    You DO NOT have to run this command on a Domain Controller. However, you DO need to have installed the Admin Support Pack .msi which is included in the Support Tools on the Windows 2003 Server CDROM. Or download it from microsoft.com/downloads/en/… Mar 24, 2011 at 15:50
  • Regarding dsquery and Admin Tools pack, it's worth noting that "Starting with Windows 10 October 2018 Update, RSAT is included as a set of "Features on Demand" in Windows 10 itself.", see microsoft.com/en-za/download/details.aspx?id=45520
    – W.Prins
    Jul 15, 2021 at 8:35
30

For a PowerShell solution that doesn't require the Quest AD add-in, try the following

Import-Module ActiveDirectory

Get-ADGroupMember "Domain Admins" -recursive | Select-Object name

This will enumerate the nested groups as well. If you don't wish to do so, remove the -recursive switch.

1
  • 7
    You're correct that the ActiveDirectory module doesn't require the Quest ActiveRoles AD snap-in, but it does require that you have the RSAT tools installed and that your AD has at least one DC with either the Active Directory Web Service (for 2008 R2) or the Active Directory Management Gateway Service (for 2003, 2008 DCs) installed. See my answer above for details and links. Oct 11, 2011 at 19:36
18

A very easy way which works on servers and clients:

NET GROUP "YOURGROUPNAME" /DOMAIN | find /I /C "%USERNAME%"

Returns 1 if user is in group YOURGROUPNAME, else will return 0

You can then use the %ERRORLEVEL% value (0 if user in group, 1 if not) like

IF %ERRORLEVEL%==0 NET USE %LOGONSERVER%\YOURGROUPSHARE
1
  • 3
    The "net group" command searches only domain (or domain tree) where the computer is joined. A more modern alternative is: whoami /groups | find /I /C "YOURGROUPNAME"
    – DavisNT
    Sep 15, 2015 at 17:04
9

Using PowerShell and Quest Software's Free ActiveRoles Management Shell for Active Directory, you can use:

(Get-QADGroup "GroupName").Members

http://www.quest.com/powershell/activeroles-server.aspx

3
  • Also: Get-QADGroupMember 'GroupName' Aug 9, 2012 at 15:13
  • 1
    It looks like the AD tooling from Quest may now be defunct and the link currently redirects to oneidentity.com/products/active-roles for a paid product called Active Roles
    – KyleMit
    May 14, 2020 at 19:41
  • Oh well, that sucks. It was good while it lasted. May 19, 2020 at 19:11
7

The answers here using dsget and dsquery will only work on server versions of Windows as those command's aren't shipped on other versions of Windows (e.g. Windows 7). On machines without those commands you can get the information you want using the AdFind command.

Here's an example query for getting group membership:

AdFind.exe -default -f name="Domain Admins" member -list
1
  • 5
    ADFind.exe is a third party tool you have to install anyway. The ds* commands are installable via the Admin Tools pack. Oct 11, 2011 at 19:37
4

For display members of the UserGroup1 try:

dsquery group -name UserGroup1 | dsget group -members | dsget user -display
6
  • 1
    Might want to point out what CLI utility you'd use this in. I don't think that's gonna work too well from cmd, for some reason. Oct 10, 2012 at 9:01
  • @HopelessN00b: this works in cmd
    – Pete
    Nov 7, 2012 at 20:42
  • @Pete Newp! dsget failed:'Target object for this command' is missing. Nov 7, 2012 at 20:53
  • @HopelessN00b: I get error if UserGroup1 doesn't exist, try something like "Accounting*"
    – Pete
    Nov 7, 2012 at 21:48
  • @Pete: HopelessN00b is right, this requires a set of tools that are not coming with every version of windows.
    – petrosmm
    Apr 7, 2021 at 16:39
4

How to list local groups and users?

Use the following powershell script to list the local groups and members of those groups.

$server="YourServerName"
$computer = [ADSI]"WinNT://$server,computer"

$computer.psbase.children | where { 

$_.psbase.schemaClassName -eq 'group' } | foreach {
    write-host $_.name
    write-host "------"
    $group =[ADSI]$_.psbase.Path
    $group.psbase.Invoke("Members") | foreach {
$_.GetType().InvokeMember("Name", 'GetProperty', 

$null, $_, $null)}
    write-host
}

Copy the text above in to notepad and save as filename.ps1. Then run the file. I should display the Groups and Users in each group, or you can just run this from powershell.

2
  • I get, cmdlet ForEach-Object at command pipeline position 1. Supply values for the following parameters: and then in asks for input, Process[0]
    – Mike S
    Mar 18, 2016 at 13:55
  • @MikeS ... Something like this perhaps: $computer.psbase.children | ? {$_.psbase.schemaClassName -eq 'group'} | ? {$_.Name -eq 'Group Name 123'} | % {$group =[ADSI]$_.psbase.Path;$group.psbase.Invoke("Members") | % {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) | % {"[email protected]"}}}; Oct 22, 2021 at 19:15

You must log in to answer this question.