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.
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.
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.
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
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
Import the remote PowerShell session into your local one:
Import-PSSession $Session
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
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
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
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
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