CodeTwo Base.title

How to connect to Exchange Server via PowerShell

Problem:

How to set up remote session to Exchange Server via PowerShell.

Solution:

  1. Check the requirements for:
  2. Run ps Windows PowerShell.
  3. Check your Execution policy settings:

    Get-ExecutionPolicy
  4. 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):

    Set-ExecutionPolicy RemoteSigned
  5. Provide the target server administrator credentials:

    $LiveCred = Get-Credential
  6. Configure the connection:

    • In the case of connecting to Exchange Server 2010, 2013, 2016:

      $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://<target-server-address>/powershell/ -Credential $LiveCred

      Be advised, the method above is not universal, i.e. 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. 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 example below.

      $SessionOpt = New-PSSessionOption -SkipCACheck:$true -SkipCNCheck:$true -SkipRevocationCheck:$true
      
      $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ex-mbx.domain1.example/powershell/ -Credential $LiveCred -SessionOption $SessionOpt -Authentication Kerberos

      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.

    • In the case of connecting to Exchange Online (Office 365):

      $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
  7. Start the connection:

    Import-PSSession $Session
  8. To disconnect, type:

    Remove-PSSession $Session

Also, if you are logged in directly to the on-premises Exchange Server and for some reason cannot run the ems Exchange Management Shell, you can start ps Windows PowerShell and load the Exchange snap-in from there by executing the cmdlet below:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

See also:

Our Clients: