Microsoft 365

Office 365 Exchange Online PowerShell: Quick connect with Connect-ExchangeOnline

More and more companies are migrating to Office 365. This applies above all to small and medium-sized enterprises. This trend can hardly be stopped. So I decided to focus more on this new technology to support my Office 365 customers even better than before.

The first thing I noticed was that it took 3 commands to log in to Exchange Online. My mission was clear: to automate these commands to simplify the logon to Exchange Online.

The Objective

My objective was to use only one command instead of three. And here it is, my new Powershell function Connect-ExchangeOnline.



Nice one. You will now wonder how you can implement this in your environment. Which brings me to the next point. The code itself.


function Connect-ExchangeOnline {

# Connect-ExchangeOnline connects you to your Office365 Exchange Online portal.

# None

# Connect-ExchangeOnline

# Author: Patrick Gruenauer
# Web:

$userCredential = Get-Credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri `
-Credential $userCredential -Authentication Basic -AllowRedirection
Import-Module (Import-PSSession $session -DisableNameChecking) -Global `
-WarningAction SilentlyContinue
$domain=Get-AcceptedDomain | Where-Object Default -EQ 'True'
Write-Output "***** Welcome to Exchange Online for the domain $domain *****"

How to use it

Copy the code above into PowerShell ISE (ise.exe) and run the code. Then type the command and have fun with it.

If you want to make the function permanently available, so that the function is available every time you start PowerShell, you have to create a folder in C:\Program Files\WindowsPowerShell\Modules. Name the folder Connect-ExchangeOnline. Then save the code as .psm1 file in that folder. The screenshot below will help you.


See also

More about connecting to Exchange Online see Microsoft Docs:

2 replies »

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.