PowerShell : List all active users and their group membership in a domain

So you need to know which users are members of which group… this little PowerShell Script will do the job.

  • Open Notepad and paste the code in below. Save it with the file extension ps1.
  • Open PowerShell with administrator rights.
  • Run the following command the allow unsigned scripts to run locally.
Set-ExecutionPolicy RemoteSigned

Full Documentation here.

Now run the file that you created above, ensure you have a folder on the root of the C: drive called tmp or change the script to your own folder.

It will spit out an CSV file which can be opened in Excel.

Import-Module ActiveDirectory

$Users = ForEach ($U in (Get-ADUser -Filter {Enabled -eq "True"}))
$UN = Get-ADUser $U -Properties MemberOf
$Groups = ForEach ($Group in ($UN.MemberOf))
(Get-ADGroup $Group).Name
$Groups = $Groups | Sort
ForEach ($Group in $Groups)
New-Object PSObject -Property @{
Name = $UN.Name
Group = $Group
$Users | Export-CSV C:tmpUserMemberships.csv


1 comment

  1. Is it possible for this script to display just the name and then each group on the same row in different cells ?

    Joe blogs | Group 1 | Group 2 | Group 3

    Also is it possible to only show groups that begin with a certain letter?

    Many Thanks and great work.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.