Tracking user account modifications in Microsoft 365 is crucial for security, compliance, and troubleshooting. With Microsoft Graph PowerShell, administrators can query Azure AD audit logs to fetch recently updated users, along with details about when the update occurred and who performed it.
In this article, we will explore a Graph PowerShell script that helps identify all recently updated users in your organization.
Below is a Graph PowerShell script that queries Azure AD audit logs to fetch all recently updated users. The script displays:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "AuditLog.Read.All"
# Define the date range (fetch updates from the last 7 days)
$startDateTime = (Get-Date).AddDays(-7).ToString("yyyy-MM-ddTHH:mm:ssZ")
# Fetch all DirectoryAudit logs related to 'Update user' in the past 7 days
$updatedUsersLogs = Get-MgAuditLogDirectoryAudit -Filter "activityDisplayName eq 'Update user' and activityDateTime ge $startDateTime"
# Create an array to store the results
$updatedUserDetails = @()
# Loop through the audit logs to extract relevant details
foreach ($event in $updatedUsersLogs) {
$updatedTime = $event.ActivityDateTime
$updatedUser = ($event.TargetResources | Where-Object { $_.UserPrincipalName }).UserPrincipalName
$updatedBy = $event.InitiatedBy.User.UserPrincipalName
# Create an object for each updated user record
$userDetail = [pscustomobject]@{
"Updated Time" = $updatedTime
"Updated User" = $updatedUser
"Updated By" = $updatedBy
}
# Add the object to the results array
$updatedUserDetails += $userDetail
}
# Display results in a tabular format
if ($updatedUserDetails.Count -eq 0) {
Write-Host "No updated user events found in the last 7 days."
} else {
$updatedUserDetails | Format-Table -AutoSize
}
Here are some ways to extend the script’s capabilities:
To check for updates over a longer period, adjust the date filter:
$startDateTime = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ssZ") # Last 30 days
If you need to store or analyze the data, export it:
$updatedUserDetails | Export-Csv -Path "UpdatedUsers.csv" -NoTypeInformation
To track changes to a specific user, modify the filter:
$updatedUsersLogs = Get-MgAuditLogDirectoryAudit -Filter "activityDisplayName eq 'Update user' and initiatedBy/user/userPrincipalName eq 'admin@domain.com'"
This script is valuable in multiple administrative scenarios:
Error | Cause | Solution |
Get-MgAuditLogDirectoryAudit : Access Denied | The account running the script lacks the required permissions. | Ensure you have the AuditLog.Read.All permission. Run: Connect-MgGraph -Scopes "AuditLog.Read.All" |
No updated user events found in the last 7 days. | No user updates were recorded in the given time frame. | Increase the date range by modifying AddDays(-7) to AddDays(-30). |
The term 'Get-MgAuditLogDirectoryAudit' is not recognized. | Microsoft Graph PowerShell module is not installed. | Install it using: Install-Module Microsoft.Graph -Scope CurrentUser |
Tracking user modifications in Microsoft 365 is essential for security, compliance, and troubleshooting. This Graph PowerShell script provides an easy way to fetch recently updated users, highlighting when updates occurred and who performed them.
By customizing the script to your needs, you can enhance your audit logs analysis, improve security monitoring, and optimize user management workflows.
Try it out today and take control of your Microsoft 365 user management!
© m365corner.com. All Rights Reserved. Design by HTML Codex