How to start remote PowerShell session to Exchange or Office 365

UPDATE: Post updated on August 2, 2016.

Powershell script.

One of many features of the PowerShell command line tool is its ability to connect with and manage the Exchange Server remotely. The procedure described below applies to the classic on-prem Exchange server and to the Office 365/Exchange Online version.

System requirements

The workstation used for remote PowerShell administration must meet the following requirements:

For Exchange 2016:

  1. Operating system: Windows 10, Windows 8.1 (Windows 8.1 requires Microsoft .NET Framework 4.5.2 or later)
  2. Windows Server 2012 R2 (this version requires Microsoft .NET Framework 4.5.2 or later), Windows Server 2012 (after installing .NET Framework 4.5.2 or later, install Windows Management Framework 4.0 as well)

For Exchange 2013/Exchange Online:

  1. Operating system: Windows 7 SP1 (or later), Windows 2008 R2 SP1 (or later).
  2. When using Windows 7 SP1 or Windows 2008 R2 SP1 you also need to install following modules:

Please note: With later versions of the operating system, there is no need to install the above modules.

Connecting to the on-premises Exchange 2016

To use PowerShell to connect to Exchange 2016:

  1. Start the PowerShell on the workstation.
  2. In the console, type the following command:
     $Credentials = Get-Credential

    In the resulting window, provide administrative credentials.

  3. The next step is to run this command:
     $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://< Exchange 2016 Mailbox server FQDN>/PowerShell/ -Authentication Kerberos -Credential $UserCredential

    Replace <Exchange 2016 Mailbox server FQDN> with the fully qualified domain name (FQDN) of Exchange 2016 you want to connect to.

  4. Then, enter this command:
     Import-PSSession $Session

    This will connect to the Exchange Server and import all necessary administration cmdlets.

You are now all set to go and manage your Exchange 2016 Server via PowerShell. Once you have finished, make sure the remote session is removed. To disconnect the session, use this command:

Remove-PSSession $Session

Connecting to the on-premises Exchange 2013

To start the remote PowerShell session with Exchange 2013:

  1. Start the PowerShell console on the workstation.
  2. In the console enter the following command:
    $Credentials = Get-Credential

    This brings up a new window where you need to enter your administrative credentials.

  3. In the console enter another command:
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<Exchange 2013 CAS FQDN>/PowerShell/ -Authentication Kerberos -Credential $Credentials

    Replace the <Exchange 2013 CAS FQDN> part with the address of your CAS role server.

  4. The next command is:
    Import-PSSession $Session

    It connects to the Exchange Server and imports all necessary server administration cmdlets.

You are now ready to manage your server with PowerShell.

When you finish your work close and remove the remote connection session with the following command:

Remove-PSSession $Session

Connecting to the Exchange Online and Office 365

The procedure is quite similar to the one described earlier:

  1. Start the PowerShell console on the workstation.
  2. In the console enter:
    $Credentials = Get-Credential

    and provide administrative credentials.

  3. Enter the following command:
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credentials -Authentication Basic –AllowRedirection

    In this case there is no need to provide your CAS server address as the online Microsoft services address is fixed.

  4. Enter the command:
    Import-PSSession $Session

    After the console downloads administrative cmdlets you are ready to work.

Similarly as before – once you finish working with the remote PowerShell session close it with the following command:

Remove-PSSession $Session

Resolving typical connection problems

Establishing the connection with the remote server using PowerShell is relatively easy. However, you need to check a couple of settings that might stop you when they are mis-configured:

  1. Make sure that you enter your administrative credentials properly.
  2. Check if your workstation allows running scripts. You can check the current policy by entering the following command in the PowerShell console:
     Get-ExecutionPolicy

    If the policy shows that running scripts on the workstation is restricted, you can enable it with the following command (answer “Yes” to any questions that appear):

     Set-ExecutionPolicy RemoteSigned
  3. Make sure that the TCP 80 port is open between your workstation and the target server.
  4. Check if you have the appropriate access rights (Remote Shell rights). To do so run the following command on the server:
     Get-User Me@mydomain.com | select Name,RemotePowershellEnabled

    If the user lacks these rights use the following command to grant them:

     Set-User Me@mydomain.com -RemotePowerShellEnabled $True

    Note: The Remote Shell right is by default enabled for all newly created users.

Remote PowerShell is a great feature when you need to manage a number of servers simultaneously from one location and perform tasks that can be easily scripted, e.g. creation of users during Exchange migration.

There are some jobs however, that cannot be easily or quickly performed using PowerShell. The data migration that was mentioned earlier is one of them. To streamline it, you can utilize a third party tool, such as CodeTwo Exchange Migration or CodeTwo Office 365 Migration that takes much of the administrative burden off your shoulders.

How to start remote PowerShell session to Exchange or Office 365 by

2 thoughts on “How to start remote PowerShell session to Exchange or Office 365


Leave a Reply

Your email address will not be published.

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>