Creating a function to connect to Exchange Online

OK so I’m way behind on this. I do migrations to Office 365/Exchange Online almost constantly, but I have been opening Remote PowerShell connections to Exchange Online the hard way. What I am about to post below is in no way original or timely. I am mostly posting it here so I can find it again without having to do too much searching.

Here is how to create a function in your PowerShell session to connect to Exchange Online

Step 1

Open PowerShell and type $profile. This will show you where your profile PowerShell profile is on that computer. It’s likely that you would have one, so go ahead and create a ps1 file in the location it tells you.

Step 2

Paste the following bit of code into your newly created PowerShell profile.

function Connect-ExchangeOnline
    if($ProxyEnabled){$Session = New-Pssession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential (Get-Credential) -Authentication Basic -AllowRedirection -sessionOption (New-PsSessionOption -ProxyAccessType IEConfig -ProxyAuthentication basic)}
    else{$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Authentication Basic -AllowRedirection -Credential (get-credential)}
    Import-PSSession $session
function Disconnect-ExchangeOnline
{Get-PSSession | ?{$_.ComputerName -like “*”} | Remove-PSSession}

then save the ps1 file

Step 3

Launch PowerShell, and type connect-exchangeonline. You get a pop-up box asking for credentials, put in your admin account to your Office 365 tenant.

Give it a second to connect, and you’re done.

When you’re done type disconnect-exchangeonline to close your session.

Easy as pie, right? It’s kind of silly that it has taken me this long to create these functions on my laptop but I guess better late than never, right?