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
3
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

Checking Windows Activation Status on Active Directory Computers

June 27, 2022

Configuring Multiple VLAN Interfaces on Windows

June 24, 2022

How to Disable or Enable USB Drives in...

June 24, 2022

FAQ: Licensing Microsoft Exchange Server 2019/2016

June 14, 2022

How to Approve and Deploy Updates in WSUS?

June 14, 2022

Leave a Comment Cancel Reply

Categories

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

Recent Posts

  • How to Deploy Windows 10 (11) with PXE Network Boot?

    June 27, 2022
  • Checking Windows Activation Status on Active Directory Computers

    June 27, 2022
  • Configuring Multiple VLAN Interfaces on Windows

    June 24, 2022
  • How to Disable or Enable USB Drives in Windows using Group Policy?

    June 24, 2022
  • Adding Domain Users to the Local Administrators Group in Windows

    June 23, 2022
  • Viewing a Remote User’s Desktop Session with Shadow Mode in Windows

    June 23, 2022
  • How to Create a Wi-Fi Hotspot on your Windows PC?

    June 23, 2022
  • Configuring SSH Public Key Authentication on Windows

    June 15, 2022
  • How to Run a Program as a Different User (RunAs) in Windows?

    June 15, 2022
  • FAQ: Licensing Microsoft Exchange Server 2019/2016

    June 14, 2022

Follow us

woshub.com

ad

  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Outlook Keeps Asking for Password on Windows
  • 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?
  • Fix: Microsoft Outlook Search Not Working on Windows 10/11
  • Moving Exchange Mailboxes to Different Database
  • How to Delete or Rename Default Mailbox Database in Exchange Server?
  • Search and Delete Emails from User Mailboxes on Exchange Server (Microsoft 365) with PowerShell
Footer Logo

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


Back To Top