PowerShell Script To Create Bulk Sub Sites In Modern Site And Associate Group To That Site

Manimekalai
 
Technology Specialist
November 26, 2018
 
Rate this article
[Total: 0    Average: 0/5]
 
Views
1983

In this article, let us see how to create bulk sites by accessing csv in modern site and associate default group to that site.

 

 Clear-host
   
  Write-Output -msg "Start : Module loading Process"
  $modulePath = "C:\Program Files (x86)\SharePointPnPPowerShellOnline\Modules\SharePointPnPPowerShellOnline"
  import-module ($modulePath + "\SharePointPnPPowerShellOnline.psd1")
  Add-Type -Path ($modulePath + "\SharePointPnP.PowerShell.Online.Commands.dll")
  Add-Type -Path ($modulePath + "\Microsoft.SharePoint.Client.dll")
  Add-Type -Path ($modulePath + "\Microsoft.SharePoint.Client.Runtime.dll")
  Add-Type -Path ($modulePath + "\Microsoft.Online.SharePoint.Client.Tenant.dll")
  Write-Output -msg "End : Module loading Process"
   
  $cred = Get-Credential
  $csv = import-csv -Path "D:\Scripts\SiteInput.csv"
  Clear-Content "D:\SiteCreationLog.txt"
  try
  {
  foreach($value in $csv)
  {
  $ParentURL=$value.ParentSiteURL
  $TargetURL=$value.URL
  $WebTemplate=$value.Template
  $Title=$value.Title
  $Description=$value.Description
  $URL= $TargetURL -replace $ParentURL+'/'
  if($WebTemplate -eq "Team")
  {  
      #Connect to Site Collection       
      Connect-PnPOnline -Url $ParentURL -Credentials $cred
      #Create sub web with unique permissions
      New-PnPWeb -Title $Title -Url $URL -Description $Description -BreakInheritance -InheritNavigation -Template "STS#3"
      Disconnect-PnPOnline
      Connect-PnPOnline -Url $TargetURL -Credentials $cred
      $owner = (Get-PnPContext).Credentials.UserName
   
      #Create default groups for the new web
      $OwnerGroupName=$URL+" Owners"
      $MemberGroupName=$URL+" Members"
      $VisitorGroupName=$URL+" Visitors"
   
      $ownerGroup = New-PnPGroup -Title $OwnerGroupName -Owner $owner
      Set-PnPGroup -Identity $ownerGroup -SetAssociatedGroup Owners
      Set-PnPGroupPermissions -Identity $ownerGroup -AddRole "Full Control"
         
      $memberGroup = New-PnPGroup -Title $MemberGroupName -Owner $owner
      Set-PnPGroup -Identity $memberGroup -SetAssociatedGroup Members
      Set-PnPGroupPermissions -Identity $memberGroup -AddRole "Edit"
   
      $visitorGroup = New-PnPGroup -Title $VisitorGroupName -Owner $owner
      Set-PnPGroup -Identity $visitorGroup –SetAssociatedGroup Visitors
      Set-PnPGroupPermissions -Identity $visitorGroup -AddRole "Read"
       
      $TargetURL+" PASS" | Out-File -FilePath D:\SiteCreationLog.txt -Append
  }
  else
  {
      Write-Error "Invalid Template Name"
      $TargetURL+" Invalid Template Name"| Out-File -FilePath D:\SiteCreationLog.txt -Append
  }
  }
  }
  catch
  {
  Write-Output $_.Exception.Message 
  $TargetURL+" "+$_.Exception.Message | Out-File -FilePath D:\SiteCreationLog.txt -Append
  }

Author Info

Manimekalai
 
Technology Specialist
 
Rate this article
[Total: 0    Average: 0/5]
 
Having 6 years of experience in design and development of SharePoint applications, Mani has expertise in developing applications using Office 365, SharePoint 2013 & 2010, SPFX, SP Designer Workflows, Visual ...read more
 

Leave a comment