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.
SharePoint Online tutorial contents:
- 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
- Create a SharePoint Online Communication Site using PnP PowerShell
- Retrieve all Communication Site using PnP PowerShell
- Delete a Communication Site Using PnP PowerShell
- 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
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."
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 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."
Once you run the PowerShell cmdlet, you can see in the Active Sites tab, the SharePoint Online team site without an Office 365 group.
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."
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.
When you will click on the Yes button, then the Modern SharePoint Team Site will remove as shown in the below screenshot.
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.
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
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
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.
If you will check in the SharePoint Online Admin center, you can see the communication sites appears under Active Sites.
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."
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.
When you will click on the Yes button, then the SharePoint Communication Site will remove as shown in the below screenshot.
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.
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
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
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
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."
Once you run the script, you can see the user to the SharePoint group in the SharePoint Online site.
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."
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."
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."
This below PnP PowerShell command will help you to remove all existing site collection administrators from the SharePoint site collection.
Get-PnPSiteCollectionAdmin | Remove-PnPSiteCollectionAdmin
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."
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."
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."
You may like following PowerShell tutorials:
- Get SharePoint document library size using PowerShell
- Rename or hide/remove SharePoint List Title Column Programmatically using PnP CSOM
- Get all SharePoint subsites programmatically using PnP CSOM
- Delete All items from the SharePoint List using PnP core CSOM Library Programmatically
- Create Folder and Subfolder in SharePoint Document Library using PnP Core CSOM Library
- Insert/Add item to SharePoint Online list using PowerShell
- SharePoint Online check if File exists or not in document library using PowerShell
- How to check file size using PowerShell Script
- The remote server returned an error (401) Unauthorized SharePoint Online/2016/2013 CSOM or PowerShell
This SharePoint tutorial, we learned how to work with SharePoint online sites with PnP PowerShell cmdlets. We saw 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
I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site EnjoySharePoint.com