Friday, August 16, 2013

Managing SharePoint Online with Powershell

With the release of SharePoint 2013 as part of Office 365, Microsoft has also introduced the ability to manipulate SharePoint site collections in the cloud using PowerShell. Before you can start with the SharePoint Online cmdlets you have to install them separately from this link - . This downloads allows you to run SharePoint Online PowerShell from any host machine. It only requires PowerShell v3 or v4. SharePoint does not need to be installed locally. To use  the SharePoint Online cmdlets, you must must be a global administrator in Office 365.
Once installed you can open the SharePoint Online Management Shell,  it is implemented as  a PowerShell module which also allows you to use it in another PowerShell editor by manually loading the required module Import-Module Microsoft.Online.SharePoint.PowerShell.
To establish a connection to your SharePoint Online tenant you will need to use the Connect-SPOService cmdlet (Remember that you must use the tenant admin site url as a parameter). This will fill up a static internal variable which will be used afterwards. You should clear this internal variable using the Disconnect-SPOService cmdlet.
    Connect-SPOService –Url –credential

Once you are connected you can manipulate your SharePoint Online environment. There are only 30 cmdlets available in for SharePoint Online – to get a full list type the following command.
Get-Command –Module Microsoft.Online.SharePoint.PowerShell

All cmdlets start with SPO. Most cmdlets are related to site collection management, user and app management. All returned object are simple data objects and most of them are limited in functionality. 

Cmdlet name



Adds an existing Office 365 user or an Office 365 security group to a SharePoint group.


Connects a SharePoint Online global administrator to a SharePoint Online connection (the SharePoint Online Administration Center). This cmdlet must be run before any other SharePoint Online cmdlets can run.


Disconnects from a SharePoint Online service.


Returns application errors.


Returns all installed applications.


Returns all deleted site collections that are in the Recycle Bin.


Returns one or more site collections.


Returns all the groups on the specified site collection.


Retrieves SharePoint Online company logs.


Returns the time when the SharePoint Online organization logs are collected.


Returns the SharePoint Online user or security group accounts that match given search criteria.


Shows all site templates that match the given identity.


Creates a new SharePoint Online site collection for the current company.


Creates a new group in a SharePoint Online site collection.


Removes a SharePoint Online deleted site collection from the Recycle Bin.


Sends a SharePoint Online site collection to the SharePoint Online Recycle Bin.


Removes a SharePoint Online group from a site collection.


Removes a user or a security group from a site collection or a group.


Checks and repairs the specified site collection and its contents.


Requests to create a copy of an existing site collection for the purposes of validating the effects of upgrade without affecting the original site.


Restores a SharePoint Online deleted site collection from the Recycle Bin.


Sets or updates the values of one or more properties for a site collection.


Updates the SharePoint Online owner and permission level on a group inside a site collection.


Sets properties on the SharePoint Online organization.


Configures properties on an existing user.


Tests a SharePoint Online site collection.


Starts the upgrade process on a site collection.

There are no cmdlets available for managing SharePoint objects at a lower scope than the site collection. The following examples show you some examples of the more common cmdlets. To see a list of site collections associated with a subscription or to see the details for a specific site collection use the Get-SPOSite cmdlet. Use the following command to retrieve the details about a specific SharePoint Online site collection. You need to specify the –Detailed option to retrieve information about CompatibilityLevel,ResourceUsageCurrent,ResourceUsageAverage, StorageUsageCurrent,Webcount and the Title.
Get-SPOSite –Detailed | select *

A slight variation on the script above allows you to get usage data about all of your different SharePoint Online site collections. The use of –limit all allows you to get all of the SP Online site collection, standard it will only return 200.
Get-SPOSite –limit all –detailed | Export-CSV –path MyReport.csv

There is however a workaround for manipulating objects in SharePoint Online at a lower level using the SharePoint Server 2013 Client Components SDK which enables remote development against SharePoint Server 2013. The Client Side Object Model (CSOM) is an API which allows you to do remote development against SharePoint in a fashion quite similar to the way that you would program on the server (but it is a subset and therefore does not expose all of the same classes).  There are 3 implementations of this API  - one for .NET, one for Silverlight and one for Javascript and they are meant to replace the SharePoint web services. The .Net managed model installs a number of dlls (all using the Microsoft.SharePoint.Client.* namespace) underneath

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI.
The next sample code shows how you can use these assemblies from Powershell which allow you to do a lot more with PowerShell and SharePoint Online.

$loc = "C:\Powershell" # Location of DLL's

$siteUrl = ""

$loginname = ""


Set-Location $loc


Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")

Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")


Write-Host "Please enter password for $($siteUrl):"

$pwd = Read-Host -AsSecureString

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)

# Authenticate against SharePoint Online

$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($loginname, $pwd)



$web = $ctx.Web 




Write-Host " Current web title is '

$($web.Title)', $($web.Url)"

In the next blogpost I will show some additional examples of how you can use PowerShell in combination with the SharePoint CSOM.


1 comment:

AMOL GHUGE said...

Thank you for sharing this valuable info! Great source of information.