In this blog, we are going to create classic TeamSite, modern team site and communication sites using PnP PowerShell. Also, we will see how to retrieve all site collections from the tenant and its subsites using PnP PowerShell. First, we need to connect to the Site. To perform connection add,
- First We Need to Connect to Site. To Perform Connection Add
$tenantSiteURL = Read-Host "Provide tenant url"
Connect-PnPOnline -Url $tenantSiteURL
#Executing this line will ask for credentials. Provide use name and password to connect.
2. To add different sites we need to follow these steps.
# To add Classic Teamsite
New-PnPTenantSite -Title My_TeamSite -Url http://portal/sites/My_TeamSite -Owner user@domain.com -TimeZone 4 -Template STS#0
# To add Modern Teamsite
New-PnPSite -Type TeamSite -Title 'My_ModernTeamSite' -Alias My_ModernTeamSite
# To add Communicationsite
New-PnPSite -Type CommunicationSite -Title My_CommunicationSite -Url http://portal/sites /My_CommunicationSite
From the below image you can see classic TeamSite, modern TeamSite and communication site created successfully.
3. To retrieve all sites and subsites from tenant we need to follow these steps.
# Get all site collections
$SiteCollections = Get-PnPTenantSite
foreach ($SiteCollection in $SiteCollections)
{
# Displays each sitecollection url, template , title in console window.
Write-Host "SiteCollections:" $SiteCollection.Url " - " $SiteCollection.Template " - " $SiteCollection.Title
# To retrieve each subsite and its property of a sitecollection we need to connect to site. using
“Connect-PnPOnline -Url $SiteCollection.Url -Credentials $cred” will ask to enter credentials each time. So here we will use alternative
-UseWebLogin
Connect-PnPOnline -Url $SiteCollection.Url -UseWebLogin
#To get all subsites
$subsites = Get-PnPSubWebs;
foreach ($subsite in $subsites){
# Displays each subsite url, template , title in console window.
Write-Host "Subsites:" $subsite.Url " - " $subsite.Template " - " $subsite.Title
}
}
From the below image we can see all the sites and its subsites created in a tenant.
This solution is brought to you by our SharePoint professionals.
Softree Technology employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.
Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best