Exchange: Connecting to Exchange Server using Windows PowerShell

Summary

Most of the times Exchange administrators needs to perform several tasks from PowerShell related to Exchange troubleshooting, health diagnostics etc. Exchange Server 2016 Exchange Management Shell is built on Windows PowerShell technology and provides a powerful command-line interface that enables automation of administrative tasks. We can use the Exchange Management Shell to manage every task of Exchange, for example, we can create email accounts, create Send and Receive connectors, configure mailbox database properties, and manage distribution groups and so on. Exchange admins can connect to Exchange PowerShell from their own desktop computer or any specific Server which they dedicated for administrative tasks. In this article, we will walk through the steps how we can connect with Exchange Server form Local PowerShell and can perform administrative jobs/tasks.

PowerShell or Windows PowerShell ISE Session

When we run a normal Windows PowerShell or Windows PowerShell ISE sessions, the cmdlets that we required to administer Exchange Server are not available immediately. But if we install the Exchange Server 2016 management tools locally, we can add Exchange Management Shell support in it. For that purpose we need to run the following command:-

Note:- We always need to run PowerShell with administrator privileges (Run As Administrator).

Import-Module 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto -ClientApplication:ManagementShell

Connecting to a Remote On-Premises Exchange Server

We can connect Exchange on-premises server from Windows 10 based client system where we have the network access to our Exchange Servers. if we do not have the Exchange Server 2016 management tools installed locally, we can still establish a remote Windows PowerShell session to your Exchange Server 2016. The following commands will establish a remote Windows PowerShell session to an on-premises Exchange Server, and make the Exchange Management Shell cmdlets available for you to run Exchange- related cmdlets.

Start PowerShell from your local PC with administrative privileges and run the following commands:-

$UserCredential = Get-Credential

After running above command, it will prompt you Credential Request dialog box, type your username and password which has the proper permission to remote the Exchange PowerShell and then click OK. 

At this point, you need to specify the fully qualified domain name (FQDN) of the Exchange 2016 Mailbox server that you want to connect to such as mbx01.mstechguru.ae or you can also specify the IP address of your mailbox server.

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mbx01.mstechguru.ae/PowerShell/ -Authentication Kerberos -Credential $UserCredential

Now you need to run the following command to finally start working on Exchange remote PowerShell.

Import-PSSession $Session

This will import Exchange PowerShell into your local PC windows PowerShell and you will be able to perform all administrative tasks from PowerShell.

After performing the necessary task you can remove the session by this command.

Remove-PSSession $Session

This is how we can perform routing task and troubleshooting with Exchange remote PowerShell connection.