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

Leave a Reply