CodeTwo Base.title

How to connect to Exchange Server via PowerShell

Problem:

How to set up remote session to Exchange Server via PowerShell

Applies to:

Exchange Online (Office 365) and Exchange Server 2010, 2013, 2016.

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 please change it to RemoteSigned or Unrestricted (this might need to be executed from PowerShell in "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 above is not universal i.e. depending on your target environment configuration you may be required to specify 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
    • 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 in:

    Remove-PSSession $Session

Also, if are logged in directly to the Exchange Server On-Premises and for some reason cannot run the ems Exchange Management Shell, you can start ps Windows Powershell and load Exchange's snapin from there by executing the below cmdlet:

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

See also:

Our Clients:
Unicef
Facebook
Shell
T-Systems
Loreal
Casio
UPS Israel
Oford University
Mitsubishi Motors
Toshiba TEC UK Imaging Systems Ltd
Illinois Institute of Technology
MAN Diesel
McDonalds India
Skoda Auto
Bank of Israel
Fujifilm
China Mobile
Santander
Samsung SDI
Skanska
Generali
Telmex
Toyota Tsusho
BECHTEL
Ricoh
BAE SYSTEMS
Federação Portuguesa de Futebol
Credit Agricole
HYUNDAI
Rothschild
Toyota Boshoku
Oriflame Romania
ING
Ikea
Nordea

Partners, certificates & awards