Are you connecting to Exchange Online and On-premises Exchange server in two different PowerShell windows? Are you struggling to script to do things with two different Exchange environments?
Say No More to that struggling. Say HELLO to “PREFIX” when connecting to Exchange server in PowerShell. This blog post will show you how to connect both Exchange environments (cloud and on-prem) on same PowerShell Window and use both Exchange servers on a single script.
Before we go further: I am using Exchange Online PowerShell V2 Module that supports modern authentication (Okta, AzureAD, OneLogin, etc.,). Install it from here: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps
Now the little script to connect to both Exchange environments. You may download the script from >>> HERE <<<.
<# * * * * Connect-ExchangeEnvironments.ps1 * * * * Connects Exchange Online and Exchange OnPrem with "Prefix" option. Prefix: EO for Exchange Online and OP for OnPrem. Written by: Anand, The Awesome #> # ACTION REQUIRD: Enter the Admin Username, email address, and OnPrem Exchange Server Name below $OnPremUser = "Domain\AdminUserName" $CloudUser = "AdminUsername@company.com" $OnPremExchangeServer = "ExchServer.company.com" # Connection URI to connect to OnPrem Exchange PowerShell Web Session $ConnectionURI = "http://$OnPremExchangeServer/PowerShell/" # Get credentials for Exchange Online and Exchange OnPrem Admin account $OnPremcredential = Get-Credential -UserName $OnPremUser -Message "Enter Password for Exchange On-Prem Admin" $O365credential = Get-Credential -UserName $CloudUser -Message "Enter Password for Exchange Online Admin" # Connect to Exchange Online V2 Connect-ExchangeOnline -UserPrincipalName $CloudUser ` -Prefix 'EO' ` -Credential $O365credential # Connect to Exchange OnPremises $Session = New-PSSession -ConfigurationName Microsoft.Exchange ` -ConnectionUri $ConnectionURI ` -Authentication Kerberos ` -Credential $OnPremcredential Import-PSSession $Session -Prefix OP <# * * * * End of the Script * * * * When you are done with Exchange Work: You may kill the Exchange sessions when you are done with following Command. Get-PSSession | Remove-PSSession #>
Alright, it’s time to explain how this works.
Notice the Prefix option on these both commands in the script:
Connect-ExchangeOnline -UserPrincipalName $CloudUser `
-Prefix ‘EO’ `
Import-PSSession $Session -Prefix OP
I chose EO for Exchange Online and OP for On Premises for the tag. You MAY choose to have your own Tag here. Once you run this script, it will connect to Exchange Online and Exchange OnPrem on the PowerShell Window.
To use the Exchange command, you use the tag with the usual Exchange commands. See examples below:
On-Prem Exchange – Get-Mailbox:
Get-OPMailbox -Identity AwesomeAnand@company.com
Exchange Online – Get-Mailbox:
Get-EOMailbox -Identity CloudUser@company.com
Hope this helps you all Exchange Admins. Enjoy.
2 thoughts on “How to Connect Exchange Online & On-Premises Exchange server on a single PowerShell Window”
A discussion on this topic: