Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save win2000b/ff56b4d00c3c82fca2e7d58c9490562e to your computer and use it in GitHub Desktop.

Select an option

Save win2000b/ff56b4d00c3c82fca2e7d58c9490562e to your computer and use it in GitHub Desktop.
Find All disabled Users excluding Shared Mailboxes
# Step 1: Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All"
# Step 2: Get all disabled users from Microsoft Graph
$allUsers = Get-MgUser -All -Property "DisplayName,UserPrincipalName,AccountEnabled,CreatedDateTime"
$disabledUsers = $allUsers | Where-Object { $_.AccountEnabled -eq $false }
# Step 3: Connect to Exchange Online
Connect-ExchangeOnline
# Step 4: Get all shared mailboxes from Exchange Online
$sharedMailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox
$sharedUPNs = $sharedMailboxes.UserPrincipalName
# Step 5: Filter out shared mailboxes from disabled users
$filteredUsers = $disabledUsers | Where-Object { $sharedUPNs -notcontains $_.UserPrincipalName }
# Step 6: Format output
$results = $filteredUsers | Select-Object DisplayName, UserPrincipalName, CreatedDateTime, @{Name="DisabledDate";Expression={"Unknown (requires audit logs)"}}
# Step 7: Display and export
$results | Format-Table -AutoSize
$results | Export-Csv -Path "Disabled365Accounts_ExcludingSharedMailboxes.csv" -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment