Connect to an Exchange server by using PowerShell
Problem:
You want to use PowerShell to set up a remote connection to an Exchange server.
Solution:
With this article you can:
Connect to on-premises Exchange server
- Verify the requirements for on-premises Exchange Server
- Start Windows PowerShell in administrator mode
Verify your Execution policy settings by running the below command:
Get-ExecutionPolicy
Change the execution policy to 'RemoteSigned' or 'Unrestricted' if it is currently set to Restricted.
Set-ExecutionPolicy RemoteSigned
Run the following command and enter the target server administrator credentials:
$LiveCred = Get-Credential
Run the following command to set up the connection to your Exchange server 2010, 2013, 2016 and 2019
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://<target-server-address>/powershell/ -Credential $LiveCred
Please note that the aforementioned approach is not always applicable. Depending on the setup of your target environment, you might need to use http rather than https to connect, specify the authentication method as an option (-Authentication Kerberos) and so on. The following example demonstrates these requirements:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<target-server-address>/powershell/ -Credential $LiveCred -Authentication Kerberos
In addition, you must specify some Options instead of New-PSSession and pass it as a variable. For a sample of how to use the advanced option for a session, refer to the 'Troubleshooting the PowerShell errors' section below.
Run the following command to start the connection
Import-PSSession $Session
Run the following command to disconnect Exchange server
Remove-PSSession $Session
Furthermore, you can launch Windows PowerShell and load the Exchange snap-in from there, if you are logged in directly to an on-premises Exchange server and for some reason are unable to run the Exchange Management Shell:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
Connect to Exchange Online (Microsoft 365)
- Verify the requirements for Exchange Online (Microsoft 365)
- Start Windows PowerShell in administrator mode
Verify your Execution policy settings by running the below command:
Get-ExecutionPolicy
The execution policy is normally set to Restricted. It is advised to configure the policy to RemoteSigned in order to connect to Exchange Online with PowerShell successfully:
Set-ExecutionPolicy RemoteSigned
Run the following command and enter your Microsoft 365 global admin credentials:
$LiveCred = Get-Credential
Run the following command to connect to Exchange Online (Microsoft 365)
Connect-ExchangeOnline -Credential $LiveCred
Use the Connect-ExchangeOnline cmdlet without the -Credential parameter if you're connecting to Exchange Online using an account that has MFA enabled. Provide your credentials in a pop-up window.
Note that if you have your account with MFA enabled, the cmdlet doesn't accept the -Credential parameter.
Run the following command to disconnect Exchange Online
Disconnect-ExchangeOnline
Troubleshooting the PowerShell errors
Invalid credentials
You may get the following error when you attempt to connect to an On-Premises Exchange server
Connecting to remote server <ServerName> failed with the following error message : The user name or password is incorrect.
To resolve the error, you need to enter the correct login credentials. When you login to Exchange Online, a similar error may appear.
Connecting to remote server ps.outlook.com failed with the following error message : [ClientAccessServer=XXXXX,BackEndServer=YYYYY,RequestId=ZZZZZ, TimeStamp=MM/DD/YYYY H:MM:SS PM] Access Denied
In this situation, you should also carefully check the provided credentials for spelling errors.
Issues with certificates
If you encounter the following certificate errors, you will be unable to set up a remote connection by using the methods described previously in this article.
Connecting to remote server <ServerName> failed with the following error message : The server certificate on the destination computer <ServerName> has the following errors:
The SSL certificate is signed by and unknown certificate authority.
The SSL certificate contains a common name (CN) that does not match the hostname.
To bypass the certificate validation checks, use the New-PSSessionOption cmdlet instead:
$SessionOpt = New-PSSessionOption -SkipCACheck:$true -SkipCNCheck:$true -SkipRevocationCheck:$true
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://<target-server-address>/powershell/ -Credential $LiveCred -SessionOption $SessionOpt
After you run the cmdlet, you should be able to start a new session successfully.
Cannot recognize 'Connect-ExchangeOnline'
If you want to connect to Exchange Online using PowerShell (with the modern method), you must first install the Exchange Online PowerShell V2 module. The following error message occurs if the PowerShell V2 module is not installed:
Connect-ExchangeOnline : The term 'Connect-ExchangeOnline' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
To resolve this error, install and import the PowerShellGet and Exchange Online PowerShell V2 modules. You can accomplish this activity by using the following cmdlets:
Install-Module -Name ExchangeOnlineManagement;
Import-Module -Name ExchangeOnlineManagement
Install the prerequisite package provider prompted by PowerShell before you install the new Exchange Online Management module.
You may need to update your registry to proceed if you encounter the "Unable to download from URI" issue at this stage (i.e., while trying to download the NuGet provider). To solve the problem, the .NET Framework must support strong cryptography. Refer to the Microsoft website for more details.
Click here to know the steps for Exchange migration using EdbMails
Click here to know the steps for Exchange Online (Office 365) migration using EdbMails