Connect To Exchange Online Using PowerShell Automatically

This very handy PowerShell script will show how to connect to Exchange Online automatically without a Username and Password prompt, I use it all the time to manage my environments.

To get started you will need to Install all the necessary components and PowerShell modules to connect to Office 365.

Once everything In Installed, Copy the script below to ISE, change the login details and run:

$powerUser = ""

$powerPass = "password"

$password = ConvertTo-SecureString $powerPass -AsPlainText -Force

$adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $powerUser,$password

$O365Cred = $adminCredential

Connect-MsolService –Credential $O365Cred

Import-Module MSOnline

$exchangeSession = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri -Credential $O365Cred -Authentication "Basic" -AllowRedirection

Import-PSSession $exchangeSession

When running Exchange Online will load all cmdlets to the PS Session.

To view all available cmdlets type using the session name :

Get-command -module tmp_h1fto0n.g5r

When you are finished working with Exchange Online type to cmdlet below to exit the Ps Session

Remove-PSSession $Session

Leave a Reply