Simple start with Microsoft 365 and PowerShell

Office for Mere Mortals
Your beginners guide to the secrets of Microsoft Office
Invalid email address
Tips and help for Word, Excel, PowerPoint and Outlook from Microsoft Office experts.  Give it a try. You can unsubscribe at any time.  Office for Mere Mortals has been running for over 20 years, we've never, ever revealed or sold subscriber details.  Privacy policy

How to run administration commands via PowerShell for Microsoft 365 hosting and especially Microsoft 365 Exchange Server or mailbox hosting.

Microsoft 365 customers will often see instructions like this to configure their Exchange Server mailboxes using PowerShell. 

Set-OrganizationConfig -AllowPlusAddressInRecipients $true

Looks simple, but that command alone won’t work in PowerShell because there are add-ins to be installed and login necessary.

That command enables Plus Addressing in Exchange Server. It’s typical of an option only available via PowerShell, not the admin web page.

PowerShell also let administrators control multiple mailboxes more easily.  PowerShell is a formidable tool but hardly user friendly.

Unfortunately, Microsoft seems to assume all their customers are born knowing the intricacies of PowerShell and how it works with Microsoft 365.  Redmond seems happy to publish long complicated command lines without any context.  It’s especially hard for small self-managing organizations.

Setting up Exchange Server access isn’t simple and made worse with truly awful error messages.  The warnings and errors are so unhelpful they would have been considered inadequate in the 1990’s, let alone now.

Here’s how to setup PowerShell ‘from scratch’ so you can administer Microsoft 365 hosted mailboxes. 

For this article, we’ll show how to setup admin access to a Microsoft 365 hosted Exchange Server organization. It applies to most Microsoft 365 hosting worldwide but there are special cases with slightly different commands or  21Vianet (China), Office 365 Germany plus US Government DoD and GCC High.

Before you start

You’ll need two things to start.

PowerShell – we’ll use PowerShell that comes with all Windows 10 64-bit systems.  PowerShell 5.1 to 6 only.   If you have the latest Windows release, you’ll have Powershell. 

Admin access – email address, password and, if necessary, Multi-factor authentication for administrators access to the Microsoft 365 organization.

Start PowerShell

Open up PowerShell from the Start Menu. It needs ‘Run as Administrator’ access to install some modules.

image 32 - Simple start with Microsoft 365 and PowerShell

Just like the Command Prompt (DOS Box), PowerShell lets you recall commands using the Up / Down arrow keys.  Edit a command by up arrow to recall it, then retype as required.  Copy/Paste works to enter commands.

See “Allow script access aka Execution Policy” below if you strike a script access error while following these steps.

Install AzureAD

Firstly, install Azure Active Directory module.  All Microsoft 365 hosted organizations use Active Directory, whether they realize it or not!

Install-Module -Name AzureAD

If you see an ‘untrusted repository’ warning or other prompts, allow the install to proceed, type Y

image 33 473x150 - Simple start with Microsoft 365 and PowerShell

Then connect to Active Directory

Connect-AzureAD

Then you have to login with the familiar Microsoft account login which pops up over the PowerShell box.  Login as usual including multi-factor authentication.

image 34 421x296 - Simple start with Microsoft 365 and PowerShell

If you connect OK, the account is listed like this …

image 35 473x52 - Simple start with Microsoft 365 and PowerShell

Install Exchange Server

Now install the Exchange Server module to manage Microsoft 365 managed mailboxes.

Import-Module ExchangeOnlineManagement

Though there seems to be a problem with that command at the moment.  Instead try this:

Install-Module -Name ExchangeOnlineManagement -RequiredVersion 2.0.3

If there’s no error message, the module has installed OK.

image 36 473x90 - Simple start with Microsoft 365 and PowerShell

If you get an error : The specified module ‘ExchangeOnlineManagement’ was not loaded because no valid module file was found in any module directory.”

image 37 473x73 - Simple start with Microsoft 365 and PowerShell

See the ‘Allow script access’ section below or try the alternative install command above.

Now connect to Exchange Server.  This is the official syntax.

Connect-ExchangeOnline -UserPrincipalName <UPN> -ShowProgress $true

<UPN> means User Principal Name or as anyone else would say administrators ‘email address’ like this:

Connect-ExchangeOnline -UserPrincipalName [email protected] -ShowProgress $true
image 38 473x200 - Simple start with Microsoft 365 and PowerShell

There will be another login required if you’re using a different login to the Azure login.

Wait while various modules are loaded and connected.

Now you can control ExchangeServer from PowerShell. 

A simple command to test if it’s working is:  Get-Mailbox

That shows the basic details of the current mailbox account.

Later, on the same machine ….

Once you’ve installed the AzureAD and ExchangeOnline modules, they apply to any future PowerShell sessions on that computer.

If you open PowerShell later on the same machine, all you have to do is login to ExchangeOnline (or AzureAD)

Make sure Execution Policy is set to RemoteSigned or Unrestricted.

Use the same login command as above:

Connect-ExchangeOnline -UserPrincipalName [email protected] -ShowProgress $true
image 39 473x240 - Simple start with Microsoft 365 and PowerShell

Login as prompted and you’re good to go!

For regular use, we save these two lines separately then paste both into PowerShell.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Connect-ExchangeOnline -UserPrincipalName [email protected] -ShowProgress $true

Or use Up arrow to recall them from earlier sessions.

Allow script access aka Execution Policy

You might strike script errors due to PowerShell’s security policy which limits script execution.

If you get that error, change the ExecutionPolicy for PowerShell on that computer.   There are different policies depending on the scope, but for our needs just focus on the LocalMachine scope (the default scope).

There are various policy levels, for our purposes there are two possibilities: RemoteSigned and Unrestricted.

Use this command to allow external scripts that are digitally signed.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
image 40 473x61 - Simple start with Microsoft 365 and PowerShell

Failing that use this:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

It allows unsigned scripts and is a potential security risk. Only use if you’re sure the script is OK. This is the default for PowerShell on non-Windows computers. 

To see the current ExecutionPolicy use the command

Get-ExecutionPolicy 

Or

Get-ExecutionPolicy -List

This shows the ExecutionPolicy for all scopes, including LocalMachine

image 42 - Simple start with Microsoft 365 and PowerShell

There’s a lot more to Execution Policy.  See About Execution Policy and Set-ExecutionPolicy

Just the beginning

We’ve skipped over a LOT about Azure, ExchangeServer and PowerShell. It should be enough to let you use simple configuration commands like Plus Addressing.

Our aim was a simple guide to administrative access for options not possible in the admin web pages.  Well … as simple as it’s possible with Microsoft’s intricate PowerShell system.

How to get ‘Plus’ email addresses, the new feature in Microsoft 365 and Outlook.com
Protect against the latest ransomware attack via Excel

Latest news & secrets of Microsoft Office

Microsoft Office experts give you tips and help for Word, Excel, PowerPoint and Outlook.

Give it a try. You can unsubscribe at any time.  Office Watch has been running for over 20 years, we've never, ever revealed or sold subscriber details.  Privacy policy
Invalid email address