Home » How to find outside email forwarding in Office 365 using Powershell

How to find outside email forwarding in Office 365 using Powershell

Hi there!

Have you ever tried doing simple stuff in Office 365 as an Email Administrator like ya know..

find out how many people have their work email forwarded to outside email addresses? Simple? Not exactly. Don’t worry, we got this.

There’s lots of awesome things you can do with Windows Powershell and 365 remotely, it turns out.

It’s great to know in case you are worried someone has forwarded their corporate email to an internal or external email address (or just want to be sure!)

1.     Connect to a machine that has access to Powershell

2.     Open Windows Powershell

3.     To start a session (that will ultimately connect you to Exchange Online) type:

$UserCredential = Get-Credential

 

4.     (In the dialog box type in your office 365 username and password using this format: aturner@mycompany.com and then click OK)

 

5.     Next, type (or paste – probably easier) the below:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic –AllowRedirection

 

6.     Then:

Import-PSSession $Session

 

7.     Next, type (or paste) the below command. You’ll want to be sure the following folder you specify below exists on your machine. This is so it can deposit the .csv there, in your aturner-admin\users (etc) .You can of course, name it anything you want, I named it Forwards.csv below.

 

Get-Mailbox | Where {$_.ForwardingsmtpAddress -ne $Null} | Select Name, ForwardingAddress,ForwardingsmtpAddress,DeliverToMailboxAndForward| Export-csv .\scripts\Forwards.csv –NoTypeInformation

 

8.     Double check the folder that you told it to put the .csv in. If your machine/server can’t read .csv. , you can always log into webmail at portal.officeonline.com and make a draft, attach it, then check it on your machine to verify that you’ve got the right results.

 

9.     When finished, make sure to type in the below command, so it will disconnect the session (otherwise it sits out there, for like, ever, bad juju, you don’t want that!)

Remove-PSSession $Session

(The scary note about it from Microsoft’s technet article: “If you close the Windows PowerShell window without disconnecting the session, you could use up all the remote PowerShell sessions available to you, and you’ll need to wait for the sessions to expire.”)

You’ll wanna check out these sources! I combined them together to figure this out, along with other fun commands!

Connecting to Exchange Online through powershellhttps://technet.microsoft.com/en-us/library/jj984289(v=exchg.160).aspx

More details on how to connect to csv- https://technet.microsoft.com/en-us/library/ee176825.aspx

More cool commandshttp://o365info.com/forward-mail-powershell-commands-quick/

 

Reposted from my LinkedIn