PowerShell Basics: Want to Use Exchange Online in Azure Cloud PowerShell?

Since rollout in Exchange 2007, PowerShell and Exchange have been partners in helping administrators everywhere manage their environments. Whether on-premises or in the cloud, PowerShell is a powerful tool for working with Exchange. Now the cmdlets for managing Exchange Online are available in Azure Cloud Shell.

In this post, I'll show you access the Exchange Online module easily, and begin working with your Exchange Online resources in Azure Cloud Shell.

Connecting to Exchange Online

To start, we need to establish a connection to Exchange Online in Azure Cloud Shell with the Connect-EXOPSSession cmdlet. This cmdlet connects to Exchange Online with managed identity for Azure (MSI) using your current login.

Before actually connecting to Exchange Online, let's take a look at what this cmdlet has to offer using get-help.

PS Azure:> get-help Connect-EXOPSSession


    To connect in other Office 365 offerings, use the following settings:
     - Office 365 operated by 21Vianet: -ConnectionURI https://partner.outlook.cn/PowerShell-LiveID
    -AzureADAuthorizationEndpointUri https://login.chinacloudapi.cn/common
     - Office 365 Germany: -ConnectionURI https://outlook.office.de/PowerShell-LiveID -AzureADAuthorizationEndpointUri

    - This PowerShell module allows you to connect to Exchange Online service.

    - To connect to the Exchange Online Service use "Connect-EXOPSSession" cmdlet

    - Uses MSI to login as yourself, and Device login to other tenant as syndicated partner

    - EnableEXOTelemetry To collect telemetry on Exchange cmdlets. Default value is False.

    - DoLogErrorMessage Switch to enable/disable error message logging in telemetry file. Default value is True.

    - PSSessionOption accept object created using New-PSSessionOption

    Connect-EXOPSSession [[-ConnectionUri] ] [[-AzureADAuthorizationEndpointUri] ] [[-PSSessionOption]
    ] [-BypassMailboxAnchoring] []

    This PowerShell module allows you to connect to Exchange Online service


    To see the examples, type: "get-help Connect-EXOPSSession -examples".
    For more information, type: "get-help Connect-EXOPSSession -detailed".
    For technical information, type: "get-help Connect-EXOPSSession -full".
    For online help, type: "get-help Connect-EXOPSSession -online"

So we see that we have a few optional parameters that can be used. Generally, you'll simply run the command as is since you are already logged into Azure through Cloud Shell, and MSI will take care of the for you.

To connect to Exchange Online, simply enter the following:

PS Azure:> Connect-EXOPSSession

This process takes a few moments as the connection is made, and the Exchange Online cmdlets are temporarily loaded into the shell as shortcut to the actual cmdlets being run inside of Exchange Online. The process uses implicit remoting, a technique for accessing a PowerShell module from a remote system without permanently installing the module based on Import-PSSession. For more information on implicit remoting and Import-PSSession, see this doc.

Finding the Exchange Commands

Normally, cmdlets available within PowerShell in Cloud Shell would be part of an installed and loaded module. However, when you search the modules in Cloud Shell, you won't find any modules explicitly calling out Exchange. That is because implicit remoting creates a temporary module containing the imported cmdlets shortcuts to the actual cmdlets running in Exchange Online. So you are not actually running the commands in your shell, but on the remote system's shell environment. It's easy to find as it will be the only module name starting with tmp_ as seen in the listing below:

PS Azure:> get-module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.5.2      Az.Accounts                         {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Connect-AzAccou…
Script     2.0.0      Az.Compute                          {Add-AzContainerServiceAgentPoolProfile, Add-AzImageDataDisk, Add-AzV…
Script     1.8.0      Az.Network                          {Add-AzApplicationGatewayAuthenticationCertificate, Add-AzApplication…
Script     1.3.1      Az.Resources                        {Add-AzADGroupMember, Export-AzResourceGroup, Get-AzADAppCredential, …
Script     1.3.0      Az.Storage                          {Add-AzRmStorageContainerLegalHold, Add-AzStorageAccountManagementPol…
Script    AzureAD.Standard.Preview            {Add-AzureADAdministrativeUnitMember, Add-AzureADApplicationOwner, Ad…
Script     0.9.3      AzurePSDrive
Script     17.0.2985… EXOPSSessionConnector               Connect-EXOPSSession
Manifest    Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty…}
Manifest    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object…}
Script     0.9.3      PSCloudShellUtility                 {Disable-AzVMPSRemoting, Dismount-CloudDrive, Enable-AzVMPSRemoting, …
Script     2.0.0      PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKey…
Binary     0.8.1      SHiPS
Script     1.0        tmp_fc3yzdtj.qan                    {Add-MailboxFolderPermission, Approve-ElevatedAccessRequest, Clear-Ac…

Notice under exported commands, the first command is Add-MailboxFolderPermission so we know that is the module we want to work with.

Now to see the commands available within the module, we use get-module and select out the commands as seen here:

PS Azure:> get-module tmp* | Select -ExpandProperty ExportedCommands

Key                                     Value
---                                     -----
Add-MailboxFolderPermission             Add-MailboxFolderPermission
Approve-ElevatedAccessRequest           Approve-ElevatedAccessRequest
Clear-ActiveSyncDevice                  Clear-ActiveSyncDevice
Clear-MobileDevice                      Clear-MobileDevice
Clear-TextMessagingAccount              Clear-TextMessagingAccount
Compare-TextMessagingVerificationCode   Compare-TextMessagingVerificationCode
Deny-ElevatedAccessRequest              Deny-ElevatedAccessRequest
Disable-App                             Disable-App
Disable-InboxRule                       Disable-InboxRule
Disable-SweepRule                       Disable-SweepRule
Disable-UMCallAnsweringRule             Disable-UMCallAnsweringRule
Enable-App                              Enable-App
Enable-InboxRule                        Enable-InboxRule
Enable-SweepRule                        Enable-SweepRule
Enable-UMCallAnsweringRule              Enable-UMCallAnsweringRule
Get-ActiveSyncDevice                    Get-ActiveSyncDevice
Get-ActiveSyncDeviceStatistics          Get-ActiveSyncDeviceStatistics

You'll note I used tmp* for the module name since there is only one temporary module. You would need to call the full name should you have other temporary modules in your session.

Now you have access to the Exchange Online cmdlets. At the time of this blog, that is 131 cmdlets available in Cloud Shell. This may change over time as more cmdlets and other services are added.

Working with Exchange Online Cmdlets

Once connected, you can begin using the cmdlets as you normally would. For example, you can view the non-exchange information for a user object in your Exchange organization using get-user.

View user objects in Azure Cloud Shell using Get-User

Looking for mailbox-specific information? Try get-mailbox.

Listing a mailbox in Azure Cloud Shell using Get-Mailbox

And like all commands in PowerShell, you can use the pipeline and filtering techniques to find more specific information.

[box type=”info”] Note: Because these cmdlets are temporary using implicit remoting, the help files are not available within the shell. So if you need to see the help file for a command, you'll need to use a local install of the Exchange PowerShell tools or you can view the Exchange PowerShell cmdlets at docs.microsoft.com[/box]

Was this post helpful? Do you have other Questions/Comments? Would you like to see other Office 365 services available in Cloud Shell? Let me know in the comments below!

If you want more information on commands from this blog, check out the docs below:


This article was originally published by Microsoft's Azure Blog. You can find the original article here.