Executing Exchange Online PowerShell Command Through SharePoint Online PowerShell

For Office 365 administrators, there is a list of Exchange Online PowerShell cmdlets available. We can execute these cmdlets through SharePoint Online Management Shell or Windows PowerShell by creating a remote PowerShell session with Exchange Online. In this article, I’ll explain how to create a remote PowerShell session to Exchange Online and then execute Exchange Online PowerShell cmdlets.

Administrators can execute these PowerShell commands from their local computer using Remote PowerShell. The administrator can manage complete Exchange Online from the command line. First of all, we will create a remote PowerShell session and then see how to fetch the entire list of Exchange online commands which administrator can execute.  

Here, I will explain this for SharePoint Online. So, the very first step is to install SharePoint Online Management Shell. For installing SharePoint Online Management Shell, please have a look on my old blog “SharePoint Online / Office 365: SharePoint Online Management Shell Installation”.

Once SharePoint Online Management Shell is ready, the next step is to make SharePoint Online Management Shell ready for connecting Office 365. For connecting to Office 365 the first time, we need to install the following two softwares:

  1. 64-bit version of the Microsoft Online Services Sign-in Assistant - Microsoft Online Services Sign-in Assistant for IT Professionals RTW.
  2. 64-bit version of the Microsoft Azure Active Directory module for Windows PowerShell

Please check https://technet.microsoft.com/en-us/library/dn975125.aspx for more details.

Once the required softwares are ready, we are ready to connect to Exchange Online.

Step 1

Get credentials using “Get-Credential” cmdlet as

$credential = Get-Credential

Once we execute this cmdlet, “Windows PowerShell credential” dialog box will appear.

Windows PowerShell

Figure 1: Windows PowerShell credential request? Dialog

Enter your Office 365 account username and password and press OK. Once we have credentials, the next step is to create the PowerShell session PSSession using “New-PSSession” as

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $ credential -Authentication Basic -AllowRedirection

Once we have session object, we import the session using “Import-PSSession” as

Import-PSSession $Session

Imports all exchange online commands such as cmdlets, functions, and aliases from PSSession into the current SharePoint online management session. The result will look like -

Windows PowerShell

Figure 2: result of Import-PSSession cmdlet

We can just print $Session object and see the details.

Windows PowerShell

Figure 3: Session object details after importing

Since we are ready to execute Exchange Online cmdlets, we will see how to get all list of cmdlets, using “Get-Module” and “Get-Command” cmdlets as, 

$mods=Get-Module | where {$_.Name -like "tmp_*"}; $modName=$mods.Name

Here, we are filtering the results based on a module name that contains “tmp_ *. We used “tmp_ *”. If we see the result of Import-PSSession cmdlet, we got the module name “{tmp_q34a00sh.o0j}” and then, will execute “Get-Command” cmdlet.

Get-Command -Module $modName | More

Windows PowerShell

Figure 4: All Exchange Online commands

References

Thanks!


Similar Articles