Get all users and the security groups they are members of in AD

David Sankovsky
David Sankovsky used Ask the Experts™
on
Hi Guys,
I'd appreciate your help.
I need a PS script that queries all users in an Active Directory (regardless of OU, all users)
and give out a CSV that has the username in one column and each group he is a member of in another (something like)
User             group 1                       group 2                           group3
Admin          domain admin          enterprise admins         etc..
Comment
Watch Question

Do more with

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

Commented:
Import-Module ActiveDirectory
(Get-ADUser userName –Properties MemberOf | Select-Object MemberOf).MemberOf

Author

Commented:
this one liner will get me the properties of a single user, and it doesn't export the info anywhere..
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Which will give me all the users in my AD, but without any security group membership.
This is a little different from the format you are after. Rather than doing...

User      Group1                  Group2
user1      domain admin      enterprise admin

It will do it as below.

User      Group
user1      domain admin
user1      enterprise admin
user2      accounts user
user2      sharepoint user
user3      some random group name

You can use pivot tables or PowerShell (via Group-Object, etc) to massage the data pretty easily. The problem I have with using column names like Group1, Group2 and so on is that they're meaningless headers. Group1 for 1 user could be different to another user. The only solution to this for me would be to make the column header the group name itself which you can then easily filter on.

Import-Module ActiveDirectory

$Results = New-Object System.Collections.ArrayList

Foreach ($User in @(Get-ADUser -Identity * -Properties memberOf))
{
	Foreach ($Group in @($User.memberOf | Get-ADGroup | Select -Expand Name))
	{
		$tmp = New-Object PSCustomObject -Property @{
			User = $User.Name
			Group = $Group
		}
		[void]$Results.Add($tmp)
	}
}

$Results | Export-Csv -NoType c:\results.csv

Open in new window

Author

Commented:
A little UDF in Excel gave me what I needed based on the result of this script!
Great work.
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial