Create Multiple Document Libraries From Custom Template Using PowerShell

The PowerShell Script given below will create a subsite with the blank template and multiple document libraries, using custom template.

In my case, custom template name is Farm Template.

Add-PSSnapin "Microsoft.SharePoint.Powershell".

  1. #create a subsite using blank Template  
  2.   
  3. $WebAppUrl="https://SharePoint.com/Sites/TEST"  
  4.  
  5. #->SubSite1<-  
  6. $SubSiteTitle1 = "Subsite"  
  7. $SubSite1Url = "Subsite"  
  8. New-SPWeb –url $WebAppUrl"/"$SubSite1Url -name $SubSiteTitle1 -Template "STS#1" –AddToTopNav –UseParentTopNav  
  9.  
  10. #load the custom template to new subsite  
  11.   
  12. $site = Get-SPSite https://SharePoint.com/Sites/TEST  
  13. $web = Get-SpWeb https://SharePoint.com/Sites/TEST/Subsite  
  14.   
  15. $custTemplate = $site.GetCustomListTemplates($web)  
  16.  
  17. #Verify the custom template name  
  18.   
  19. $custTemplate | select name  
  20.   
  21. write-host "Loading powershell module" -ForegroundColor Red  
  22.  
  23. # specify the source and destination sites  
  24. $sourceWeb = "https://SharePoint.com/Sites/TEST";  
  25. $DestinationWeb = "https://SharePoint.com/Sites/TEST/Subsite";  
  26.  
  27.  
  28. # get the source and destination sites web objects  
  29. $sourceWebObj = Get-SPWeb $sourceWeb;  
  30. $DestinationWebObj = Get-SPWeb $DestinationWeb;   
  31.  
  32. #creating new document libraries using custom template.  
  33.   
  34. $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::"FarmTemplate"  
  35. $DestinationWebObj.Lists.Add("01","My Doc Library",$custTemplate["FarmTemplate"]);  
  36. $DestinationWebObj.Lists.Add("02","My Doc Library",$custTemplate["FarmTemplate"]);  
  37. $DestinationWebObj.Lists.Add("03","My Doc Library",$custTemplate["FarmTemplate"]);  
  38. $DestinationWebObj.Lists.Add("04","My Doc Library",$custTemplate["FarmTemplate"]);  
  39. $DestinationWebObj.Lists.Add("05","My Doc Library",$custTemplate["FarmTemplate"]);   

You can add as many document libraries as you want.

I hope  this helps.

If you like it please leave a comment.