Avatar of kam_uk
kam_uk
 asked on

Count of users in AD group

Hi

I would like to get a count of users in a Distribution group in AD.

I have used the following command to get an export to xls, csv, txt but the columns aren't arranged in a way to allow me to check out how many users there are.

ldifde -f c:\emeatxt.txt -d "cn=\#Group A,ou
=users,ou=UK,ou=Europe,dc=emea,dc=kam,dc=com" -l "member"

Does anyone know how I can get a count?

Cheers
DatabasesActive DirectoryExchange

Avatar of undefined
Last Comment
Chris Dent

8/22/2022 - Mon
Chris Dent


Hey dude :)

Going to push PowerShell on you again :)

(Get-QADGroupMember "The Group").Count

Or just members if you do:

Get-QADGroupMember "The Group"

Chris
kam_uk

ASKER
Hi Chris!

I'm actually using Powershell!! :)

We seem to have a problematic DL. Powershell is giving me a count of members, I just want to verify its correct by checking with another LDAP querier! :)
Chris Dent


Ah ha :-D

DSQuery?

DSQuery group -Name "The Group" | DSGet group -members -expand

Chris
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
kam_uk

ASKER
Wow, you are the king of LDAP :)

So this will give me just the count or will it export it out somewhere?
Chris Dent


The DSQuery / DSGet command above will just drop a few lines to the console. Redirecting it to a text file will give you an easy place to count (line count if you do View / Status Bar).

DSQuery group -Name "The Group" | DSGet group -members -expand > GroupMembers.txt

There is one other thing that might be worth mentioning.

Universal Distribution Groups will not list all of their membership when you query the a standard Domain Controller. Instead you have to direct the query at a Global Catalog.

Might not be your issue, but I'm off home now so I figured it was worth mentioning ;)

In PowerShell that's:

(Get-QADGroupMember "The Group" -UseGlobalCatalog).Count

It's a bit more difficult with DSQuery, but something like this would do it:

DSQuery * ForestRoot -GC -Filter "(memberOf=CN=The Group,OU=somewhere,DC=kam,DC=uk)"

Where ForestRoot is actually an option in DSQuery rather than something you have to change. However, it does need the full DN of the group or it won't find anything.

Chris
Mike Kline

don't forget about adfind :)

http://www.joeware.net/freetools/tools/adfind/index.htm 
adfind -sc g:GROUPNAME member > c:\member.txt
You could import file into excel.  
Thanks
Mike
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chris Dent


I did have one more thought while I was on the train...

If you have a large group and you're trying to get the membership you might be bumping into a few more of the more obscure issues. How big is the group? Where big is typically more than 1500 members.

Chris
kam_uk

ASKER
Hi

The group is about 5000 odd?
Chris Dent


How inaccurate is the count you're getting?

I'll make a few thousand users on my domain here and see if I can see any discrepancies.

Chris
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
kam_uk

ASKER
Well, the other LDAP tools I tried are returning the exact value - PS is giving me about 3500.

I see from their support forum that this maybe a bug in the current version :(
Chris Dent


There's probably a way around it...

Search for the members instead of counting them on the attribute. It's a bit more hard work, but still possible.

For example:

(Get-QADUser -LdapFilter "(memberOf=$((Get-QADGroup 'The Group').DN))").Count

Note the use of single quotes in the middle there, it'll get upset if we don't use those.

Although it doesn't help us much if there's a bug... mine has just created 3900 users, 1100 to go, I'll verify it in a moment :)

Chris
Chris Dent


Lots of fun... So in mine...

Get-QADGroupMember "TheGroup" -SizeLimit 20000

Returns a maximum of 1000, and ignores any attempt to raise the limit over 1000.

(Get-QADGroup "TheGroup").Member.Count

Doesn't return anything at all (for large groups).

Fortunately this one does work:

(Get-QADUser -SizeLimit 20000 -LdapFilter "(&(objectCategory=person)(memberOf=$((Get-QADGroup 'TheGroup').DN)))").Count

If a little slowly... the trouble with broadly scoped searches on large domains...

DSQuery also works as follows:

(dsquery group -name "TheGroup" | dsget group -members -expand).Count

Although you have to do a -1 there because it's a bit of a rough way, relying on an implicit conversion of the returned lines to an array.

In summary, we need Dimitry to fix Quest's components. MS's cmdlets aren't going to be around for quite a long time and any of the simpler methods (vbscript, System.DirectoryServices) are flawed for large groups except using the same LDAP search as above.

Ho hum...

Chris
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mike Kline

good post chris!!
kam_uk

ASKER
Thanks Chris!

For Quest, I try the following command;

(get-qaduser -sizelimit 20000 -ldapfilter "(&(objectcategory=person)(memberOf=$((get-qadgroup 'Group 1').DN)))").count

But I get this error?

Get-QADUser : The (&(&(objectcategory=person)(memberOf=))(objectClass=*)(objectClass=user)) search filter is invalid.
At line:1 char:13
+ (get-qaduser  <<<< -sizelimit 20000 -ldapfilter "(&(objectcategory=person)(memberOf=$((get-qadgroup 'Group 1').DN))
)").count

And DS query;

(dsquery group -name "Group 1" | dsget group -members -expand).count

.count was unexpected at this time.

Do I need to enter the Distinguished name when using DSget?

Sorry to be a pain :(
BSonPosh

<cough>Legacy Groups</cough>
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
BSonPosh

@Chris,

You are correct. Legacy groups are groups with some or all members that are not LVR'd. Once the functionality level is upped only new members are LVR'd.

You can use this to check
http://bsonposh.com/archives/530

I have seen this cause issues with scripts... just something to look at.
Chris Dent


Hmm I did have a look at that last night when you posted.

The group I'd created, and all associated users, were created in a 2008 Native Mode domain. I ran ADFind against it with the flag in that for reporting on those and it seemed quite happy with the group.

Might have another go tonight, it's possible my Vista installation was unhappy with the Quest tools :)

Chris
BSonPosh

understand... this can only act qwerky if you were in a domain in pre 2003 mode
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chris Dent


Your mentioning it made me read up on the limits a bit (which was interesting) so I can appreciate the point and why it might be pertinent here :)

It might be worth testing that for the group in the original question anyway considering it's size.

Chris
kam_uk

ASKER
Many thanks - really appreciate your help ;)
vigge79

I require some assistance to convert legacy groups to LVR groups. I am using a powershell command but when i run this script a message displays
"dsmod failed:"group name": directory object not found

but when i execute the simple command within CMD the group converts without issues

any assistance is greatly appriciated
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Chris Dent

Probably failing to correctly parse part of the command. Can you paste exactly what you're typing here?

Chris