Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • 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 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

April 27, 2022 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 [email protected] -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 [email protected] -ShowProgress $true

Or you can disable MFA for the account:

Set-MsolUser -UserPrincipalName [email protected] -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

How to Connect VPN Before Windows Logon

November 14, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

October 15, 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

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
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
  • Moving Exchange Mailboxes to Different Database
  • FAQ: Licensing Microsoft Exchange Server 2019/2016
  • How to Cleanup, Truncate or Move Log Files in Exchange Server 2013/2016/2019?
  • Search and Delete Emails from User Mailboxes on Exchange Server (Microsoft 365) with PowerShell
Footer Logo

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


Back To Top