How to connect to Exchange servers via PowerShell
You want to set up a remote session to an Exchange server via PowerShell.
This article will help you:
- connect to your Exchange servers via remote PowerShell session
- troubleshoot PowerShell errors that you may encounter during the process.
- Check the requirements for:
- Run Windows PowerShell.
Check your Execution policy settings:
If the execution policy is set to Restricted, change it to RemoteSigned or Unrestricted (this might need to be executed from PowerShell in the Run as administrator mode):
Provide the target server administrator credentials:
$LiveCred = Get-Credential
Configure the connection:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://<target-server-address>/powershell/ -Credential $LiveCred
Be advised the method shown above is not universal. Depending on your target environment configuration, you may be required to specify the authentication method as an argument (-Authentication Kerberos), connect via http instead of https, etc., as shown in the example below:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<target-server-address>/powershell/ -Credential $LiveCred -Authentication Kerberos
What is more, some options cannot be passed as New-PSSession arguments and must be specified as New-PSSessionOption, and only then passed together as a variable. See the Troubleshooting section below to see an example of using advanced option for a session.
If you cannot use a hostname or FQDN as a target for PowerShell connection and must use an IP address instead, see this article first.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Start the connection:
To disconnect, type:
Also, if you are logged in directly to an on-premises Exchange server and for some reason cannot run Exchange Management Shell, you can start Windows PowerShell and load the Exchange snap-in from there by executing the cmdlet below:
When connecting to an on-premises Exchange server, you might see the following error:
Connecting to remote server <ServerName> failed with the following error message : The user name or password is incorrect.
This error message is self-explanatory – you need to make sure that the entered credentials are correct. A similar problem occurs when connecting to Exchange Online. This time the error message is not that informative:
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 such a case you should also double-check if there aren't any spelling mistakes in the provided credentials.
If you encounter errors specified below (or similar), you will not be able to establish a remote connection using the method described earlier 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.
However, you can use the New-PSSessionOption cmdlet to ignore the validation of certificate-related operations:
$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
The above-mentioned errors will not appear and you should be able to create a new session.
- Connect to Exchange Online using remote PowerShell in MS TechNet