In this post, 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.
You need to have PowerShell 3.0, available on 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 / 2016 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).
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.
Other way of creating the site is to pass the parameters along with the command. The following command helps in creating a sub site.
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:
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 URL.
The following PowerShell snippet helps to update the site or sub site properties.
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.