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 with 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).

$siteurl = "https://abc.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$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.

The new-SPOWeb command is used to create sub-sites on SharePoint sites. The necessary parameters to create the site are the Title, URL, and template. Other parameters that can be passed to create a 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.

NewSite

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

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 that can be retrieved using the command are title, GUID, and URL.

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

$siteurl = "https://abc.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  
# Get Current Context Site (Root)  
function RetrieveSite() {  
    $web = Get-SPOWeb  
    Write-Host "Title : " $web.Title  
    Write-Host "Description : " $web.Description  
    Write-Host "URL : " $web.Url  
}  
# Get Sub Site  
function RetrieveSubSite() {  
    $web = Get-SPOWeb "PnPSite1"  
    Write-Host "Title : " $web.Title  
    Write-Host "Description : " $web.Description  
    Write-Host "URL : " $web.Url  
}  
RetrieveSite # Get Current Context Site (Root)  
RetrieveSubSite # 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 the title, site logo, and URL.

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

$siteurl = "https://abc.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  
# Update Current Site  
function UpdateSite() {  
    $web = Get-SPOWeb  
    Set-SPOWeb -Web $web -Title "Title Update"  
}  
# Update Sub Site  
function UpdateSubSite() {  
    $web = Get-SPOWeb "/PnPSite1"  
    Set-SPOWeb -Web $web -Title "PnPSite1 Update"  
}  
# Call above Functions  
UpdateSite    # Update Current Site  
UpdateSubSite # 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 be passed is an identity. By using the Remove-SPOWeb command, the site or sub-site can be deleted.

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

$siteurl = "https://abc.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  
# Delete site  
function DeleteSubSite() {  
    Remove-SPOWeb -Identity "PnPSite1" -Force      
}  

Summary

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