The essential admin's guide to Office 365 PowerShell
A comprehensive collection of articles, videos and more, hand-picked by our editors
On-premises Microsoft SharePoint includes robust Windows PowerShell support, enabling SharePoint administrators...
to perform just about every conceivable management task across a farm.
Over the past few years Microsoft has made strides in bringing its SharePoint Online software as a service offering in parity with on-premises SharePoint, but the product team has a long way to go.
Today I'm going to explain the capabilities and limits of SharePoint Online management with PowerShell.
Some preliminary points to ponder
The outermost security boundary in on-premises SharePoint is the farm. A farm consists of IIS websites called web applications, which resolve into one or more site collections.
In SharePoint Online, management begins and ends at the site collection level. Your SharePoint Online account is called a tenant, and in the browser you use a stripped down version of Central Administration to manage the site collections within that tenant (Figure 1).
Once you have a SharePoint online site collection up and running, the daily management is similar to what you have in SharePoint's on-premises counterpart -- as long as you work from the browser.
While SharePoint on-premises installs the Microsoft.SharePoint.PowerShell snap-in along with the program binaries, the SharePoint Online Management Shell needs to be downloaded and installed from the Microsoft Download Center. It's a tiny download and will take less than a minute to add to your administrative workstation.
Of course, you're not going to get very far without a SharePoint Online subscription. Microsoft has subscription plans with and without Office 365, the cloud-based counterpart to Microsoft's industry standard Office productivity suite.
Exploring the SharePoint Online cmdlets
Now that you have a SharePoint Online subscription and the Management Shell loaded, start an elevated Windows PowerShell console session. Start an administrative PowerShell console by right-clicking the PowerShell icon in Windows and selecting Run as Administrator from the shortcut menu.
Use Get-Command to obtain the full list of SharePoint Online cmdlets:
Get-Command –Module Microsoft.Online.SharePoint.PowerShell
You'll observe some interesting facts about the cmdlet set:
- The noun portion of the cmdlet names start with the SPO prefix. By contrast, the SharePoint on-premises cmdlet nouns have an SP prefix.
- Microsoft is constantly updating the number of cmdlets. When this article was written, there were 42 cmdlets in the SharePoint Online module. The SharePoint on-premises module contains more than 700 cmdlets. To obtain a cmdlet count, simply pipe the results of the previous Get-Command to Measure-Object -- specifically, add | Measure-Object to the end of the previous statement.
- The SharePoint online cmdlets seem to center their functionality around site collections in general and user administration. This is bad news for PowerShell admins who want to programmatically control SharePoint site assets, such as lists and libraries.
We can leverage Get-Help to learn cmdlet syntax. First update your local PowerShell help repository:
Update-Help –Force –ErrorAction SilentlyContinue
And then try this:
Get-Help –Command Connect-SPOService
The Connect-SPOService cmdlet connects a SharePoint Online global administrator to the SharePoint Online Administration Center.
Managing SharePoint Online site collections
The first step to managing your SharePoint Online site collections with PowerShell is running Connect-SPOService to authenticate to the Microsoft public cloud. You'll need two pieces of information:
- Your tenant URL. This takes the form https://tenant-admin.sharepoint.com, where tenant is the name of your service name. You specify that service name during SharePoint Online subscription enrollment.
- Your SharePoint Online administrator username and password.
Here's the code:
Connect-SPOService -url https://techtarget-admin.sharepoint.com -Credential firstname.lastname@example.org
We use Get-SPOSite to retrieve a list of site collections attached to your subscription. Here are mine:
Notice in the previous code I piped the Get-SPOSite results to Select-Object to show only the URL property. Wherever possible, I filter my PowerShell pipelines so we can concentrate on what we want to see and hide the rest.
To get a handle on our primary SharePoint Online site, let's store it in a variable called $main:
We access the properties by using dot notation, shown below:
A bit boring, perhaps. Why not create a new team site? To do that, we need to fetch the appropriate template name. There's a cmdlet for that: Get-SPOWebTemplate. Here I use Where-Object to filter on templates whose title is Team Site.
Get-SPOWebTemplate | Where-Object -Property Title -eq 'Team Site'
Name Title LocaleId CompatibilityLevel
---- ----- -------- ------------------
STS#0 Team Site 1033 15
We also need to know our target time zone in advance. Fetch that information from the Microsoft Developer Network; my local time zone is Central, which corresponds to time zone ID 11.
Finally, we'll use New-SPOSite to build our new site collection. I'll use a storage quota of 500 MB:
New-SPOSite -Url https://techtarget.sharepoint.com/sites/techtarget -Title 'TechTarget Test Team Site' -Owner 'email@example.com' -Template 'STS#0' -TimeZoneId 11 –StorageQuota 500
Specifically, SharePoint Online built the site collection container and automatically populated it with a top-level website that employs the Team Site template. Let's verify its creation programmatically and graphically (Figure 2).
Get-SPOSite | Where-Object -Property Url -Like '*techtarget*' | Select-Object –Property Url
We use the SPOUser command set to add and manage users:
Get-Command -Noun SPOUser | Select-Object -Property Name
SharePoint Online resources
Here are a few resources to help you get started with API access to SharePoint Online.
Use Remove-SPOSite to send an unwanted SharePoint Online site collection to the Recycle Bin:
Remove-SPOSite –Identity https://techtarget.sharepoint.com/sites/techtarget -Confirm:$false
Finally, use Remove-SPODeletedSite to permanently delete a site collection:
Remove-SPODeletedSite –Identity https://techtarget.sharepoint.com/sites/techtarget -Confirm:$false
CSOM? Isn't that a playground toy?
We must turn directly to the underlying .NET Framework to manage SharePoint Online granularly. Specifically, our options are:
- The client-side object model (CSOM) application programming interface (API)
- The Representational State Transfer (REST) API
An API is a set of conventions that programmers can leverage to interact with the API's host services. The CSOM is a set of client-side code libraries that proxy their respective server-side counterparts.
REST is a popular API for web services that involves data interchange using the firewall friendly HTTP and HTTPS protocols. Read Andrew Connell's discussion on the pros and cons of each API.
Get started with the SharePoint Online Management Shell
Prepare to migrate to SharePoint Online
Should your enterprise choose SharePoint in the cloud?