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.
Advertisements


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).

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)

Advertisements


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.

  • Was this post Helpful?
  • YesNo

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

How to Add Bullet Points in Google Slides

How to Add Bullet Points in Google Slides (Android or iOS Apps or PC)

This Itechguide teaches you how to add bullet points in Google Slides. The guide offers steps to add bullet points in Google...
How to Create a Business Facebook Page without a Personal Account

How to Create a Business Facebook Page without a Personal Account

This Itechguide teaches how to create a business Facebook page without a personal account. There could be a...
How to Wrap Text in Google Sheets

How to Wrap Text in Google Sheets (from a PC or the Apps)

This Itechguide teaches you how to wrap text in Google Sheets. The guide offers steps to wrap text in Google Sheets from...
How to Change Birthday on Facebook

How to Change Birthday on Facebook (from the App or PC)

This Itechguide teaches you how to change birthday on Facebook. The guide contains steps to change birthday on Facebook from the Facebook...
Best Thrillers on Netflix

The 10 Best Thrillers on Netflix

The thrillers on Netflix is quite a large number of movies. Knowing the best thrillers to watch can be a challenge.

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...

ADVERTISEMENTS

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...

BEST OF ITECHGUIDES

How to Create a Pie Chart in Excel and Google Sheets

How to Create a Pie Chart in Excel and Google Sheets

Introduction This guide demos how to create a pie chart in Excel and Google Sheets. A pie chart is...

How to Fix “BootMgr is Missing” Error in Windows 10

Introduction If you receive Fix "BootMgr is missing" Error in Windows 10, the default response is panic! But you...
Active Directory Domain Services

Active Directory Domain Services: Installation & Configuration

What is Active Directory Domain Services? Active Directory Domain Services (AD DS) is at the core of Microsoft's Directory...
routing and remote access windows server 2016

Routing and Remote Access in Windows Server 2016 (Part 1): Install Remote Access Role

Introduction This is the first of a 4-part "Routing and Remote Access in Windows Server 2016" series.
Cortana Search not Working in Windows 10 [Fixed]

Cortana Search not Working in Windows 10 [Fixed]

Introduction This guide offers the fix for Cortana search not working in Windows 10. The reason for Cortana search...

ADVERTISEMENTS

RECENT POSTS

How to Logout of Amazon App

How to Logout of Amazon App (from Android, iPhone or Windows 10)

This Itechguide teaches you how to logout of Amazon app. The guide offers steps to logout of Amazon app for Android, for...
How to Turn off YouTube Notifications

How to Turn off YouTube Notifications (PC or YouTube Apps)

This Itechguide teaches you how to turn off YouTube notifications. The guide offers steps to turn off YouTube notifications from a PC...
How to Change YouTube Thumbnail

How to Change YouTube Thumbnail (PC or YouTube Studio App)

This Itechguide teaches you how to change YouTube thumbnail. The guide offers steps to change YouTube thumbnail from a PC or...
How to Add Bullet Points in Google Slides

How to Add Bullet Points in Google Slides (Android or iOS Apps or PC)

This Itechguide teaches you how to add bullet points in Google Slides. The guide offers steps to add bullet points in Google...
How to Change Shipping Address on Amazon

How to Change Shipping Address on Amazon

This Itechguide teaches you how to change shipping address on Amazon. The guide offers steps to change shipping address on Amazon:

MUST READ

Cannot Start Microsoft Outlook

Cannot Start Microsoft Outlook [Fixed]

Introduction While I was working on How to Change Outlook Password, I opened Outlook and received the error message...
windows server 2016 recovery mode

How to Repair Windows Server 2016 Boot (“BootMgr is Missing”)

Introduction If you are reading this, your Windows Server 2016 is probably broken and you may need to repair...
server 2016 not downloading updates from wsus

Fix for Server 2016 not Downloading Updates from WSUS

Introduction If you experience Server 2016 not downloading updates from WSUS, it is likely caused by a known bug....
How to Delete a Google Calendar

How to Delete a Google Calendar (from a Smartphone or PC)

Introduction This guide demos how to delete a Google calendar from a smartphone or a PC.
Windows Server 2016 Interview Questions and Answers

Windows Server 2016 Interview Questions and Answers

Introduction This guide provides common Windows Server 2016 interview questions and answers. The questions are...

LATEST DEALS

Exclusive Student Offer_Save 10% on selected Surface devices

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