Connecting to Exchange Online by using Remote PowerShell
The Power of PowerShell – Article Series
In the former article, we review the steps that we need to use for creating remote PowerShell to Office 365 (Windows Azure Active Directory).
The process of connecting to Exchange Online using remote PowerShell is a bit different because of the following charters:
Using Copy & Paste option.
When we manually type a PowerShell command, the command may include a syntax or spelling errors.
To avoid these scenarios, we can use the Copy/Paste option. For example: when a PowerShell command displayed in the article, you can copy the command. Open the PowerShell console, right click on the console and chose the option: “Paste”.
Step 1 – Run the PowerShell console using administrative rights (Run as Administrator).
To change the PowerShell Execution Policy, we need to run the Windows PowerShell by using the option: Run as administrator
Right click on the PowerShell Console icon and choose the option: Run as administrator.
Update the default PowerShell Execution Policy
By default, the PowerShell console will not allow us to run a script.
We can enable script execution by changing the default PowerShell Execution Policy to Unrestricted. (The default mode is: Restricted).
To change the Execution policy open the PowerShell console and type the command:
Step 2 – Creating remote session to Exchange Online
For creating the remote connection, we will need to use a set of three PowerShell commands. Each command performs a different task and includes variable that serves as a “container” for information such as user credentials, URL etc.
- The first command will cause the PowerShell console to display a “pop out” Authentication window. After entering the required credential, the information will be “delivered” to the “next” command.
- The second command includes the “Target URL” of the Exchange Online server who will accept our authentication request.
The URL that we use, function as a “focal point” for all Office 365 customers. Based on the credentials that we provide (that include our domain name), the request will be redirected to the specific Exchange Online server who hosts our organization mailboxes.
- The thread command will download the Exchange Online cmdlets to the local host (the desktop that we use for creating the remote PowerShell session.
Copy the following “Block” of PowerShell commands and paste it on the PowerShell console:
PowerShell command Example
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
A pop out windows will appear. Type your credentials by using the UPN (user Principal name) format. For example [email protected]
After executing the command, the following message appears:
“WARNING: Your connection has been redirected to the following URI: “https://pod51013psh.outlook.com/PowerShell-LiveID?PSVersion=2.0 ” WARNING: Your connection has been redirected to the following URI: https://dbxprd0312psh.outlook.com/PowerShell-LiveID?PSVersion=2.0 ”
The URL that we use the command: “https://outlook.office365.com/powershell-liveid” serves as a “general address”.
Using the credential information that was provided in the former step, the remote session will be redirected to the specific Exchange Online server who host the mailbox of the user that creates the session (in our example the Exchange Online server address is: dbxprd0312psh.outlook.com)
On the last screen, we can see the process of the import Exchange Online cmdlets
Congratulations! You are now connected to the Exchange Online.
Step 3: Verifying the remote PowerShell connection
Q: How can we know if the remote session was successfully created?
A: To verify that we are connected, we type the name of Exchange Online cmdlets such as –
This command will display a list all the mailboxes that were created.
In the screenshot, you can see the list of Exchange Online mailboxes.
Step 4: Disconnecting the Remote PowerShell session
Remote PowerShell session to Exchange Online have built-in limitations:
- The extinction of the remote PowerShell session – the maximum time for inactive (idle) remote PowerShell session is 15 minutes (this limitation is quite flexible because I manage to continue a PowerShell session that was idle for more than 15 minutes).
- The amount of data that can be transferred to the remote PowerShell “Channel” (throttling) – Exchange Online environment includes a predefined restriction of the amount of data, which can be transferred at one time. This limitation is implemented by using “throttling Policy”.
- The number of concurrent remote PowerShell session – the maximum number of concurrent PowerShell session that we can use simultaneously is: three
The best practice is to disconnect a remote PowerShell session after we have finished using the required management tasks.
For disconnecting the remote PowerShell session uses the following command:
The PowerShell indication for the successful execution of commands is a bit poor.
The fact that we didn’t receive an error message implies that the operation of “disconnecting the remote PowerShell Session” was successfully completed.
Q: How can know if the remote session was successfully disconnected?
A: To verify that the remote session was successfully disconnected, you can try to type Exchange Online PowerShell cmdlets such as
The following error will appear:
“The term ‘get-mailbox’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.”
This is actually a good sign (the error was expected). After disconnecting the PowerShell session, the PowerShell cmdlets for Exchange Online are no longer available.
Part 4: working with PowerShell cmdlets – basic introduction
In former articles we review the way for creating a remote PowerShell session to the Office 365 and Exchange Online.
In The last part, we will review some basic PowerShell commands that will help us to get familiar with the cmdlets and the PowerShell command syntax.
Display list of the PowerShell cmdlets
Office 365 and Exchange Online include a different set of cmdlets
1. Office 365 cmdlets
To view a list of available Office 365 cmdlets, use following PowerShell command:
Get-command -module msOnline
The name of the Office 365 cmdlets include the string “msol” (Microsoft Online). We can use this naming convention for displaying a list of Office 365 cmdlets, so additional option is to use the command: Get-command *msol*
2. Exchange Online cmdlets
Displaying a list of Exchange Online cmdlets is a bit different from the way that we use for displaying the Office 365 cmdlets. When we create the remote PowerShell session to Exchange Online all the PowerShell cmdlets are dynamically downloaded to the local user Desktop.
In addition the Exchange Online cmdlets don’t have a specific name convention like the Office 365 cmdlets. To view a list of available Exchange Online cmdlets, use following PowerShell command: Get-Command –Module tmp*
Using the AutoComplete feature
The PowerShell console has a very useful feature named: AutoComplete as the name implies, we can use this feature for automatic compilation of PowerShell commands by typing the first letters of command name is using the TAB key.
The AutoComplete option enables us to work quickly and efficiently and in addition help us to avoid from the need to memorize the complete name of the PowerShell command (and avoid from typing syntax mistakes).
Example 1: AutoComplete with Remote PowerShell session to Exchange Online
We want to use the AutoComplete option for the command: Get-Mailbox
Type the command Get-M and hit the TAB key.
You will see that the command is completed automatically to: Get-Mailbox
Example 2: AutoComplete with Remote PowerShell session to Office 365
We want to use the AutoComplete option for the command:Get-MsolUser
Type the command Get-Msoland hit the TAB key.
You will see that the command is completed automatically to: Get-MsolAccountSku.
This is not the command that we wanted.
The Office 365 cmdlets set include a couple of cmdlets that begin with the name “Get-Msol“.
Using the TAB key, we can browse through all the cmdlets until we will reach the required cmdlets. In our example, we will need to hit the TAB key couple of time until the “right” command (Get-MsolUser) will be displayed.
An additional option that we could use for helping the PowerShell console to “understand” better what we need is -typing the command Get-Msolu, the addition of the “u” letter will get us to the exact command that we need.
Using PowerShell help
Using the PowerShell is based on a specific syntax that we need to use. One of the distinct advantages of the PowerShell environment is the good help reference for each of the PowerShell cmdlets.
To get information about specific cmdlets when can use the following command:
For example: to learn about the way we use the Get-MsolUser cmdlets type the following command Get-Help Get-MsolUser
If you need more information or examples about how to use the command, we can add the “Detailed” option: Get-Help Get-MsolUser -Detailed
An additional option that we can use is displaying help information in HTML format.
We can use this nice trick by adding the option “Online”.
For example: Get-help Get-mailbox –Online
In this three parts article series, we learn about:
- Why do we need to use PowerShell for managing the Office 365 cloud environment.
- How to use remote PowerShell for managing Office 365 and Exchange Online.
- How to use PowerShell Basic introduction: using PowerShell cmdlets, using the AutoComplete feature and how to get information about the cmdlets that available for us.
I hope you enjoy reading and, May the force of PowerShell will be with you!
We really want to know what you think about the article