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.

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)

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

ADVERTISEMENTS

FEATURED POSTS

How to Share Amazon Wish List

How to Share Amazon Wish List

This Itechguide teaches you how to share Amazon Wish List. The guide covers steps to share Amazon Wish List from a PC...
How to Pay with PayPal on Amazon

How to Pay with PayPal on Amazon

This Itechguide teaches you how to pay with PayPal on amazon. The guide covers 2 options to pay with PayPal on amazon....
How to Export Contacts from Outlook

How to Export Contacts from Outlook

This Itechguide teaches you how to export contacts from Outlook. The guide covers steps to export contacts from Outlook (Windows 10) or...
Best 90s Movies on Netflix

The 10 Best 90s Movies on Netflix

This Itechguide reviews the 10 best 90s movies on Netflix. The movies are listed in reverse order – with the 10th movie...
How to Clear Search History on Instagram

How to Clear Search History on Instagram

This Itechguide teaches you how to clear search history on Instagram. The guide covers steps to clear search history on Instagram from...

AMAZON DEALS

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

BEST OF ITECHGUIDES

How to Change YouTube Profile Picture

How to Change YouTube Profile Picture

This Itechguide teaches you how to change YouTube profile picture. The guide covers steps to change YouTube profile picture from a PC...
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...
Spotify Playlists

How to Find, Download or Share Spotify Playlists

Introduction This quick guide will show you step by step how to find, download or share Spotify Playlists. You...
printer offline

5 Ways to Restore Your Printer Online If Status is Offline

Introduction Is your printer offline? Setting it online is very simple but sometimes it is more than just setting...
How to Upload a PowerPoint to YouTube

How to Upload a PowerPoint to YouTube

This Itechguide teaches you how to upload a PowerPoint to YouTube. YouTube accepts .mp4 video formats. So, before...

RECENT POSTS

5 Best Bluetooth Headphones Under 100

5 Best Bluetooth Headphones Under $100

This Itechguide reviews the 5 best Bluetooth headphones under 100 (USD). The best headphone is reviewed last, starting with the 5th on...
How to Search for a Word in Google Docs

How to Search for a Word in Google Docs

This Itechguide teaches you how to search for a word in Google Docs. The guide covers steps to search for a word...
How to Create a Folder in Google Docs

How to Create a Folder in Google Docs

This Itechguide teaches you how to create a folder in Google Docs. The guide covers steps to create a folder in Google...
How to Check Amazon Gift Card Balance

How to Check Amazon Gift Card Balance

This Itechguide teaches you how to check Amazon gift card balance. The guide covers steps to check Amazon gift card balance from...
How to Remove Google Account from Phone

How to Remove Google Account from Phone

This Itechguide teaches you how to remove Google Account from phone. The guide covers steps to remove Google Account from Android or...

AMAZON DEALS

MUST READ

How to Strikethrough in Google Docs

How to Strikethrough in Google Docs

This Itechguide teaches you how to strikethrough in Google Docs. The guide offers steps to strikethrough in Google Docs from a PC...
How to Change Birthday on Facebook

How to Change Birthday on Facebook

This Itechguide teaches you how to change birthday on Facebook. The guide contains steps to change birthday on Facebook from the Facebook...
Best Sci-Fi Movies on Netflix

The 10 Best Sci-Fi Movies to Watch on Netflix

Want the very best of Sci-Fi movies on Netflix? We have a list that will thrill you. The films...
dropbox login

Dropbox Login: Your Ultimate Guide to Dropbox

Introduction Dropbox login allows you to sign in to and use Dropbox. But what is Dropbox? Let's kick off...
RAID 5 vs RAID 6

RAID 5 vs RAID 6: Differences, Benefits and Disadvantages

What is RAID 5 vs RAID 6? RAID 5 and RAID 6 uses striping with distributed parity technique. However,...

AMAZON DEALS

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