We help IT Professionals succeed at work.

powershell script to output username and the number of groups they are a member of

davidm27
davidm27 used Ask the Experts™
on
Hi
I need a power shell script to output all Active directory users and display the number of groups they are a member of.

username, number of groups
username, number of groups
username, number of groups

thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You can do this easy using the free Quest AD cmdelts.

From a list of user

$Users = "User1", "User2", "User3", "User4"
$Users | foreach {
$count = (Get-QADMemberOf $_ | Measure-Object).count
Write-Output "$_, $Count"
}

All Domain Users, this may take awhile depending on user count

$Users = get-qaduser -sizelimit 0
$Users | foreach {
$count = (Get-QADMemberOf $_ | Measure-Object).count
Write-Output "$_, $Count"
}

Commented:
Also forgot to post this other option


$Users = Get-QADUser -Sizelimit 0
$Users | foreach {
$Name = $_.Samaccountname
$count = ($_.memberof | Measure-Object).count
Write-Output "$Samaccountname, $Count"
}

Commented:
If you have the Microsoft ActiveDirectory module available, you can try:

Get-ADUser -filter * -Properties MemberOf | select Name,
@{Label="Number of Groups";Expression={($_.MemberOf | Measure-Object).Count}}

Author

Commented:
@kenmcf
The script seems to do what i want but it does not display the username next to the group number it just displays the number. Any idea how I can get it to display

thanks
change the Write-Output line to

Write-Output "$_.samAccountName, $Count"
Commented:
I had a typo

$Users = Get-QADUser -Sizelimit 0
$Users | foreach {
$Name = $_.Samaccountname
$count = ($_.memberof | Measure-Object).count
Write-Output "$Name, $Count"
}

Author

Commented:
it just produces the following

#TYPE System.String
Length
30
43
38
34
31
42

Author

Commented:
works perfectly thanks