Create, Retrieve, Update Or Delete Sites On SharePoint Using PnP PowerShell


Introduction

In this article, you will learn how we can create, retrieve, update and delete SharePoint sites, 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 cmdlets from the official site.


The following operations will be compatible for SharePoint 2013 on-premise or Office 365 versions.


Connect To Site

Connect to the site, using the snippet given below. The below PnP PowerShell code helps in getting the current context of the site, using the client side object model (CSOM).

  1. $siteurl = "https://abc.sharepoint.com"

  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.


Create Sub Site

From the existing site collection, the sub sites can be created. The same can be accomplished, using PnP CSOM PowerShell.


New-SPOWeb command is used to create sub sites on SharePoint sites. The necessary parameters to create the site are Title, URL and template. Other parameters that can be passed to create site are description, break inheritance etc.


There are two ways to create a site. When the command New-SPOWeb is entered on the PowerShell, you will be prompted for the parameters. The site will be created. The screenshot given below shows the scenario:


Another way of creating the site is to pass the parameters along with the command. The following command helps in creating a sub site.

  1. New-SPOWeb -Title "PnPSite1" -Description "PnPDescription1" -Url "PnPSite1" -Template "STS#0"


Retrieve Site or Sub Site

From the site collection, the sub site properties can be retrieved. The same can be accomplished, using PnP CSOM PowerShell.


Get-SPOWeb is used to retrieve the sub site properties. Along with the command, the sub site name or the URL is passed to retrieve the information. When the command is just passed without any parameters, the current site properties will be retrieved.


The basic properties which can be retrieved using the command are title, GUID, URL.

The following PowerShell code snippet helps to retrieve the site properties:

  1. $siteurl = "https://abc.sharepoint.com"

  2. Connect-SPOnline -Url $siteurl  

  3. $ctx = Get-SPOContext  

  4. # Get Current Context Site (Root)

  5. function RetrieveSite(){      

  6. $web = Get-SPOWeb      

  7. Write-Host "Title : " $web.Title      

  8. Write-Host "Description : " $web.Description      

  9. Write-Host "URL : " $web.Url  }  

  10. # Get Sub Site

  11. function RetrieveSubSite(){      

  12. $web = Get-SPOWeb "PnPSite1"     

  13. Write-Host "Title : " $web.Title      

  14. Write-Host "Description : " $web.Description      

  15. Write-Host "URL : " $web.Url  }  RetrieveSite 

  16. # Get Current Context Site (Root)  RetrieveSubSite 

  17. # Get Sub Site  

Update Site

From the site collection, the sub site properties can be updated. The same can be accomplished, using PnP CSOM PowerShell.


To change the site properties, retrieve the corresponding site, using title or URL. The parameters are not required if you are trying to change the current context site (root) properties.


Update the site with the necessary parameters, using Set-SPOWeb.


The parameters that can be changed are title, site logo, and  URL.


The following PowerShell snippet helps to update the site or sub site properties.

  1. $siteurl = "https://abc.sharepoint.com"

  2. Connect-SPOnline -Url $siteurl  

  3. $ctx = Get-SPOContext  

  4. # Update Current Site

  5. function UpdateSite(){      

  6. $web = Get-SPOWeb      

  7. Set-SPOWeb -Web $web -Title "Title Update" }  

  8. # Update Sub Site

  9. function UpdateSubSite(){      

  10. $web = Get-SPOWeb "/PnPSite1"     

  11. Set-SPOWeb -Web $web -Title "PnPSite1 Update" }  

  12. # Call above Functions UpdateSite 

  13. # Update Current Site  UpdateSubSite 

  14. # Update Sub Site  


Delete Site

 From the site collection, the sub sites can be deleted. The same can be accomplished, using PnP CSOM PowerShell.


The parameter that needs to passed is an identity. By using Remove-SPOWeb command, the site or sub site can be deleted.


The following PowerShell snippet helps to delete the site from the site collection.

  1. $siteurl = "https://abc.sharepoint.com"

  2. Connect-SPOnline -Url $siteurl  

  3. $ctx = Get-SPOContext  

  4. # Delete site function DeleteSubSite(){      

  5. Remove-SPOWeb -Identity "PnPSite1" -Force     

  6.  }  


Summary Thus, you have learned how to create, retrieve, update or delete the site, using PnP PowerShell on SharePoint 2013 / SharePoint online sites.