How to connect to Office 365 Services using PowerShell

With the increased adoption of Cloud based technologies that offer nice tools to administer the big variety of options and configurations, sometimes it becomes difficult to concentrate and and execute multiple operations at the same time or using a single “console” without jumping from one screen to another. Also the ability to run same operations in batch mode can be a tedious task for certain configurations. This is were PowerShell can come handy and can provide a quick access to desired configurations.

The purpose of this post is to show the quick steps to connect to O365 Services with PowerShell and in particular with the SharePoint Online Services.

Before proceeding here you are the shopping list.


At this point we are ready to start by executing the following steps:

Step 1: Open Windows PowerShell as an administrator

For convenience I prefer to use the PowerShell ISE. When starting with the Administrator account will look like the following:

At this point we need to import the MSOnline and the Microsoft.Online.SharePoint.PowerShell modules with a classic Import-Module. We can then verify the modules are loaded successfully with a simple Get-Module. Please note that in order to avoid a warning message when loading the module for SharePoint Online we’ll use the -DisableNameChecking parameter as per screenshots below:

And we can now proceed with the module to manage SharePoint Online. If we forget to add the DisableNameChecking parameter we’ll get the following warning message: The specified module ‘Microsoft.Online.SharePoint.Online.PowerShell’ was not loaded because no valid module file was found in any directory

Now with a simple Get-Module we can verify the needed modules have been loaded correctly

Step 2: Create a Windows PowerShell credentials object

At this point we’re ready to create the credentials object. We can do this by saving them in an encrypted format into a variable by using the credential window as per next screenshots. Let’s create a variable $Credsecurely storing our credentials:

When prompted enter the credentials for an o365 user with Global-Admin role. Should you have a custom Domain associated with your o365 Tenancy, enter directly the required credentials using your custom domain rather than

To verify the credentials are successfully stored we issue the command for the associated variable:

Step 3: Connect to Office 365

At this point we are ready to connect to the o365 tenant by issuing the command:

Connect-Msolservice -Credential $Cred

After connection is established there is a big number of commands we can execute to query and execute actions on the o365 objects. From here we can also manage Users and Groups, Contacts, Directory Synchronization, Domain Info and much more.

Step 4: Connect to SharePoint Online

Now we are ready for the last step: connect to SharePoint Online. For this scope we’ll use the Connect-SPOService command making sure we remember the following simple rules

  1. Always use the HTTPS in the URL paramter

  2. Build the URL with

After the connection is successful we can then use the Get-SPOSite to list the available Sites

There are plenty of commands already available for managing SharePoint Online and these will be part of my next posts.





Michele Domanico

Passionate about Virtualization, Storage, Data Availability and Software Defined Data Center technologies. The aim of is sharing with the Community the knowledge and experience gained with customers, industry leaders and like minded peers. Always open to constructive feedback and new challenges.

Leave a Reply