PowerShell: Connect to Office 365 (Step by Step Guide)

Step 1: Install Required PowerShell Modules

Before you can connect to Office 365 you first need to install the required modules. Unfortunately, Microsoft is now recommending you install two modules to manage Office 365. Here are the two modules we will be installing.

  1. Microsoft Azure Active Directory Module for Windows PowerShell
    • The old module for Azure AD
    • Also known as MSOnline
  2. Azure Active Directory PowerShell for Graph
    • The new module for Azure AD

To make things even more confusing if you upgrade PowerShell to the new core version (PowerShell 7) it does not support the old module (Microsoft Azure Active Directory Module). If you are using PowerShell core you will need to use the new module (Azure Active Directory PowerShell for Graph).

Let’s move on to installing the modules.

Installing Module 1 (Microsoft Azure Active Directory Module for Windows PowerShell)

1) Open PowerShell as administrator. To do this right-click on PowerShell and choose “run as administrator”

2) Type the following command and press enter

Install-module -name MSOnline

If you get a message about NuGet version 2.8.5.201 or newer click yes or type Y.

If you get a message about installing the module from an untrusted repository click yes or type Y.

When it is completed we will very it’s installed in step 3.

3) Type the command below to list the installed modules

Get-Module -ListAvailable

Look for the MSOnline module name

The first module is installed. Now, let’s installed the 2nd module.

Installing Module 2 (Azure Active Directory PowerShell for Graph)

1) Open PowerShell as Administrator

2) Type the following command and press enter

Install-Module -Name AzureAD

3) Verify the module is installed

get-module -ListAvailable

Step 2: Connect to Office 365 with PowerShell

Each module has different commands for connecting to office 365. You don’t have to use both, you can pick which module you want to use.

Connect with Module 1 (Microsoft Azure Active Directory Module for Windows PowerShell… AKA MSOnline)

1) Type the following command and press enter

Connect-MsolService

You will get prompted to sign in.

If your account is configured for MFA you will get prompted to approve the request.

2) Verify connection

I like to use the command below to verify I’m connected to Office 365.

Get-MsolCompanyInformation

If you are connected PowerShell will display the company info that you have listed in Office 365.

Connect with Module 2 (Azure Active Directory PowerShell for Graph)

1) Type the following command and press enter

Connect-AzureAD

The AzureAD module will automatically display some company information when it connects.

So far you have learned the required modules, how to install them, and the commands to run to connect PowerShell to office 365.

Now let’s look at how to list the available commands for each module.

List Office 365 Module Commands

To view a list of commands you first need to know the module name, to list the module names use this command.

Get-Module -ListAvailable

In this example, I want to see all the commands for the AzureAD module. Type the command below.

Get-Command -Module AzureAD

To get a list of commands for the MSOnline module use this command

Get-Command -Module MSOnline

Example Office 365 Powershell commands

Here are a few examples of using PowerShell to manage office 365.

Get all Office 365 Users

Get-MsolUser -All | select-object *

Get all licensed users

Get-MsolUser -All | Where-Object { $_.isLicensed -eq "TRUE" }

Get Azure devices

Get-AzureADDevice

Set a users password

Set-MsolUserPassword-UserPrincipalName “<email address>” -NewPassword “New Password”

Assign a license to a user

Set-MsolUserLicense -UserPrincipalName “user name” -AddLicenses “<licensed name>”

10 thoughts on “PowerShell: Connect to Office 365 (Step by Step Guide)

  1. I loved as much as you will receive carried out right here The sketch is attractive your authored material stylish nonetheless you command get got an impatience over that you wish be delivering the following unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike

  2. I have been browsing online more than three hours today yet I never found any interesting article like yours It is pretty worth enough for me In my view if all website owners and bloggers made good content as you did the internet will be a lot more useful than ever before

  3. I share your level of enthusiasm for the work you’ve produced. The sketch you’ve displayed is refined, and the material you’ve authored is impressive. Nevertheless, you seem anxious about the prospect of heading in a direction that could cause unease. I agree that you’ll be able to address this concern in a timely manner.

  4. obviously like your website but you need to test the spelling on quite a few of your posts Several of them are rife with spelling problems and I to find it very troublesome to inform the reality on the other hand Ill certainly come back again

  5. Fantastic site Lots of helpful information here I am sending it to some friends ans additionally sharing in delicious And of course thanks for your effort

  6. Its like you read my mind You appear to know a lot about this like you wrote the book in it or something I think that you could do with some pics to drive the message home a little bit but instead of that this is fantastic blog An excellent read I will certainly be back

  7. I am not sure where youre getting your info but good topic I needs to spend some time learning much more or understanding more Thanks for magnificent info I was looking for this information for my mission

  8. Hi Neat post Theres an issue together with your web site in internet explorer may test this IE still is the marketplace chief and a good component of people will pass over your fantastic writing due to this problem

  9. no entanto, você fica nervoso por querer estar entregando o próximo mal, inquestionavelmente, volte mais cedo, já que exatamente o mesmo quase com muita frequência dentro do caso de você proteger esta caminhada

  10. I was recommended this website by my cousin I am not sure whether this post is written by him as nobody else know such detailed about my trouble You are amazing Thanks

Comments are closed.

RELATED POST

How to Install Exchange Online PowerShell Module

Managing email communication processes can be a real challenge for medium and big-sized businesses. Although Microsoft Exchange offers different management features, streamlining and…