Find inactive member users with signInActivity and Microsoft Graph
What it does
Section titled “What it does”- Connects to Microsoft Graph with read-only permissions (
User.Read.All,AuditLog.Read.All). - Reads every user with the
signInActivityproperty and keeps members only. Guests are skipped. - Takes the most recent of three dates: last interactive sign-in, last non-interactive sign-in and last successful sign-in.
- Flags accounts where that date is older than
-Days(default 90). - Also flags accounts that never signed in and were created more than
-Daysago. - Writes one CSV row per flagged account.
It changes nothing in your tenant.
Prerequisites
Section titled “Prerequisites”- Licence: Microsoft Learn states that
signInActivitydetails need Microsoft Entra ID P1 or P2. - Role: Reports Reader is the least privileged role Learn lists for this task.
- Permissions:
User.Read.AllandAuditLog.Read.All, granted to Microsoft Graph PowerShell.
Install-Module Microsoft.Graph.Users -Scope CurrentUserScript
Section titled “Script”#Requires -Version 7.0#Requires -Modules Microsoft.Graph.Users<#.SYNOPSIS Read-only report of member accounts with no sign-in for N days.
.DESCRIPTION Reads signInActivity for all users through Microsoft Graph. Keeps member accounts (userType = Member). Flags an account when its most recent sign-in date is older than -Days, or when it never signed in and was created more than -Days ago. Exports the result to CSV. Makes no changes to the tenant.
.PARAMETER Days Number of days without a sign-in before an account counts as inactive. Default 90.
.PARAMETER OutFile Path of the CSV file to write.
.EXAMPLE ./Get-InactiveUsers.ps1
.EXAMPLE ./Get-InactiveUsers.ps1 -Days 180 -OutFile C:\Reports\inactive-180.csv#>param( [ValidateRange(1, 3650)] [int]$Days = 90, [string]$OutFile = (Join-Path $PWD ('InactiveUsers-{0:yyyyMMdd-HHmm}.csv' -f (Get-Date))))
Connect-MgGraph -Scopes 'User.Read.All', 'AuditLog.Read.All' -NoWelcome
$now = (Get-Date).ToUniversalTime()$cutoff = $now.AddDays(-$Days)
# signInActivity is only returned when you ask for it.$props = 'id', 'userPrincipalName', 'displayName', 'accountEnabled', 'userType', 'createdDateTime', 'signInActivity'
$users = Get-MgUser -All -Property $props | Where-Object { $_.UserType -eq 'Member' }
# Turn a nullable date into UTC, or $null when empty.# Graph sends UTC times; if the date arrives without a time-zone label, treat it as UTC.function ConvertTo-Utc($value) { if ($null -eq $value) { return $null } $d = [datetime]$value if ($d.Kind -eq [System.DateTimeKind]::Unspecified) { return [datetime]::SpecifyKind($d, 'Utc') } return $d.ToUniversalTime()}
# Format a date for the CSV, or "Never" when empty.function Format-SignIn($value) { if ($null -eq $value) { return 'Never' } return $value.ToString('yyyy-MM-dd HH:mm') + ' UTC'}
$report = foreach ($u in $users) { $sia = $u.SignInActivity $interactive = ConvertTo-Utc $sia.LastSignInDateTime $nonInteractive = ConvertTo-Utc $sia.LastNonInteractiveSignInDateTime $successful = ConvertTo-Utc $sia.LastSuccessfulSignInDateTime $created = ConvertTo-Utc $u.CreatedDateTime
# Most recent of the three sign-in dates. $null if none exist. $lastActivity = @($interactive, $nonInteractive, $successful) | Where-Object { $null -ne $_ } | Sort-Object -Descending | Select-Object -First 1
if ($lastActivity) { if ($lastActivity -gt $cutoff) { continue } $daysInactive = [int][math]::Floor(($now - $lastActivity).TotalDays) } else { # Never signed in. Skip new accounts created inside the window. if ($created -and $created -gt $cutoff) { continue } $daysInactive = 'Never' }
[pscustomobject]@{ UserPrincipalName = $u.UserPrincipalName DisplayName = $u.DisplayName AccountEnabled = $u.AccountEnabled CreatedDate = if ($created) { $created.ToString('yyyy-MM-dd') } else { 'Unknown' } LastInteractiveSignIn = Format-SignIn $interactive LastNonInteractiveSignIn = Format-SignIn $nonInteractive LastSuccessfulSignIn = Format-SignIn $successful DaysInactive = $daysInactive }}
$report = @($report)$report | Sort-Object @{ Expression = 'AccountEnabled'; Descending = $true }, UserPrincipalName | Export-Csv -Path $OutFile -NoTypeInformation -Encoding utf8
Write-Host "Checked $($users.Count) member accounts. Found $($report.Count) inactive for $Days+ days."Write-Host "Report saved to $OutFile"Disconnect-MgGraph | Out-NullHow to run
Section titled “How to run”- Save the script as
Get-InactiveUsers.ps1. - Open PowerShell 7 and run it:
./Get-InactiveUsers.ps1 # 90 days, CSV in the current folder./Get-InactiveUsers.ps1 -Days 180 # 180 days- Sign in as a Reports Reader (for example
admin@yourdomain.com). Accept the consent prompt if it appears.
Output
Section titled “Output”| Column | Meaning |
|---|---|
UserPrincipalName |
Sign-in name, for example jane@yourdomain.com |
DisplayName |
Name shown in the directory |
AccountEnabled |
True if the account can still sign in |
CreatedDate |
When the account was created. Unknown if Graph has no value |
LastInteractiveSignIn |
Last interactive sign-in attempt, or Never |
LastNonInteractiveSignIn |
Last non-interactive sign-in attempt, or Never |
LastSuccessfulSignIn |
Last successful sign-in, or Never |
DaysInactive |
Days since the most recent of the three dates, or Never |
Enabled accounts are listed first. Those are the ones to look at first.
Read the results
Section titled “Read the results”- Interactive and non-interactive dates record attempts, not only successes. A failed sign-in still updates them. Check
LastSuccessfulSignInto see real use. lastSuccessfulSignInDateTimestarted on 1 December 2023 and was not backfilled.Neverin that column alone does not prove the account was unused before then.- A blank date can be old activity. Learn says the last interactive sign-in can be empty if the last attempt was before April 2020.
- Data is not instant. The last sign-in value can take up to 24 hours to update.
- CreatedDate can be empty for some accounts created, or synced, before June 2018. The script treats those as old enough to report.
- Service and shared accounts may show as inactive. Confirm with the owner before you act.
What to do next
Section titled “What to do next”- Confirm each account with HR or the manager. Is the person still here? Is the account still needed?
- For leavers, follow the offboard a user SOP.
- For guests, use the stale guest accounts report instead.
- Run this report on a schedule, for example monthly, and keep each CSV with your review record.
- The script filters on the client side, not with
-Filter. Learn states that asignInActivityfilter can’t be combined with other filterable properties, so you can’t filter on sign-in date anduserTypein one query. - When
signInActivityis selected, Graph returns at most 500 users per page.-Allfollows every page for you, so large tenants take longer. - Store the CSV securely. It lists account names and activity.
Weekly · 5-minute read
The Identity Brief
Identity & Microsoft 365 changes that need action, one SOP, one script — every week. Verified against official Microsoft sources.
No spam. Unsubscribe anytime. Privacy