Get Group Membership

Overview

Scripts useful for getting group membership. Run them from an admin-elevated Windows PowerShell ISE script pane.

ActiveDirectory

 1# Import the Active Directory PowerShell module to provide cmdlets for AD operations
 2Import-Module ActiveDirectory
 3
 4# Get all Active Directory groups, sorted by name. The -filter * returns all groups.
 5$groups = (Get-ADGroup -filter * | Sort Name)
 6
 7# Store the total number of groups for progress tracking
 8$totalgroups = $groups.Count
 9
10# Initialize a counter for tracking the current group number
11$i = 1
12
13# Initialize an empty array to hold the output data
14$output = @()
15
16# Loop through each group
17foreach ($group in $groups) {
18    # Increment the counter
19    $i++
20    # Display the current processing progress in the console
21    Write-Progress -activity "Processing $($group.Name)" -status "$i out of $totalgroups groups"
22
23    # Get the members of the current group
24    $groupMembers = Get-ADGroupMember -Identity $group
25
26    # Loop through each member of the current group
27    foreach ($member in $groupMembers) {
28        # Create a new PSObject to hold the details
29        $object = new-object PSObject
30
31        # Add all properties of the group to the object
32        $group.PSObject.Properties | foreach {
33            $object | Add-Member -MemberType NoteProperty -Name ("Group_" + $_.Name) -Value $_.Value
34        }
35
36        # Add all properties of the member to the object
37        $member.PSObject.Properties | foreach {
38            $object | Add-Member -MemberType NoteProperty -Name ("Member_" + $_.Name) -Value $_.Value
39        }
40
41        # Add the object to the output array
42        $output += $object
43    }
44    
45    # Exit the loop early for debugging purposes after processing 10 groups
46    if($i -eq 10){break;}
47}
48
49# Define the file path for the output CSV file at the root of the file system
50$csvFilePath = "C:\output.csv"
51
52# Export the data in the output array to a CSV file, omitting the type information
53$output | Export-Csv $csvFilePath -NoTypeInformation
54
55# Open the newly created CSV file in the default CSV file handler (typically Excel or a text editor)
56Start-Process -FilePath $csvFilePath

Connect-AzureAD

 1# Connect to Azure Active Directory (Azure AD)
 2Connect-AzureAD
 3
 4# Get all Azure AD groups
 5$groups = Get-AzureADGroup -All $true
 6
 7# Initialize an empty array to hold the results
 8$resultsarray = @()
 9
10# Get the total number of groups for progress tracking
11$totalgroups = $groups.Count
12
13# Initialize a counter for the loop
14$i = 0
15
16# For each group in the array of groups
17ForEach ($group in $groups){
18    $i++
19
20    # Display a progress bar in the console
21    Write-Progress -activity "Processing $group.DisplayName" -status "$i out of $totalgroups groups"
22
23    # Get all members of the current group
24    $members = Get-AzureADGroupMember -ObjectId $group.ObjectId -All $true 
25
26    # For each member in the array of members
27    ForEach ($member in $members){
28        # Create a new PSObject to hold the details
29        $object = new-object PSObject
30
31        # Add properties to the object for the group and member details
32        $group.PSObject.Properties | ForEach-Object {
33            $object | add-member -membertype NoteProperty -name ("Group " + $_.Name) -Value $_.Value
34        }
35
36        $member.PSObject.Properties | ForEach-Object {
37            $object | add-member -membertype NoteProperty -name ("Member " + $_.Name) -Value $_.Value
38        }
39
40        # Add the object to the results array
41        $resultsarray += $object
42    }
43    # Break after processing 10 groups for debugging. Remove or adjust this for actual run.
44    if($i -eq 10){break;}
45}
46
47# Define the output path for the CSV file
48$csvFilePath = "C:\output.csv"
49
50# Export the results array to a CSV file, without type information
51$resultsarray | Export-Csv $csvFilePath -NoTypeInformation
52
53# Open the CSV file automatically with the default associated application
54Start-Process -FilePath $csvFilePath