PnP PowerShell commands for SharePoint Online Site

PnP PowerShell is now, widely used for SharePoint Online as well as SharePoint on-premises versions like SharePoint 2013, SharePoint 2016 and SharePoint 2019.

If you are new to PnP PowerShell, have a look at an article on Getting Started with PnP PowerShell in SharePoint.

Here, we will see how to use PnP PowerShell commands to work with SharePoint sites. We will see below examples:

  • Create a Modern SharePoint Online Team Site with an Office 365 Group Using PnP PowerShell
  • Create a Modern SharePoint Team Site without an Office 365 Group Using PnP PowerShell
  • Retrieve all Modern SharePoint Team Site Using PnP PowerShell
  • Remove a Modern SharePoint Team Site Using PnP PowerShell
  • PowerShell Code to remove a Modern SharePoint Team Site
  • Remove the Modern SharePoint Team Site (with Site URL) with Force
  • Remove the Modern SharePoint Team Site (with Site URL) from the Recycle Bin
  • Create a SharePoint Communication Site Using PnP PowerShell
  • Retrieve all Communication Site Using PnP PowerShell
  • Delete a Communication Site Using PnP PowerShell
  • PowerShell Code to delete a Communication Site:
  • Delete the SharePoint Communication Site (with Site URL) with Force:
  • Delete the SharePoint Communication Site (with Site URL) from the Recycle Bin
  • Get Created Date of SharePoint Online Site Using PnP PowerShell
  • Add User to SharePoint Group Using PnP PowerShell
  • Add Site Collection Administrator SharePoint Online Using PnP PowerShell
  • Get Site Collection Administrator SharePoint Online Using PnP PowerShell
  • Remove Site Collection Administrator SharePoint Online Using PnP PowerShell
  • Get all Site Collections in SharePoint Online Using PnP PowerShell
  • Get all Sub Sites in SharePoint Online Using PnP PowerShell
  • Restore SharePoint Online Site Using PnP PowerShell

All these PowerShell commands you can run in Windows PowerShell ISE.

Create a modern team site using pnp PowerShell in SharePoint Online with Office 365 group

Now, we will see how to create a SharePoint Online modern team site using PnP PowerShell. This will be an Office 365 group connected team site in SharePoint.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

New-PnPSite -Type TeamSite -Title 'TSInfo Modern Team' -Alias "TSInfoModernTeam" -IsPublic
Write-Host "The modern team site has been created successfully."
create modern team site using pnp powershell
Create a modern team site using pnp PowerShell

Once you run the command, you can see the team site get created which is available in Active sites on SharePoint Online admin center.

create modern team site using powershell
Create a modern team site using pnp PowerShell

Create a modern team site without an office 365 group using PnP PowerShell

We can easily create a modern team site without an Office 365 group using PnP PowerShell in SharePoint Online.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

New-PnPTenantSite `
  -Title "TSInfo Modern Team Without Group" `
  -Url "https://tenant.sharepoint.com/sites/TSInfoModernTeamWithoutGroup" `
  -Description "Modern Site" `
  -Owner "preeti@tenant.onmicrosoft.com" `
  -Lcid 1033 `
  -Template "STS#3" `
  -TimeZone 10 `
  -Wait
Write-Host "The modern team site without Office 365 Group has been created successfully."
sharepoint create modern team site without group pnp powershell
Create a modern team site without an office 365 group using PnP PowerShell

Once you run the PowerShell cmdlet, you can see in the Active Sites tab, the SharePoint Online team site without an Office 365 group.

create a modern team site without an office 365 group
Create a modern team site without an office 365 group using PnP PowerShell

PnP Powershell get all modern team sites

Now, we will see how to get all modern team sites in SharePoint Online using PnP PowerShell.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

Get-PnPTenantSite -Template GROUP#0
Write-Host "Retrieved all Modern Team Sites."
Retrieve all Modern Team Sites Using PnP PowerShell
get all modern team sites using PowerShell

Remove a Modern SharePoint Team Site using PnP PowerShell

Below is the PnP PowerShell command to remove a Modern SharePoint Team Site Collection from your Office 365 tenant.

NOTE:

Before removing the Modern Team Site from your office 365 tenant, You must ensure to delete the SharePoint Office 365 Group which will be present with the same existing Modern Team Site name.
$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

Remove-PnPTenantSite -Url https://tenant.sharepoint.com/sites/TSInfoModernTeam
Write-Host "SharePoint Online Team Site is Removed."

Once you will run the above PowerShell script, then a pop-up window will appear to ensure to delete the Modern SharePoint Team Site.

Remove Modern Team Site using PnP PowerShell

When you will click on the Yes button, then the Modern SharePoint Team Site will remove as shown in the below screenshot.

Remove SharePoint Modern Team Site using PnP PowerShell

Now when you will go to the SharePoint Deleted sites in the SharePoint Online admin center, you can see the deleted SharePoint Modern Site like below.

Now, you can see the TSInfo Modern Team Site in Deleted sites.

Remove SharePoint Online Modern Team Site using PnP PowerShell

Remove Modern SharePoint Team Site (with Site URL) with Force

This below PnP PowerShell Code will remove the Modern SharePoint Team Site with the Site URL (https://tenant.sharepoint.com/sites/TSInfoModernTeam) with force and it will skip the recycle bin.

Remove-PnPTenantSite -Url https://tenant.sharepoint.com/sites/TSInfoModernTeam -Force -SkipRecycleBin

Remove Modern SharePoint Team Site (with Site URL) from the Recycle Bin

This below PnP PowerShell cmdlets will remove the Modern SharePoint Team Site with the Site URL (https://tenant.sharepoint.com/sites/TSInfoModernTeam) from the Recycle bin.

Remove-PnPTenantSite -Url https://tenant.sharepoint.com/sites/TSInfoModernTeam -FromRecycleBin

Create a SharePoint Online Communication Site using PnP PowerShell

We can use PnP PowerShell commands to create a SharePoint Online communication site.

Below is the PowerShell cmdlets to create a communication site in SharePoint Online.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

New-PnPSite -Type CommunicationSite -Title 'TSInfo Communication Site' -Url "https://tenant.sharepoint.com/sites/TSInfoCommunicationSite" -SiteDesign "Showcase"
Write-Host "The communication site has been created successfully.
Create a Communication Site Using PnP PowerShell
Create a SharePoint Online Communication Site using PnP PowerShell

If you will check in the SharePoint Online Admin center, you can see the communication sites appears under Active Sites.

Create Communication Site Using PnP PowerShell
Create a SharePoint Online Communication Site using PnP PowerShell

Retrieve all Communication Site using PnP PowerShell

Below is the PowerShell script to get all communication sites from Office 365 using PnP PowerShell.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

Get-PnPTenantSite -Template SITEPAGEPUBLISHING#0
Write-Host "Retrieved all Communication Sites."
Retrieve all Communication Site Using PnP PowerShell
Retrieve all Communication Site using PnP PowerShell

Delete a Communication Site Using PnP PowerShell

Below is the PowerShell script to delete a Communication Site from Office 365 tenant using PnP PowerShell.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

Remove-PnPTenantSite -Url https://tenant.sharepoint.com/sites/TSInfoCommunicationSite
Write-Host "SharePoint Online Communication Site is Removed."

Once you run the script, a pop-up window will appear to confirm to delete the SharePoint Communication Site.

Delete a Communication Site Using PnP PowerShell

When you will click on the Yes button, then the SharePoint Communication Site will remove as shown in the below screenshot.

Delete a SharePoint Communication Site Using PnP PowerShell
Delete a Communication Site Using PnP PowerShell

Now when you will go to the SharePoint Deleted sites, then you can see your deleted SharePoint Communication Site will be there as like below.

As I have removed the TSInfo Communication Site, So it is showing in my SharePoint Deleted sites page.

Delete SharePoint Communication Site Using PnP PowerShell
Delete a Communication Site Using PnP PowerShell

Delete a SharePoint Communication Site (with Site URL) with Force

This below PnP PowerShell Code will delete the Communication Site with the Site URL (https://tenant.sharepoint.com/sites/TSInfoCommunicationSite) with force and it will skip the recycle bin.

Remove-PnPTenantSite -Url https://tenant.sharepoint.com/sites/TSInfoCommunicationSite -Force -SkipRecycleBin

Delete the SharePoint Communication Site (with Site URL) from the Recycle Bin

This below PnP PowerShell Code will delete the SharePoint Communication Site with the Site URL (https://tenant.sharepoint.com/sites/TSInfoCommunicationSite) from the Recycle bin.

Remove-PnPTenantSite -Url https://tenant.sharepoint.com/sites/TSInfoCommunicationSite -FromRecycleBin

Get Created Date of SharePoint Online Site Using PnP PowerShell

By using PnP PowerShell cmdlets, we can easily get created date of a SharePoint Online site.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/tsinfoclassic/" -Credentials $cred

$web=Get-PnPWeb -Includes Created
write-host $web.Created
Get Created Date of SharePoint Online Site Using PnP PowerShell
Get Created Date of SharePoint Online Site Using PnP PowerShell

Add User to SharePoint Group Using PnP PowerShell

Now, we will check how to add user to SharePoint group using PnP PowerShell.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
$siteURL= "https://tenant.sharepoint.com/sites/tsinfoclassic/"
Connect-PnPOnline -Url $siteURL -Credentials $cred

Add-PnPUserToGroup -LoginName Bijay@tenant.onmicrosoft.com -Identity 'TSInfoGroup'
Write-Host "A user is added to the SharePoint Group."
Add User to SharePoint Group Using PnP PowerShell
Add User to SharePoint Group Using PnP PowerShell

Once you run the script, you can see the user to the SharePoint group in the SharePoint Online site.

Add Users to SharePoint Group Using PnP PowerShell
Add User to SharePoint Group Using PnP PowerShell

Add Site Collection Administrator in SharePoint Online Site using PnP PowerShell

Now, we will see how to add site collection administrator’s to SharePoint Online list using PnP PowerShell.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

Add-PnPSiteCollectionAdmin -Owners @("preeti@tenant.onmicrosoft.com", "bijay@tenant.onmicrosoft.com")
Write-Host "Added additional Site Collection Administrator."
Add Site Collection Administrator SharePoint Online Using PnP PowerShell
Add Site Collection Administrator in SharePoint Online Site

Get Site Collection Administrator SharePoint Online Using PnP PowerShell

Below is the PowerShell cmdlets to get the site collection administrators using PnP PowerShell in SharePoint Online Office 365.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

Get-PnPSiteCollectionAdmin
Write-Host "Retrieved Site Collection Administrator."
Get Site Collection Administrator SharePoint Online Using PnP PowerShell
Get Site Collection Administrator SharePoint Online Using PnP PowerShell

Remove Site Collection Administrator SharePoint Online using PnP PowerShell

Now, we will see how to remove SharePoint site collection administrator usng PnP PowerShell in SharePoint Online Office 365.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

Remove-PnPSiteCollectionAdmin -Owners @("preeti@tenant.onmicrosoft.com", "bijay@tenant.onmicrosoft.com")
Write-Host "Removed site collection owners from the site collection in the current context."
Remove Site Collection Administrator SharePoint Online Using PnP PowerShell

This below PnP PowerShell command will help you to remove all existing site collection administrators from the SharePoint site collection.

Get-PnPSiteCollectionAdmin | Remove-PnPSiteCollectionAdmin

Get all Site Collections in SharePoint Online Using PnP PowerShell

Now, we will see how to get site collections in SharePoint Online using pnp PowerShell.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

Get-PnPTenantSite
Write-Host "Retrieved all Site Collections."
Get all Site Collections in SharePoint Online Using PnP PowerShell
Get all Site Collections in SharePoint Online Using PnP PowerShell

Get all Sub Sites in SharePoint Online Using PnP PowerShell

If you want to get all the sub sites from a SharePoint Online site collection using PnP PowerShell.

Below is the PnP PowerShell script to get all sub sites in SharePoint Online site collection.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
$siteURL= "https://tenant.sharepoint.com/sites/SharePointSky/"
Connect-PnPOnline -Url $siteURL -Credentials $cred

Get-PnPSubWebs
Write-Host "All the Sub Sites are retrieved from SharePoint Online."
Get all Sub Sites in SharePoint Online Using PnP PowerShell
Get all Sub Sites in SharePoint Online Using PnP PowerShell

Restore SharePoint Online Site Using PnP PowerShell

Below is the PnP PowerShell script to restore a SharePoint Online site using PnP PowerShell.

$userName = "preeti@tenant.onmicrosoft.com"
$passWord = "**********"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord
$DeletedSiteURL= "https://tenant.sharepoint.com/sites/TSInfoModernTeam"
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com/" -Credentials $cred

Restore-PnPTenantRecycleBinItem -Url $DeletedSiteURL -Force -ErrorAction Stop
Write-Host "The SharePoint Deleted Site has been restored successfully."
Restore SharePoint Online Site Using PnP PowerShell
Restore SharePoint Online Site Using PnP PowerShell
Restore SharePoint Online Sites Using PnP PowerShell
Restore SharePoint Online Site Using PnP PowerShell

You may like following PowerShell tutorials:

In this SharePoint tutorial, we learned how to work with SharePoint online sites with PnP PowerShell cmdlets. We saw the below examples:

  • Create a modern team site using pnp PowerShell in SharePoint Online with Office 365 group
  • Create a modern team site without an office 365 group using PnP PowerShell
  • PnP Powershell get all modern team sites
  • Remove a Modern SharePoint Team Site using PnP PowerShell
  • Remove Modern SharePoint Team Site (with Site URL) with Force
  • Remove Modern SharePoint Team Site (with Site URL) from the Recycle Bin
  • Create a SharePoint Online Communication Site using PnP PowerShell
  • Retrieve all Communication Site using PnP PowerShell
  • Delete a Communication Site Using PnP PowerShell
  • Delete a SharePoint Communication Site (with Site URL) with Force
  • Delete the SharePoint Communication Site (with Site URL) from the Recycle Bin
  • Get Created Date of SharePoint Online Site Using PnP PowerShell
  • Add User to SharePoint Group Using PnP PowerShell
  • Add Site Collection Administrator in SharePoint Online Site using PnP PowerShell
  • Get Site Collection Administrator SharePoint Online Using PnP PowerShell
  • Remove Site Collection Administrator SharePoint Online using PnP PowerShell
  • Get all Site Collections in SharePoint Online Using PnP PowerShell
  • Get all Sub Sites in SharePoint Online Using PnP PowerShell
  • Restore SharePoint Online Site Using PnP PowerShell
>