In this article, you will learn how we can create, retrieve, update and delete SharePoint site collections on Office 365 tenant, using PnP PowerShell. The Client Side Object Model is used internally for these operations.
You need to have PowerShell 3.0, available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentation on the official site. The installers are available here. Online version installer is preferred. You can also install all the three installers for testing (SharePoint 2013, 2016, online).
The PnP PowerShell is supported from SharePoint 2013 On Premise and Office 365 versions. The following operations are compatible/tested on Office 365 environments/sites. These operations are not supported by On Premise versions.
Connect To Tenant Site:
To create, retrieve or modify the site collections, we need to connect to the SharePoint tenant (admin) site on Office 365, using the snippet given below. PnP PowerShell code, given below, helps in getting the current context of the admin site, using the Client Side Object Model (CSOM):
Once connected, you can carry out any of the operations mentioned below, based on the requirement.
The site collections can be accessed from the site collections page (The URL will look like https://abc-admin.sharepoint.com/_layouts/15/online/SiteCollections.aspx).
Retrieve Site Collections:
From the admin site, the available site collections can be retrieved, using PnP CSOM PowerShell.
Get-SPOTenantSite command is used to retrieve the site collections from the O365 SharePoint tenant site. The optional parameters used to retrieve the site collections are
- Detailed: Used to explicitly pull out information like the title. If not passed, the properties like the title value will be null.
- IncludeOneDriveSites: Includes one drive sites in the result.
- Force: Used to get the site collection results without any prompts.
The following code snippet helps to retrieve all the site collections from the O365 SharePoint tenant site:
The URL parameter is additionally used to get the information about the particular site collection. In the URL parameter, the site collection URL is passed to get the required information.
Create Site Collection:
New-SPOTenantSite command is used to create the site collections on O365 SharePoint tenant.
The necessary parameters to create the site collections are:
- URL - New site collection full URL
- Template – site template Id
- Resource quota – Number of the resources
- Storage quota – Storage limit
- Owner – Owner user Id
- Time zone – Time zone number, which can be retrieved, using Get-SPOTimeZoneId command.
The following code snippet helps to create a new site collection with the necessary parameters. The new site collection URL is passed.
Update Site Collection:
Set-SPOTenantSite is used to update the site collections present in the tenant site. The required parameter for the update is the site collection URL. The parameters like title, sharing and storage values can be updated.
The following code snippet helps to update the existing site collection with the necessary parameters. The existing site collection URL is passed.
Delete Site Collection:
Remove-SPOTenantSite command is used to delete a site collection. The required parameter for the delete operation is the existing site collection URL.
The following code snippet helps to remove a site collection from the O365 tenant. The site will be moved to the recycle bin.
The following code snippet helps to remove the site collection from the O365 tenant permanently.
Thus, you have learned how to create, retrieve, update or delete the site collections on Office 365 tenant/admin sites, using PnP PowerShell.