Exchange

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.


Connect-ExchangeOnline

Unbenannt.PNG

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

Connect-ExchangeOnline


function Connect-ExchangeOnline {

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

# .PARAMETER
# None

# .EXAMPLE
# Connect-ExchangeOnline

# .NOTES
# Author: Patrick Gruenauer
# Web: https://sid-500.com

$userCredential = Get-Credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-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.

Unbenannt.PNG

See also

More about connecting to Exchange Online see Microsoft Docs: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/connect-to-exchange-online-powershell?view=exchange-ps

1 reply »

Leave a Reply

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

WordPress.com Logo

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

Google photo

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

Twitter picture

You are commenting using your Twitter 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.