Cool PowerShell Scripts for Managing Office 365

Office 365 is Microsoft’s SaaS office productivity offering and PowerShell is Microsoft’s strategic task automation platform. Combined, they become a tool you can use to manage most aspects of Office 365. Writing PowerShell scripts from scratch can be tedious, so in this article I’ll look at some existing scripts you can download and leverage.

For many IT Pros simply getting started using PowerShell to manage Office 365 can be daunting. There are several hundred cmdlets that enable you to manage both your Office 365 subscription and the Exchange 2010 component of Office 365. As I noted earlier, Lync Online and SharePoint Online do not currently have PowerShell Cmdlet support – although I hope that will change sooner rather than later!

Getting Scripts

One of the main advantages of PowerShell for managing virtually any aspect of Windows (on-premise and the cloud!) is the community. There are PowerShell and Office 365 fans out there who are willing to share their scripts and script fragments. This PowerShell code can sometimes be used, straight out of the box, or more likely, adapted for use in your environment.

A small word of caution: whenever you download code from the Internet, whether it’s just a two-line script or large multi-thousand line modules, you need to be careful. While for the most part, malware within the PowerShell community is uncommon – it can happen. Fortunately, with PowerShell, you can read and check the script before running it to ensure that the script will do what you want, and what you expect. The scripts that follow all seemed to work and have no unwanted side effects when I worked with them – but as ever, caveat emptor.

Microsoft Sample PowerShell Scripts for Office 365 Deployment

You can download the first set of scripts from Microsoft's Office 365 Helper Scripts. This is a self-extracting zip file that contains 8 scripts:

  • AssignLicenceByDG.ps1 – this script assigns Office 365 licenses based on a membership of a specified distribution group (i.e. the group aligns to an Office 365 license type that you want to assign to a given user).
  • GetMsolTennantSkuUsage - Generates a CSV usage report of licenses owned, licenses consumed, and available licenses.
  • GetMsolUserReport - Generates a CSV report that containing general and Exchange Online related information about users in Office 365. A nice combination of the general Office 365 and the Exchange On-Line cmdlets.
  • GetSynchedObjectCount – counts how many objects are synched between the Office 365 and your on-premise directory. This count can provide early warning of replication issues.
  • GetTotalUserGroupscontactsInForest - Search Active Directory and calculate the total number of users, groups, and contacts for the domains in your forest.
  • NewDirSynchPowerShellShortcut – creates a PowerShell shortcut in the start menu for DirSync.
  • NewRandomPasswordFile - Generates a random password by referencing a CSV file which contains the User Principal Name of a list of users.
  • SetMsolUserPassword_FromCSV - Sets the Office 365 password by referencing a CSV file.

This package is a nice set of starter scripts. You can use them as they are, or customise them as well as factoring out parts of these scripts and incorporating the logic into your own.

Managing Office 365 and On-Premises Exchange 2010 from the same Powershell Session

Once you get started managing Office 365 and Exchange with Powershell, you’ll discover there is an overlap of cmdlets between an Office 365 environment and your on-premise Exchange 2010 environment. To deal with this, Steve Goodman has written a simple script fragment to connect to both environments simply and easily. You can get more details of this script fragment from Steve’s blog. This is a classic example of getting a script fragment from the web which you can then use in your own scripts.

PowerShell Script for starting client programs

This is a neat client-based script you can get from the Office 365 Community forum. This script can be used on a client computer at boot time to load key programs (eg Lync Communicator, Outlook, etc) and open a multi-tabbed instance of Internet Explorer that opens several key sites (key to the user).

This code is a great example of a script that you’d want to tweak – you’d probably not want the exact same pages or tools open as the poster of this script, but it’s a good starting script which you can customise to meet your needs.

Command Builder for Office 365

Microsoft’s TechNet site also has a great page that enables you to build cmdlets using a drag and drop interface. After navigating to the PowerShell Drag and Drop page, you can drag a verb then a noun to the design surface. The page will then tell you the parameters you need – after you enter the parameters, you will see the complete construction for the cmdlet. As the full screenshot above is quite tiny for some, I've broken the screen into the two sections below.

The other half of the screen builds the PowerShell command.

You can copy this command to the clipboard (and paste it into a Powershell window), which looks like this:

This tool is excellent for helping in-house administrators who are new to PowerShell. It takes some of the pain out of the learning curve – although the user does need to do some background reading to get the basics of PowerShell!

Set Mailbox Quotas in Office 365

This final script set enables you to set or change mailbox quotas on one or more Exchange Online mailboxes. You can get these scripts from the Exchange Server web site. This is another good example of a script fragment you may want to leverage in your own scripts.


There are a number of resources an Office 365 administrator can draw on to help make administration less difficult. For partners managing multiple customers’ Office 365 accounts, these scripts and script fragments are a great starting point that can be leveraged to reduce the complexity of managing multiple O365 accounts.