Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitMonitoring user activity is vital for ensuring compliance, identifying inactive accounts, and maintaining license efficiency in Microsoft 365. With Microsoft Graph PowerShell, you can easily fetch the last sign-in time of all users, including those who have never logged in. This article provides a script and a breakdown of how it works, along with practical enhancements and error handling tips.
# Connect to Microsoft Graph with required scopes
Connect-MgGraph -Scopes "User.Read.All", "AuditLog.Read.All", "Directory.Read.All"
# Get all users with SignInActivity property
$users = Get-MgUser -All -Property Id, DisplayName, UserPrincipalName, SignInActivity -ConsistencyLevel eventual
# Format and display results
$users | Select-Object `
@{Name = "Display Name"; Expression = { $_.DisplayName }},
@{Name = "User Principal Name"; Expression = { $_.UserPrincipalName }},
@{Name = "Last Active Time"; Expression = {
if ($_.SignInActivity.LastSignInDateTime) {
($_.SignInActivity.LastSignInDateTime).ToLocalTime()
} else {
"Not Signed In"
}
}} | Format-Table -AutoSize
The script starts by connecting to Microsoft Graph using:
Connect-MgGraph -Scopes "User.Read.All", "AuditLog.Read.All", "Directory.Read.All"
These scopes are required to access directory and sign-in activity information.
The Get-MgUser cmdlet is called with:
Each user's SignInActivity.LastSignInDateTime is checked.
Results are printed in a table with:
Here are a few ways you can take this script further:
$users | Select-Object ... | Export-Csv "LastActiveUsers.csv" -NoTypeInformation
Add logic to filter users inactive for 30+ days.
Combine with license reports to identify users who are inactive but still consuming licenses.
Schedule the script and trigger alerts for stale accounts.
Error | Cause | Solution |
SignInActivity not returned | Missing -Property or -ConsistencyLevel | Ensure both parameters are present in Get-MgUser |
Access Denied | Insufficient Graph permissions | Add AuditLog.Read.All, User.Read.All, Directory.Read.All scopes |
Object Not Found | Invalid UPN or ID | Ensure correct usage of Get-MgUser with -All |
Empty Output | No users have signed in yet | Valid result if you're testing in a fresh or demo tenant |
The Last Active Time of Microsoft 365 users provides actionable insights for identifying inactive users, optimizing license usage, and strengthening security postures. Using Graph PowerShell and the SignInActivity property, administrators can access this data with precision and flexibility.
This lightweight, scalable approach is ideal for periodic audits and activity tracking within any Microsoft 365 environment.
© m365corner.com. All Rights Reserved. Design by HTML Codex