PowerShell to find inactive user accounts in Office 365

If you’re like me and need to report on inactive accounts so that they can be disabled, this PowerShell script might help you out:
Get Office365 inactive accounts:

$UserCredential = Get-Credential
Import-Module MsOnline
Connect-MsolService -Credential $UserCredential
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://outlook.office365.com/powershell-liveid/” -Credential $Usercredential -Authentication “Basic” -AllowRedirection
Import-PSSession $exchangeSession -DisableNameChecking
Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | where {$_.LastLogonTime -lt ((get-date).AddDays(-90))} |
Select Name, Alias, UserPrincipalName, ForwardingSMTPAddress, ForwardingAddress, DeliverToMailboxAndForward, displayname, lastlogontime,  |
Export-csv c:\temp\O365InactiveUsers03082016.csv -NoTypeInformation

One thought on “PowerShell to find inactive user accounts in Office 365”

  1. And if your accounts aren’t using, or licensed with, a mailbox in Office365?

    By relying on Exchange to provide connectivity stats you’re excluding any accounts that have email turned off.

Leave a Reply