How to Import and Use Office 365 PowerShell Modules

Photo of author

By Victor Ashiedu

Published

Have you ever wondered how to import and use the Office 365 PowerShell module? In this guide, I’ll walk you through the steps to get started with using PowerShell to manage your Office 365 environment.

Step 1: Install the Office 365 PowerShell Module

Installing the module is the first step to importing and using Office 365 PowerShell Module. Follow the steps below to install the MSOnline module on your PC.

  1. Search powershell. Windows PowerShell will be highlighted as the best match.

    On the details pane, click Run as administrator. Windows will request that you confirm PowerShell to make changes to your Windows 11 – click Yes.
  2. Then, enter the command below on the opened Windows PowerShell command console; run the command by pressing Enter on your keyboard.
powershell.exe -ExecutionPolicy Unrestricted
The above command configures your current PowerShell session to allow you to run commands from modules you download from the internet. Without running this command, you could not run commands from the O365 module you’ll download later.
  1. Next stop: run the command below to download and install the MSOnline module. Then, run the command beneath it to import the module into your computer.
Install-Module -Name MSOnline
Import-Module MSOnline

The command takes a short while to complete. When it completes, PowerShell returns to its prompt.

  1. Finally, before you proceed to the next section, confirm that you’ve installed the MSOnline module on your computer by running the command below:
Get-Module MSOnline | Select-Object -ExpandProperty ExportedCommands

The command displays all the cmdlets in the Office 365 PowerShell module. Here is an alternative command that returns the same result.

Get-Command -Module MSOnline

If you see a list of commands similar to what I have in my screenshots below, you’ve successfully imported the Office 365 PowerShell module. The first screenshot is the result of the first command above, while the second screenshot shows the results of the second command.

In the next section, you’ll prep the credentials you require to connect to Office 365. Then, in the subsequent sections, you’ll connect to 365 and use the imported O365 modules to perform some admin tasks.

Step 2: Prepare Your UserName and Password

In the next section, you will connect Office 365, but before you do that, it is a good idea to prepare the credentials you require.

Before you proceed, create a folder to save your encrypted username and password. Then, follow the steps below.

  1. Run the command below to encrypt your Office 365 username and password into an XML file.
Before you run the command, change [email protected] to your username. Also, change the path – D:\PowerShellCred\ – to a path on your PC.
Get-Credential [email protected] | Export-CliXml -Path D:\PowerShellCred\O365loginCred.xml

Windows PowerShell will display a login window when you run the above command. Enter your Office 365 password, then click OK.

  1. Finally, import the saved password into a variable by running the command below:
$O365loginCred = Import-Clixml D:\PowerShellCred\O365loginCred.xml
The command imports the password into a variable called O365loginCred. I will use this variable in the Credential parameter when I connect to Office 365.

Step 3: Import O365 PowerShell Module

  1. Run the command below to connect to your Office 365 account using PowerShell.
Connect-MsolService -Credential $O365loginCred

When you run the command, PowerShell returns almost immediately, giving the impression that nothing happened! If PowerShell did not return any error messages, I can assure you that you’ve successfully connected to your Office 365 account.

To confirm this, let’s perform some tasks using some of the commands in the imported Office 365 PowerShell module (MSOnline).

  1. Return user information with the Get-MsolUser command. Once you have connected to Office 365 by running the Connect-MsolService command, you can return information about users by running the Get-MsolUser command.

    If you run the Get-MsolUser command without adding any filtering, it returns ALL your Office 365 users.

The screenshot below shows the result of the Get-MsolUser command when I run the command on my Office 365 account.

Warning!
DO NOT run this command in a production account with so many users. It may take too long to run.
  1. Return licensed Office 365 users. In the last example, the Get-MsolUser command returned all users in my O365 account.

    If you examine my previous screenshot closely, you’ll notice that the command returned a column called isLicensed. The isLicensed column returns True for users that have been assigned an Office 365 license.

    On the contrary, if a user has not been assigned a license, the isLicensed column returns False.

    Based on this information, I can modify my previous command to return users assigned O365 licenses by running the command below:
Get-MsolUser | Where-Object {$_.isLicensed -eq "True"}

The command now returns only users assigned.

  1. Return unlicensed Office 365 users using the UnlicensedUsersOnly parameter of the Get-MsolUser command. In the last example, I showed you how to return only licensed users by pipping the output of Get-MsolUser to the Where-Object command.

    It may interest you to know that to return unlicensed users, you do not need to pipe Get-MsolUser to the Where-Object command! This is because the Get-MsolUser cmdlet has a parameter called UnlicensedUsersOnly.

    If you include this parameter, PowerShell returns all Office 365 users that have not been licensed. How cool is that!

    Here is the command and the result.
Get-MsolUser -UnlicensedUsersOnly
  1. Return all Office 365 groups using the Get-MsolGroup command. In this last example, I want to show you how to return all groups (including shared mailboxes) to your Office 365 account.

    To return ALL groups, run the Get-MsolGroup command without any parameters or filtering…

Frequently Asked Questions

1. How do I use an imported module in PowerShell?

The first step to using an imported PowerShell module is to find the cmdlets in the module. Run the Get-Module command with the name of the module.

Once you list the cmdlets in the imported module, use the Get-Help followed by the name of the cmdlet to learn how to use the cmdlet. For example, Get-help Get-MsolGroup displays information about the Get-MsolGroup command.

You may also get some examples by adding the Examples parameter. For instance, Get-help Get-MsolGroup -Examples.

2. How do I use Office 365 PowerShell modules?

To use Office 365 PowerShell module, follow the steps below:

a) Install the MSOnline module by running the Install-Module -Name MSOnline command.
b) Then, import the modules by running the Import-Module MSOnline command.
c) Finally, connect to Office 365 using the Connect-MsolService command.

Once you’re connected, you can manage office 365 by running commands using the cmdlets available in the MSOnline module.

3. How do I connect to Office 365 PowerShell Azure AD modules?

Follow the steps below to install the AzureAD PowerShell module, then connect to Azure Active Directory and use the module to manage your tenant.

a) Run PowerShell as administrator, then run the Install-Module AzureAD command to install the Module.
b) Then, to import the installed modules on your computer, run the Import-Module AzureAD command.
c) To connect to Azure Active Directory, run the Connect-AzureAD command; then run the PowerShell commands in the AzureAD module

4. How do I get an Office 365 group in PowerShell?

To return all Office 365 groups in PowerShell, import the Office 365 module by running the Install-Module -Name MSOnline command. Then, use the Connect-MsolService command to connect to your Office 365 account.

Finally, to return all groups in your Office 365 account, run the Get-MsolGroup command.

5. What is the difference between executing and importing a module?

Importing a module downloads the module to your computer while executing it means running commands in the module.

Conclusion

Working with Office 365 Powershell is that straightforward! I hope I made your day.

I hope you found this guide helpful. If you did, let us know by sparing two minutes to share your experience vis the “Was this page helpful?” question below.

Love this guide? Get more PS guides from our Windows PowerShell page.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment

Send this to a friend