Protiviti / SharePoint Blog

SharePoint Blog

July 01
Manage Office 365 using PowerShell

​As a SaaS, O365 provides a robust Admin Center to manage all aspects of a service from within the service’s Admin Center. However, quite often we are faced with the need to customize the way some of the administration tasks are performed. A prime example would be the need to provision a user account, assigning an O365 license and granting access to certain site. While this can definitely be accomplished via the Admin Center, doing it using PowerShell, ensures that the process can be automated.

Before delving into the dark world of the almighty PowerShell, the first step is to ensure that your machine is properly setup, which is the scope of this article.

The following steps will help you get setup:
1. Install the 64 bit version of the Microsoft Online Services Sign-in Assistant which can be downloaded from https://www.microsoft.com/en-us/download/details.aspx?id=41950
2. Install the 64 bit version of the Windows Azure Active Directory Module for Windows PowerShell which can be downloaded from http://go.microsoft.com/fwlink/p/?linkid=236297
3. To verify everything install successfully, you should be able to search for “Azure” and see the following:


4. Open PowerShell ISE and execute the following script
The above script will prompt for the O365 Credentials, create a new Power Shell session at https://outlook.office365.com and imports the cmdlets from that session into your local session. However, if you are like me and are managing multiple O365 tenants for various clients, entering the above script every time is a handful and defeats the purpose of using PowerShell.

Modifying the script as shown below alleviates that concern


This script retrieves the user credentials from the csv file (credentials.txt) based on the client name which is passed in as a parameter. Saving the script as Connect.Msol.ps1 and connect my client (Contoso) tenant involves executing the following:

    Connect.Msol.ps1 –clientName contoso


Once the connection is established you are now able to manage the O365 assets.
5. At this point you are however not yet connected to SharePoint Online. For this,
a. First install the SharePoint Online cmdlets from http://go.microsoft.com/fwlink/p/?LinkId=255251.
b. Use the following script


This script read the username, password and the admin url from the csv file (credentials.txt) and establishes to the O365 and SharePoint Online.
Now you are all connected!!!

If you are new to PowerShell, I recommend that you use the Windows PowerShell Command Builder tool available at https://www.microsoft.com/resources/TechNet/en-us/Office/media/WindowsPowerShell/WindowsPowerShellCommandBuilder.html. This tool has served me well and hope it does for you as well.

Quick Launch


© Protiviti 2020. All rights reserved.   |   Privacy Policy