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

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..
LVL 9
David SankovskySenior SysAdminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Iradat SiddiquiManager OperationsCommented:
Import-Module ActiveDirectory
(Get-ADUser userName –Properties MemberOf | Select-Object MemberOf).MemberOf
David SankovskySenior SysAdminAuthor Commented:
this one liner will get me the properties of a single user, and it doesn't export the info anywhere..
Iradat SiddiquiManager OperationsCommented:
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

David SankovskySenior SysAdminAuthor Commented:
Which will give me all the users in my AD, but without any security group membership.
LearnctxEngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David SankovskySenior SysAdminAuthor Commented:
A little UDF in Excel gave me what I needed based on the result of this script!
Great work.
Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.