How to Import and Use Office 365 PowerShell Modules

-

|

Introduction

Office 365 Powershell provides modules you can use to automate Office 365 tasks. But to access these modules you need to connect to Office 365 Powershell.

In this guide I will use Office 365 and O365 interchangeably. I will also use PowerShell in place of PS and vise versa.
It goes without saying that you require internet connection to run commands in this guide! You will also need a valid Office 365 account.
Advertisement

Steps to Get and Use Office 365 PowerShell Modules

This is a step by step guide that walks you through how to connect to Office 365 Powershell to access the modules. The guide also has some examples of O365 commands.

1
Export Your User Name and Password to an Encrypted XML file

Before you can access Office 365 via PowerShell you need an account with the right permission to access O365.

You can provide the credentials directly when you connect to 365 via PS. But, for scripting purposes, it is better to export an encrypted version of the your users name and password to an XML file.

Here are the steps to export your user name and password to an encrypted XML file:

  • Type PowerShell in the search bar. Then right-click Windows PowerShell and select Run as administrator.
office 365 powershell - open PowerShell as administrator
  • At the PowerShell prompt, type this command. Then press Enter.
powershell.exe -ExecutionPolicy Unrestricted

This opens a new PS session that permits you to run remote unsigned scripts. Without this, when you run the command that imports Office 365 PowerShell module, the command may fail.

office 365 powershell -run powershell.exe -ExecutionPolicy Unrestricted command
  • Then type the this command and press enter.
Get-Credential <UserName@DomainName.com> | Export-CliXml -Path C:\PS\Get-Credential\O365loginCred.xml
Replace <UserName@DomainName.com> with your O365 username. The name MUST be in UPN format: UserName@DomainName.com. If you use the format DomainName\UserName, subsequent commands in this guide may fail.
  • When you execute the command, you will be prompted to enter the password of the account. Enter the password and click OK. If the command is successful, an XML file will be created in the specified path (see the second image below).
Advertisement

2
Import Office 365 PowerShell Modules

The next step is to connect to your O365 account via PowerShell and import O365 PS modules.

Here are the steps:

  • Still on the same PS command prompt, type this command. Then press enter.
$ImpCred = Import-Clixml C:\PS\Get-Credential\O365loginCred.xml -ErrorAction SilentlyContinue

The command imports the encrypted XML file with your password and stores it in a variable called $ImpCred.

  • Next, execute this command…type, then enter.
$SessionOption = New-PSSessionOption -SkipRevocationCheck -SkipCACheck -SkipCNCheck

The command creates the required SessionOption needed in the next command. Then stores the result in $SessionOption variable.

In the next command, you will create a PS session to Office 365 PowerShell.

  • Here is the command. Type it then press enter.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $ImpCred -Authentication Basic -AllowRedirection -SessionOption $SessionOption -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
The command will delay a bit – may not be as fast as the other commands we have discussed so far.

The last command appears scary and long. But it performs a very simple task: it uses New-PSSession to make a remote PS connection to Office 365 PowerShell.

The next command will import Office 365 PowerShell modules into your current PS session.

  • To import the session you created into your PS console, type this command, then press enter:
Import-PSSession $Session -DisableNameChecking -AllowClobber -ErrorAction SilentlyContinue

The command will take some time to run. While it is running, it will display the modules and commands it is importing. When the modules are imported completely, it will return the module details – highlighted in the second image below.

Office 365 PowerShell modules are now available for you to use! The next section will demo how to access the Cmdlets with some examples.

3
Run Office 365 Powershell Commands (With Examples)

Advertisement

Now that you have imported O365 PS modules into your current PS session you can run PowerShell commands directly on your Office 365 account.

This section demos some examples.

Lets begin by displaying all PS commands available in the imported module.

  • To do this, type this command and press enter.
Get-Command -Module tmp_ycvzopmw.1bq
Replace tmp_ycvzopmw.1bq with the module name shown in the last image. This name will be different every time you run the Import-PSSession command.

The result of the last command is a very long list…

  • To get a list of all users (and Mailboxes) in your Office 365 account, type the command below:
Warning!
DO NOT run this command in a production account with so many users. It may take too long to run.
Get-User

Here is the result returned by the command for my Office 365 account. For obvious reasons, I obscured the user names and mailboxes!

office 365 powershell

As I mentioned in my warning earlier, it is NOT recommended to run the Get-User command without any filters.

The recommended way to use this command is to return a particular user or group of users. For example, to return just my name, I will use the command below:

Get-User -Filter "Name -like 'Victor*'"

Here is the result

office 365 powershell

To learn more about the Get-User cmdlet, click Get-User link.

Conclusion

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

Have any question or comment? Use the “Leave a Reply” form found at the end of this page.

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 19H2 Preview Build

Introduction If you are a member of Windows 10 Insider Program you can install Windows 10 19H2 Preview Build....

How to Sign in to Windows 10 with a Microsoft Account

Introduction When you installed Windows 10 you may have created and signed in with a local account. You can...
Disable IPv6 in Windows 10

How to Disable IPv6 in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to disable IPv6 in Windows 10: Disable IPv6 from...

Advertisement

TRENDING POSTS

Remote Desktop Connection

Remote Desktop Connection an Internal Error Has Occurred [Fixed]

Introduction I recently received the error message "Remote Desktop Connection an Internal Error Has Occurred". It was strange because...

Find My Samsung: Register and Use Samsung Find my Mobile

Introduction Ever wondered how you could find your Samsung phone if you lost it? Find my Samsung or Samsung...
What is the Difference Between PowerShell and CMD?

Windows Powershell vs CMD: Differences and Similarities

Introduction This short guide compares Windows PowerShell vs CMD (Windows command prompt). I will cover the history and nature...
Spotify No Longer Supports this Version of Microsoft Edge

Spotify No Longer Supports this Version of Microsoft Edge [Fixed]

Introduction When you open Spotify web player on Microsoft Edge, you may receive the error message "Spotify No Longer...
Windows 10 Won't Boot

Windows 10 Won’t Boot With Black Screen? 3 Ways to Fix It

Why Won't Windows 10 Boot Up? If your Windows 10 stops with a black screen, the first question in...

Advertisement

BEST OF ITECHGUIDES

turn on battery saver

7 Ways to Save Battery on Samsung Galaxy S9

Introduction Battery lifetime is arguably the biggest problem facing smartphone users today. This guide shows 7 ways to turn...
how to delete spotify account

How to Delete Your Spotify Account

Introduction There may be so many reasons you may want to delete your Spotify account. May be you prefer...
schedule shutdown windows 10

How to Schedule Windows 10 to Shutdown at Specified Time

Introduction If you want to schedule shutdown for windows 10 (turn off) at specified time, this guide is for...

Windows 7 Safe Mode: How to Start Windows 7 in Safe Mode

Introduction If need to perform some advanced troubleshooting on Windows 7, then you may need to start Windows 7...
spotify subscription

How to Subscribe to Spotify Premium

Introduction Paid Spotify subscription (premium) has a lot more benefit compared to the free one. Some of the benefits...

RECENT POSTS

How to Enable Hyper-V in Windows 10 (3 Methods)

How to Enable Hyper-V in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Hyper-V in Windows 10. To install Hyper-V...
RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

Introduction Starting from October 2018 (1809) update, RSAT Tools became part of Windows 10. From this version of Windows...

How to Enable RSAT for Active Directory in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Active Directory in Windows 10. It is not exactly enabling "Active...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 1909 (19H2) Preview Build

Introduction Windows 10 1909 Preview is available for Windows Insiders. Made available early September, 2019 you have to be...
How to Install RSAT in Windows 10 (3 Methods)

How to Install RSAT in Windows 10 (3 Methods)

Introduction This guide demos how to Install RSAT in Windows 10. Starting with Windows 10...

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...
Configure Map Network Drive with Group Policy

Map Network Drive in Windows 10 with Group Policy

Introduction This guide demos how to map network drive with group policy. This guide is...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Download Windows 10 ISO with Media Creation Tool

How to Download Windows 10 ISO with Media Creation Tool

Introduction This guide demos the steps to download Windows 10 ISO. You can download Windows 10 ISO with Media...

How to Install Windows 10 from Network Boot (Via WDS Server)

Introduction This guide demos how to install Windows 10 from network boot. The steps discussed in...

Advertisement

MUST READ

How to Enable Hyper-V in Windows 10 (3 Methods)

How to Enable Hyper-V in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Hyper-V in Windows 10. To install Hyper-V...
powershell param

Powershell Param: Syntax, Types, Attributes and Examples

Introduction A Powershell Param block is used to add parameters to advanced functions. Powershell Param adds parameters that can...
Disable IPv6 in Windows 10

How to Disable IPv6 in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to disable IPv6 in Windows 10: Disable IPv6 from...
Transfer Schema Master Role

Transfer Schema Master Role With MMC or NTDSUTIL Command Tool

Introduction There are situations when an administrator may need to transfer the schema master role from one Domain Controller...

How to Alphabetize (Sort Lists or Tables) in Microsoft Word

Introduction You can alphabetize (sort) a list or table in Microsoft word by number, date or text. You can...

By using this website you agree to accept our Privacy Policy and Terms & Conditions