Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / Exchange / Connect to Exchange Servers and Microsoft 365 with Remote PowerShell

May 24, 2021 ExchangeMicrosoft 365PowerShell

Connect to Exchange Servers and Microsoft 365 with Remote PowerShell

In this article we will show how to remotely connect to an on-premises Exchange Server or Microsoft 365 (Exchange Online) from the PowerShell console.

Contents:
  • How to Connect to Exchange Servers via Remote PowerShell (without EMS)?
  • Connect to Exchange Online (Microsoft 365) Using Remote PowerShell

How to Connect to Exchange Servers via Remote PowerShell (without EMS)?

You can use the Exchange Management Shell (EMS) cmdlets to manage your on-premises Exchange organization (Exchange Server 2010, 2013, 2016, or 2019). EMS is installed as a part of the Exchange Management Tools. If the Exchange Management Shell is not installed on your computer, you can connect to your Exchange server remotely and import the cmdlets from the Exchange host to your local PowerShell session.

Remote connections in the Exchange Server are established through a separate virtual IIS (Internet Information Services) directory called PowerShell. By default, Kerberos authentication is used, and WinRM is used for the communication.

Tip. Note that some EMS cmdlets are not fully supported in a remote PowerShell session. For example, Get-ExchangeCertificate. To use it, you will have to install Exchange Management Shell on your computer.

Before you start, make sure that your local PowerShell Execution policy allows you to run local PS scripts. The command below allows running local scripts for the current user.

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Open the PowerShell console on your computer and run the following command:

$UserCredential = Get-Credential

Enter the login and password of the account you are going to use to connect to Exchange.

get exchange admin credentials

Create a remote PowerShell session with your Exchange server:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mun-mbex1.woshub.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential

Note that http instead of https is used to access Web PowerShell.

Make sure that the session has been created and now is in an Opened state:

Get-PSSession

New-PSSession Microsoft.Exchange ConnectionUri

Import the remote PowerShell session into your local one:

Import-PSSession $Session

Import-PSSession from remote exchange server via powershell

Then you can use all Exchange management cmdlets in your local PowerShell session.

Remember to properly end the remote PowerShell sessions. If you just close the Windows PowerShell console without disconnecting your session, you may exceed the limit of remote PowerShell sessions.

To end up your session, run the command below:

Remove-PSSession $Session

Make sure that no running remote PowerShell sessions are left:

Get-PSSession

You can use a PowerShell profile to automatically import PowerShell cmdlets from remote Exchange into your session.

Create a profile file:

New-Item -Path $profile -ItemType file -force

Open Microsoft.PowerShell_Profile.ps1 with Notepad:

notepad $profile

Add commands to the file to connect to Exchange and import cmdlets from a remote session to a local PowerShell session:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mun-mbex1.woshub.com/PowerShell/ -Authentication Kerberos -Credential (Get-Credential)
Import-PSSession $Session

using powershell profile to connect to remote exchange session

Connect to Exchange Online (Microsoft 365) Using Remote PowerShell

In the same way, you can connect to your Exchange Online (Microsoft 365) tenant to manage mailboxes, conference rooms, distribution lists and other Microsoft 365 settings.

Let’s learn how to connect to Exchange Online remotely from the PowerShell console using Basic Authentication without installing Microsoft Exchange Online PowerShell module (EXO/ EXOv2) .

Allow local PS scripts to run:
Set-ExecutionPolicy RemoteSigned

Get your Exchange Online administrator credentials:

$UserCredential = Get-Credential

This Azure user must first be allowed to connect remotely via PowerShell:

Set-User -Identity maxbak@woshub.com -RemotePowerShellEnabled $true

Then you can establish a remote PowerShell session with Microsoft 365:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic –AllowRedirection

If the Multi-Factor Authentication is enabled for your account (Microsoft recommends using MFA for all administrator accounts), the following error appears when trying to connect using New-PSSession:

New-PSSession : [outlook.office365.com] Connecting to remote server outlook.office365.com failed with the following error message : Access is denied.
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed

Then you will have to install the Exchange Online PowerShell V2 (EXO V2) module:

Install-Module ExchangeOnlineManagement

In this case, the following cmdlet is used to connect to Exchange Online:

Connect-ExchangeOnline -UserPrincipalName maxbak@woshub.com -ShowProgress $true

Or you can disable MFA for the account:

Set-MsolUser -UserPrincipalName maxbak@woshub.com -StrongAuthenticationRequirements @()

Then import the remote session to your console:

Import-PSSession $Session

Connecting to Microsoft 365 with remote PowerShell

The maximum number of remote PowerShell connections to Exchange Online is limited to three sessions. If you exceed the limit, the following error appears:

Fail to create a runspace because you have exceeded the maximum number of connections allowed.


You can now manage your Microsoft 365 mailboxes.

To end all remote PowerShell sessions, run this command:

Get-PSSession | Remove-PSSession

0 comment
4
Facebook Twitter Google + Pinterest
previous post
Adding a Sound Card to a Virtual Machine on VMWare ESXi
next post
How to Disable Windows Error Reporting and Clear WER\ReportQueue Folder on Windows?

Related Reading

Using Previous Command History in PowerShell Console

January 31, 2023

How to Install the PowerShell Active Directory Module...

January 31, 2023

Finding Duplicate E-mail (SMTP) Addresses in Exchange

January 27, 2023

How to Disable or Uninstall Internet Explorer (IE)...

January 26, 2023

How to Delete Old User Profiles in Windows?

January 25, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Configure User’s Folder Redirection with Group Policy

    February 3, 2023
  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Outlook Keeps Asking for Password on Windows
  • How to Manually Configure Exchange or Microsoft 365 Account in Outlook 365/2019/2016?
  • Whitelist Domains and Email Addresses on Exchange Server and Microsoft 365
  • How to Cleanup, Truncate or Move Log Files in Exchange Server 2013/2016/2019?
  • Moving Exchange Mailboxes to Different Database
  • Fix: Microsoft Outlook Search Not Working on Windows 10/11
  • Configure Auto-Reply (Out of Office) Message in Exchange and Microsoft 365
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top