PowerShell to find Office365 mailboxes with forwards enabled

Need to find mailboxes that are leaking data outside the organisation through enabled forwarding?  This is the script for you:

#Get Office365 email forwards:

$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 | Where {($_.ForwardingSMTPAddress -ne $null) -or ($_.ForwardingAddress -ne $null)} | Select Name, ForwardingSMTPAddress, ForwardingAddress, DeliverToMailboxAndForward | Export-csv c:\temp\Fwdusers.csv -NoTypeInformation

Leave a Reply