Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitMicrosoft 365 administrators often need to track who added which members to which groups—especially for compliance, auditing, or operational reviews. In this article, we present a Graph PowerShell script that fetches audit log entries for the "Add member to group" event and displays key information such as Added Time, Group Name, Added User, Added By, and Operation Type.
# Connect to Microsoft Graph with necessary permissions
Connect-MgGraph -Scopes "AuditLog.Read.All", "Group.Read.All"
# Define the URI to fetch audit logs for "Add member to group" events
$uri = "https://graph.microsoft.com/v1.0/auditLogs/directoryAudits" +
"?`$filter=activityDisplayName eq 'Add member to group' and category eq 'GroupManagement'" +
"&`$orderby=activityDateTime desc"
# Make the API request
$response = Invoke-MgGraphRequest -Method GET -Uri $uri
# Prepare the results collection
$results = @()
# Iterate over each audit log entry
foreach ($entry in $response.value) {
try {
$addedTime = $entry.activityDateTime
$operation = $entry.activityDisplayName
$actorUPN = $entry.initiatedBy.user.userPrincipalName
$groupId = ""
$groupName = ""
$addedUserUPN = ""
# Parse target resources
foreach ($target in $entry.targetResources) {
if ($target.type -eq "Group" -and $target.id) {
$groupId = $target.id
}
if ($target.type -eq "User" -and $target.userPrincipalName) {
$addedUserUPN = $target.userPrincipalName
}
}
# Resolve group name using groupId
if ($groupId) {
try {
$group = Get-MgGroup -GroupId $groupId -Property DisplayName -ErrorAction Stop
$groupName = $group.DisplayName
} catch {
$groupName = "[Unknown Group]"
}
}
# Add result to output collection
$results += [PSCustomObject]@{
'Added Time' = $addedTime
'Group Name' = $groupName
'Operation' = $operation
'Added User' = $addedUserUPN
'Added By' = $actorUPN
}
} catch {
Write-Warning "Error processing entry: $($_)"
}
}
# Output the results
if ($results.Count -eq 0) {
Write-Host "No 'Add member to group' events found in audit logs." -ForegroundColor Yellow
} else {
$results | Sort-Object 'Added Time' -Descending | Format-Table -AutoSize
}
$results | Export-Csv "AddedGroupMembers.csv" -NoTypeInformation
Error | Cause | Solution |
Insufficient privileges to access audit logs | Missing delegated permissions | Ensure you use AuditLog.Read.All and Group.Read.All scopes |
Cannot retrieve group display name | The group might have been deleted | Display placeholder text like [Unknown Group] |
Invoke-MgGraphRequest returns empty | No matching events | Adjust filters or ensure logging is enabled for group management |
Tracking “Add member to group” actions is essential for security and compliance in Microsoft 365 environments. With Microsoft Graph PowerShell and audit logs, administrators gain deep visibility into such changes.
This script empowers you to instantly review who added whom to which group, when, and under what context—simplifying audit readiness and operational clarity.
© m365corner.com. All Rights Reserved. Design by HTML Codex