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.
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.
1Export 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.
- 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.
- Then type the this command and press enter.
Get-Credential <UserName@DomainName.com> | Export-CliXml -Path C:\PS\Get-Credential\O365loginCred.xml
- 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).
2Import 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 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.
3Run 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
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:
DO NOT run this command in a production account with so many users. It may take too long to run.
Here is the result returned by the command for my Office 365 account. For obvious reasons, I obscured the user names and mailboxes!
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
To learn more about the Get-User cmdlet, click Get-User link.
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.