Create Classic TeamSite, Modern Team Site, Communication Sites And Retrieve All Site Collections From Tenant Using PnP PowerShell

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,
  1. $tenantSiteURL = Read-Host "Provide tenant url"  
  2. Connect-PnPOnline -Url $tenantSiteURL  
  3. #Executing this line will ask for credentials. Provide use name and password to connect.  
 
To add different sites we need to follow these steps.
  1. # To add Classic Teamsite  
  2.   
  3. New-PnPTenantSite -Title My_TeamSite -Url http://portal/sites/My_TeamSite -Owner [email protected] -TimeZone 4 -Template STS#0  
  4.  
  5. # To add Modern Teamsite  
  6.   
  7. New-PnPSite -Type TeamSite -Title 'My_ModernTeamSite' -Alias My_ModernTeamSite  
  8.  
  9. # To add Communication site  
  10.   
  11. 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.
 
 
To retrieve all sites and subsites from tenant, we need to follow these steps.
  1. #Get all site collections  
  2. $SiteCollections = Get - PnPTenantSite  
  3. foreach($SiteCollection in $SiteCollections) {  
  4.     #Displays each sitecollection url, template, title in console window.  
  5.     Write - Host "SiteCollections:"  
  6.     $SiteCollection.Url " - "  
  7.     $SiteCollection.Template " - "  
  8.     $SiteCollection.Title  
  9.     # 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  
  10.     Connect - PnPOnline - Url $SiteCollection.Url - UseWebLogin  
  11.     # To get all subsites  
  12.     $subsites = Get - PnPSubWebs;  
  13.     foreach($subsite in $subsites) {  
  14.         #Displays each subsite url, template, title in console window.  
  15.         Write - Host "Subsites:"  
  16.         $subsite.Url " - "  
  17.         $subsite.Template " - "  
  18.         $subsite.Title  
  19.     }  
  20. }  
From the below image we can see all the sites and its subsites created in a tenant.