PowerShell: ExchangeOnlineManagement

In my previous post, I talked about setting up DKIM DNS for Office 365. However, you need PowerShell to log into the ExchangeOnlineManagement system.

In simplest forms, PowerShell is a cross-platform shell and scripting language that allows people to manage tasks from the command line and automate thousands of activities. Moreover, it’s a language built on Microsoft’s .NET Framework that makes our lives easier.

Wikipedia describes its functionality as:

Administrative tasks are performed by cmdlets (pronounced command-lets). Cmdlets are are specialized .NET classes implementing a particular operation. They access data in different data stores, like the file system or registry, which are made available to PowerShell via providers. Third-party developers can add cmdlets and providers to PowerShell. Cmdlets are used by scripts, which can be packaged into modules.

In this post, I’ll do a short breakdown of how I did it. In addition for more detailed information, visit the Microsoft documentation.

Install PowerShell

Since I work on a Mac, I usually install software through Homebrew. PowerShell is no different.

Fire up your terminal and install it.

$ brew install --cask powershell
==> Downloading https://github.com/PowerShell/PowerShell/releases/download/v7.1.3/powershell-7.1.3-osx-x64.pkg
==> Downloading from https://github-releases.githubusercontent.com/49609581/8aba0580-827a-11eb-86c1-20ac8f0d9c53?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Crede
######################################################################## 100.0%
==> Installing Cask powershell
==> Running installer for powershell; your password may be necessary.
Package installers may write to any location; options such as `--appdir` are ignored.
Password: ***
installer: Package name is PowerShell - 7.1.3
installer: Installing at base path /
installer: The install was successful.
🍺  powershell was successfully installed!

Now you have installed it, go fire it up!

Use ExchangeOnlineManagement

Startup PowerShell and import the ExchangeOnlineManagement module

$ pwsh
PowerShell 7.1.3
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /Users/michael> Import-Module ExchangeOnlineManagement
Import-Module: The specified module 'ExchangeOnlineManagement' was not loaded because no valid module file was found in any module directory.

It doesn’t come with a pre-installed set of modules for the Microsoft Office stack. So you actually have to separately install the ExhangeOnlineManagement module.

PS /Users/michael>  Install-Module -Name ExchangeOnlineManagement

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the
Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y

Now that you have installed the ExhangeOnlineManagement module, you can log in. Unfortunately, an error occurred.

PS /Users/michael> Import-Module ExchangeOnlineManagement                                                                                                    PS /Users/michael> Connect-ExchangeOnline -UserPrincipalName michael@enperas.com                                                                                                                                                                                                                                          ----------------------------------------------------------------------------                                                                                 The module allows access to all existing remote PowerShell (V1) cmdlets in addition to the 9 new, faster, and more reliable cmdlets.

|--------------------------------------------------------------------------|
| Old Cmdlets | New/Reliable/Faster Cmdlets |
|--------------------------------------------------------------------------|
| Get-CASMailbox | Get-EXOCASMailbox |
| Get-Mailbox | Get-EXOMailbox |
| Get-MailboxFolderPermission | Get-EXOMailboxFolderPermission |
| Get-MailboxFolderStatistics | Get-EXOMailboxFolderStatistics |
| Get-MailboxPermission | Get-EXOMailboxPermission |
| Get-MailboxStatistics | Get-EXOMailboxStatistics |
| Get-MobileDeviceStatistics | Get-EXOMobileDeviceStatistics |
| Get-Recipient | Get-EXORecipient |
| Get-RecipientPermission | Get-EXORecipientPermission |
|--------------------------------------------------------------------------|

To get additional information, run: Get-Help Connect-ExchangeOnline or check https://aka.ms/exops-docs

Send your product improvement suggestions and feedback to exocmdletpreview@service.microsoft.com. For issues related to the module, contact Microsoft support. Don't use the feedback alias for problems or support issues.
----------------------------------------------------------------------------

Exception: /Users/michael/.local/share/powershell/Modules/ExchangeOnlineManagement/2.0.5/netCore/ExchangeOnlineManagement.psm1:475
Line |
475 | … $PSSession = New-ExoPSSession -ExchangeEnvironmentName $E …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| This parameter set requires WSMan, and no supported WSMan client library was found. WSMan is either not installed or unavailable for this
| system.

During my search on the net, I learned you have to install the PSWSMan module and WSMAN.

Exit and return to your terminal prompt. The reason to install the modules outside of PowerShell is that you will need to restart it anyway in order for WSMan to take effect.

$ pwsh -Command 'Install-Module -Name PSWSMan'
$ sudo pwsh -Command 'Install-WSMan'
Password:
WARNING: WSMan libs have been installed, please restart your PowerShell session to enable it in PowerShell

Now you should have all dependencies. After that, open PowerShell again and log in ExhangeOnline.

$ pwsh
PowerShell 7.1.3
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /Users/michael> Import-Module ExchangeOnlineManagement
PS /Users/michael> Connect-ExchangeOnline -UserPrincipalName <your-account@email.com>

In conclusion, using PowerShell isn’t always as straightforward. There’s a multitude of modules that you need to be aware of, which isn’t always clear.