Working With SharePoint Site Collections On Office365 Tenant Site Using PnP PowerShell

Introduction

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.

Prerequisite

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):

  1. $siteurl = "https://abc-admin.sharepoint.com"  # Tenant Admin URL  
  2. Connect-SPOnline -Url $siteurl    
  3. $ctx = Get-SPOContext  

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: 
  1. $sites = Get-SPOTenantSite -Detailed -IncludeOneDriveSites -Force  
  2. Write-Host "There are " $sites.count " site collections present"  
  3. foreach($site in $sites){  
  4.     Write-Host "Title       : " $site.Title  
  5.     Write-Host "URL         : " $site.Url  
  6.     Write-Host "Template    : " $site.Template  
  7.     Write-Host "Status      : " $site.Status  
  8.     Write-Host "Storage (MB): " $site.StorageMaximumLevel  
  9.     Write-Host "Used (MB)   : " $site.StorageUsage  
  10.     Write-Host "Resources   : " $site.UserCodeMaximumLevel  
  11.     Write-Host "Owner       : " $site.Owner          
  12.     Write-Host "Sharing     : " $site.SharingCapability  
  13.     Write-Host "subsites    : " $site.WebsCount  
  14.     Write-Host "-----------------------------------------"    
  15. }  

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.

  1. $site = Get-SPOTenantSite -Url "https://abc.sharepoint.com/" -Detailed  
  2. Write-Host "Title       : " $site.Title  
  3. Write-Host "URL         : " $site.Url  
  4. Write-Host "Template    : " $site.Template  
  5. Write-Host "Status      : " $site.Status  
  6. Write-Host "Storage (MB): " $site.StorageMaximumLevel  
  7. Write-Host "Used (MB)   : " $site.StorageUsage  
  8. Write-Host "RESOURCES   : " $site.UserCodeMaximumLevel  
  9. Write-Host "Owner       : " $site.Owner          
  10. Write-Host "Sharing     : " $site.SharingCapability  
  11. Write-Host "subsites    : " $site.WebsCount  
  12. Write-Host "-----------------------------------------"  

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:

  • Title
  • URL - New site collection full URL
  • Description
  • 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.

  1. New-SPOTenantSite -Title "Test" -Url "https://abc.sharepoint.com/sites/Test" -Description "TestDesc" -Template "STS#0" -ResourceQuota 2 -StorageQuota 30 -Owner "[email protected]" -TimeZone 13  

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.

  1. Set-SPOTenantSite -Url "https://abc.sharepoint.com/sites/Test" -Title "TestUpdated" -Sharing ExistingExternalUserSharingOnly  

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.

  1. Remove-SPOTenantSite -Url "https://abc.sharepoint.com/sites/Test"  

The following code snippet helps to remove the site collection from the O365 tenant permanently.

  1. Remove-SPOTenantSite -Url "https://abc.sharepoint.com/sites/Test" -SkipRecycleBin -Force  

Summary

Thus, you have learned how to create, retrieve, update or delete the site collections on Office 365 tenant/admin sites, using PnP PowerShell.

Up Next
    Ebook Download
    View all
    Learn
    View all