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 tenant and its subsites using PnP PowerShell.
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.
To add different sites we need to follow these steps.
- # To add Classic Teamsite
-
- New-PnPTenantSite -Title My_TeamSite -Url http:
-
- # To add Modern Teamsite
-
- New-PnPSite -Type TeamSite -Title 'My_ModernTeamSite' -Alias My_ModernTeamSite
-
- # To add Communication site
-
- New-PnPSite -Type CommunicationSite -Title My_CommunicationSite -Url http:
From the below image you can see classic teamsite, modern teamsite and communication site created successfully.
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.