PowerShell: Get all Active Directory users membership

In PowerShell, you can do many things and this includes reporting on items or updating information but specifically in Active Directory, you can pull information about users and groups etc simply by using PowerShell.

Below is a script that allows you to get membership information for all users in Active Directory and output the file to a CSV in the root of C:\, here is the script:

$users = Get-ADUser -Filter * 
$CSVFile = "C:\group_membership.csv"

foreach($user in $users){
  add-content $CSVFile $user.Name
  $groups = Get-ADPrincipalGroupMembership $user.SamAccountName 
  foreach($group in $groups){
    add-content $CSVFile $group.name
  }
}

Here is what the code looks like in PowerShell:

I am running this from Active Directory directly, if you are not then you need to import the module using the following line at the start:

Import-Module ActiveDirectory

You can head over to the root of C:\ and open your CSV file in Excel and format the data.

Hope it helps.

You may also like...

Leave a Reply