Home

SharePoint Online: Create subsite using PowerShell script based on CSOM or PnP

%3CLINGO-SUB%20id%3D%22lingo-sub-203244%22%20slang%3D%22en-US%22%3ESharePoint%20Online%3A%20Create%20subsite%20using%20PowerShell%20script%20based%20on%20CSOM%20or%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-203244%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20Office%20365%2C%20the%20SharePoint%20%22Save%20as%20Site%20template%22%20is%20not%20available%20anymore.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20many%20case%2C%20this%20strategy%20was%20used%20by%20power%20users%20to%20create%20a%20templated%20site%20they%20can%20save%20and%20use%20after%20for%20the%20new%20site%20respecting%20that%20format%20(cupcake%20notion).%3C%2FP%3E%0A%3CP%3EIn%20SharePoint%20Online%2C%20that%20option%20is%20not%20possible%20and%20Microsoft%20will%20not%20invite%20the%20users%20to%20create%20the%20subsites%20as%20much%20as%20possible%2C%20the%20solution%20concept%20is%20now%26nbsp%3Bflat%20and%20not%20anymore%20hierarchal.%3C%2FP%3E%0A%3CP%3ESharePoint%20Online%20is%20only%20a%20branch%20of%20all%20the%20applications%20or%20tools%20given%20with%20an%20Office%20365%20Group.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20for%20many%26nbsp%3Bcases%2C%20we%20need%20to%20continue%20that%20usage%20of%20template%20(preparing%20the%20Cupcake)%20to%20industrialize%20some%20repeated%20activities%20(projects%2C%20analysis%2C%20assessment%2C%20%E2%80%A6).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20now%2C%20the%20only%20way%20is%20to%20think%20about%20that%20industrialization%20with%20PowerShell%20Scripting.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInto%20that%20page%2C%20you%20will%20find%202%20versions%20I%20wrote%20for%20my%20needs%2C%20but%20you%20can%20easily%20adapt%20it%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EFirst%20script%20based%20on%20CSOM%20only%3C%2FLI%3E%0A%3CLI%3ESecond%20script%20based%20on%20PnP%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E---------------------------------------------------------%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23ff6600%22%3E%3CSTRONG%3ECSOM%20PowerShell%20script%3A%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EThis%20one%20is%20based%20on%20many%20articles%20I%20found%20on%20Internet%20associated%20with%20my%20development%2C%20so%20the%20sources%20which%20help%20me%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.sharepointappie.nl%2Fwebnavigationsettings-csom%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.sharepointappie.nl%2Fwebnavigationsettings-csom%2F%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Ffr-fr%2Flibrary%2Foffice%2Fmicrosoft.sharepoint.client.list.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmsdn.microsoft.com%2Ffr-fr%2Flibrary%2Foffice%2Fmicrosoft.sharepoint.client.list.aspx%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgist.github.com%2Fazam-a%2Feba51bfbf922d34e8c87%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgist.github.com%2Fazam-a%2Feba51bfbf922d34e8c87%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F165235%2Fsetting-sharepoint-group-as-spgroup-owner-using-powershell%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F165235%2Fsetting-sharepoint-group-as-spgroup-owner-using-powershell%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fwww.sharepointfire.com%2F2016%2F01%2Fset-custom-permissions-site-sharepoint-online-powershell%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.sharepointfire.com%2F2016%2F01%2Fset-custom-permissions-site-sharepoint-online-powershell%2F%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFeel%20free%20to%20adapt%20and%20use%20it%20as%20you%20need.%3C%2FP%3E%0A%3CPRE%3E%23%20VALUE%20TO%20ADAPT%20!!!!!%0A%23%20--------------------------------------------------------------------------------------------%0A%24SiteIntegrationUrl%20%3D%22https%3A%2F%2Ftenant.sharepoint.com%2Fsites%2FSiteCollection%2FSubsite1%22%0A%24SubSiteURL%20%3D%20%22testsubsite%22%0A%24SubSiteName%20%3D%22_TESTSUBSITE%22%0A%24EmailAddressAccessRequest%20%3D%20%22emailaddresstosupport%40domain.com%22%0A%24RootSiteOwnerGroup%20%3D%20%22Root%20Site%20Collection%20Owners%22%0A%23%20--------------------------------------------------------------------------------------------%0A%0A%0Afunction%20Load-DLLandAssemblies%0A%7B%0A%20%5Bstring%5D%24defaultDLLPath%20%3D%20%22%22%0A%0A%20%23%20Load%20assemblies%20to%20PowerShell%20session%20%0A%0A%20%24defaultDLLPath%20%3D%20%22C%3A%5CProgram%20Files%5CSharePoint%20Online%20Management%20Shell%5CMicrosoft.Online.SharePoint.PowerShell%5CMicrosoft.SharePoint.Client.dll%22%0A%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadFile(%24defaultDLLPath)%0A%0A%20%24defaultDLLPath%20%3D%20%22C%3A%5CProgram%20Files%5CSharePoint%20Online%20Management%20Shell%5CMicrosoft.Online.SharePoint.PowerShell%5CMicrosoft.SharePoint.Client.Runtime.dll%22%0A%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadFile(%24defaultDLLPath)%0A%0A%20%24defaultDLLPath%20%3D%20%22C%3A%5CProgram%20Files%5CSharePoint%20Online%20Management%20Shell%5CMicrosoft.Online.SharePoint.PowerShell%5CMicrosoft.Online.SharePoint.Client.Tenant.dll%22%0A%20%5BSystem.Reflection.Assembly%5D%3A%3ALoadFile(%24defaultDLLPath)%0A%7D%0A%0A%0A%23Custom%20Function%20to%20Check%20if%20Site%20Collection%20Exists%20in%20Given%20URL%0AFunction%20Check-SiteExists(%24MySiteURL%2C%20%24MyCredentials)%0A%7B%0A%20%20%20%20%23Setup%20context%0A%20%20%20%20%24Ctx%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24MySiteURL)%0A%20%24Ctx.Credentials%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24MyCredentials.UserName%2C%24MyCredentials.Password)%0A%20%20%20%20%24Web%20%3D%20%24Ctx.Web%0A%20%20%20%20%24Ctx.Load(%24web)%0A%20%20%20%20%20%0A%20%20%20%20Try%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24Ctx.ExecuteQuery()%0A%20%20%20%20%20%20%20%20%20%20%20%20Return%20%24True%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20Catch%20%5BException%5D%20%7B%0A%20%20%20%20%20%20Write-host%20%22%20%26gt%3B%26gt%3B%26gt%3B%26gt%3B%20ERROR%20MESSAGE%3A%22%2C%20%24_.Exception.Message%20-f%20Red%0A%20%20%20%20%20%20Return%20%24False%0A%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%0A%7D%0A%0A%0AFunction%20Create-SPWebList(%5Bstring%5D%24MyShortTitle%2C%20%5Bstring%5D%24MyDescription%2C%20%5Bint%5D%24MyListTemplateType%2C%20%5BMicrosoft.SharePoint.Client.ClientContext%5D%24Mycontext)%0A%7B%0A%20Write-Host%20%22%20---------------------------------------------------------%22%0A%0A%20%24lci%20%3D%20New-Object%20Microsoft.SharePoint.Client.ListCreationInformation%0A%20%24lci.Title%20%3D%20%24MyShortTitle%0A%20%24lci.Description%20%3D%20%24MyDescription%0A%20%24lci.TemplateType%20%3D%20%24MyListTemplateType%20%20%20%23see%3A%20https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fmicrosoft.sharepoint.splisttemplatetype.aspx%0A%20%24list%20%3D%20%24Mycontext.web.lists.add(%24lci)%0A%20%24Mycontext.load(%24list)%0A%20%23send%20the%20request%20containing%20all%20operations%20to%20the%20server%0A%20try%7B%0A%20%20%24Mycontext.executeQuery()%0A%20%20write-host%20%22%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20info%3A%20List%20Created%20%24(%24MyShortTitle)%22%20-foregroundcolor%20green%0A%20%7D%0A%20catch%7B%0A%20%20write-host%20%22%20%20%26gt%3B%26gt%3B%26gt%3B%20List%20Creation%20Error%20info%3A%20%24(%24_.Exception.Message)%22%20-foregroundcolor%20red%0A%20%7D%0A%0A%20Write-Host%20%22%20---------------------------------------------------------%22%0A%7D%0A%0A%0A%23%20---------------------------------------------------------------------------------------------------------------%0A%0A%0ALoad-DLLandAssemblies%0A%0A%23get%20and%20save%20your%20O365%20credentials%0A%5Bstring%5D%24username%20%3D%20%22AdminSiteCollection%40tenant.onmicrosoft.com%22%0A%5Bstring%5D%24PwdTXTPath%20%3D%20%22C%3A%5CSECUREDPWD%5CExportedPWD-%24(%24username).txt%22%0A%24secureStringPwd%20%3D%20ConvertTo-SecureString%20-string%20(Get-Content%20%24PwdTXTPath)%0A%24cred%20%3D%20New-Object%20System.Management.Automation.PSCredential%20-ArgumentList%20%24username%2C%20%24secureStringPwd%0A%0A%24DocLibTitleLong%20%3D%20%22Given%20Documents%22%0A%0A%24DocLibTitle%20%3D%20-join(%24SubSiteURL%2C%20%22GivenDocuments%22)%0A%24DocLibTitleLong%20%3D%20-join(%24SubSiteName%2C%20%22%20Given%20Documents%22)%0A%24SubSiteTitle%20%3D%20-join(%24SubSiteName%2C%20%22%20-%20Subsite%20Usage%22)%0A%0A%24SubSiteOwnerOwners%20%3D%20-join(%24SubSiteName%2C%20%22%20Subsite%20Usage%20Web%20Owners%22)%0A%24SubSiteOwnerMembers%20%3D%20-join(%24SubSiteName%2C%20%22%20Subsite%20Usage%20Web%20Members%22)%0A%24SubSiteOwnerVisitors%20%3D%20-join(%24SubSiteName%2C%20%22%20Subsite%20Usage%20Web%20Visitors%22)%0A%0A%24SubSiteFullUrl%20%3D%20-join(%24SiteIntegrationUrl%2C%20%22%2F%22%2C%20%24SubSiteURL)%0A%0A%23%20---------------------------------------------------------------------------------------------------------------%0A%23connect%20to%20the%20web%20site%20using%20the%20stored%20credentials%0AWrite-host%20%22%20%22%0AWrite-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0AWrite-host%20%22%20----%20CONNECT%20THE%20SITE%20---%20%22%20-ForegroundColor%20green%0AWrite-host%20%22%20%20%20CONNECTED%20SITE%3A%22%2C%20%24SiteIntegrationUrl%20%20-ForegroundColor%20Yellow%0A%0A%24Myctx%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24SiteIntegrationUrl)%0A%0A%24Myctx.Credentials%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24cred.UserName%2C%24cred.Password)%0A%24Myctx.RequestTimeout%20%3D%201000000%20%23%20milliseconds%0A%24MyspoRootweb%20%3D%20%24Myctx.Web%0A%24Myctx.Load(%24MyspoRootweb)%0A%24Myctx.ExecuteQuery()%0A%0AWrite-Host%20%22%20%22%0AWrite-Host%20%22%20---------------------------------------------------------%22%0AWrite-Host%20%22%20%20%26gt%3B%26gt%3B%26gt%3B%26gt%3B%20%23%20Server%20Version%3A%22%20%24Myctx.ServerVersion%20%22%20%23%20%26lt%3B%26lt%3B%26lt%3B%26lt%3B%26lt%3B%26lt%3B%22%20-ForegroundColor%20Green%20%0AWrite-Host%20%22%20---------------------------------------------------------%22%0AWrite-Host%20%22%20%22%0A%0AWrite-host%20%22%20--------------------------------------------------------%20%22%0AWrite-host%20%22%20%20%20--%26gt%3B%26gt%3B%20RootSite%3A%22%2C%20%24MyspoRootweb.URL%20-ForegroundColor%20green%0A%0AWrite-host%20%22%20%22%0AWrite-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0AWrite-host%20%22%20----%20START%20THE%20SUBSITE%20CREATION%20%20---%20%22%20-ForegroundColor%20green%0AWrite-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0A%0A%0A%24SiteExists%20%3D%20Check-SiteExists%20%24SubSiteFullUrl%20%24cred%0A%0Aif(%24SiteExists%20-ne%20%24true)%0A%7B%0A%0A%20Write-host%20%22%20%20%20NEW%20SITE%20URL%20TO%20CREATE%3A%22%2C%20%24SubSiteFullUrl%20%20-ForegroundColor%20Yellow%0A%20%24Subsite%20%3D%20New-Object%20Microsoft.SharePoint.Client.WebCreationInformation%0A%20%24Subsite.WebTemplate%20%3D%20%22STS%230%22%0A%20%24Subsite.Title%20%3D%20%24SubSiteTitle%0A%20%24Subsite.Url%20%3D%20%24SubSiteURL%0A%20%24Subsite.Language%20%3D%20%221033%22%0A%20%24Subsite.Description%20%3D%20%24SubSiteTitle%0A%0A%20%24SubWeb%20%3D%20%24Myctx.Web.Webs.Add(%24Subsite)%0A%20%24Myctx.Load(%24SubWeb)%0A%20Try%7B%0A%20%20%24Myctx.ExecuteQuery()%0A%20%20Write-host%20%22%20%22%0A%20%20Write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%20Success%20info%20creating%20site%20for%20%24SubSiteURL%22%20-ForegroundColor%20green%0A%20%7D%0A%20Catch%7B%0A%20%20Write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%20Error%20info%20creating%20subsite%20for%20%24SubSiteURL%20-%20%24_.Exception.Message%22%20-ForegroundColor%20red%0A%20%7D%0A%0A%20%24context%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24SubSiteFullUrl)%0A%20%24context.Credentials%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24cred.UserName%2C%24cred.Password)%0A%20%24web%20%3D%20%24context.web%0A%20%24context.load(%24web)%0A%20%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Implement%20the%20Top%20menu%20navigation%22%20-ForegroundColor%20green%0A%20%24web.Navigation.UseShared%20%3D%20%24true%0A%20%24context.ExecuteQuery()%3B%0A%20%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Implement%20the%20Access%20Request%20settings%3A%20%22%2C%20%24EmailAddressAccessRequest%20-ForegroundColor%20green%0A%20%20%20%20%24Web.RequestAccessEmail%20%3D%20%24EmailAddressAccessRequest%0A%20%24web.breakroleinheritance(%24false%2C%20%24false)%0A%20%24web.update()%0A%20%24context.ExecuteQuery()%3B%0A%0A%20%24AllSiteCollectionGroups%20%3D%20%24web.SiteGroups%0A%20%24context.Load(%24AllSiteCollectionGroups)%0A%20%20%20%20%24context.executeQuery()%0A%20%0A%20%24existingRootSiteOwnerGroup%20%3D%20%24AllSiteCollectionGroups.getByName(%24RootSiteOwnerGroup)%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Default%20Root%20site%20group%22%2C%20%24existingRootSiteOwnerGroup%20-ForegroundColor%20green%0A%0A%20Write-host%20%22%20%22%0A%20%24PermissionLevel%20%3D%20%24context.Web.RoleDefinitions.GetByName(%22Full%20Control%22)%0A%0A%20%23Bind%20Permission%20Level%20to%20Group%0A%20%24RoleDefBind%20%3D%20New-Object%20Microsoft.SharePoint.Client.RoleDefinitionBindingCollection(%24context)%0A%20%24RoleDefBind.Add(%24PermissionLevel)%0A%20%24Assignments%20%3D%20%24context.Web.RoleAssignments%0A%20%24RoleAssignOneNote%20%3D%20%24Assignments.Add(%24existingRootSiteOwnerGroup%2C%24RoleDefBind)%0A%20%24context.Load(%24existingRootSiteOwnerGroup)%0A%20%23send%20the%20request%20containing%20all%20operations%20to%20the%20server%0A%20try%7B%0A%20%20%24context.executeQuery()%0A%20%20Write-host%20%22%20%22%0A%20%20write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%20Success%20info%3A%20Added%20Root%20site%20group%20with%20Full%20Control%22%20-foregroundcolor%20green%0A%20%7D%0A%20catch%7B%0A%20%20Write-host%20%22%20%22%0A%20%20write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%20Error%20info%3A%20%24(%24_.Exception.Message)%22%20-foregroundcolor%20red%0A%20%7D%0A%0A%20%0A%20%23Create%20new%20groups%0A%20%24MysiteGroups%20%3D%20%24SubSiteGroupOwners%2C%20%24SubSiteGroupMembers%2C%20%24SubSiteGroupVisitors%0A%20foreach%20(%24siteGroup%20in%20%24MysiteGroups)%7B%0A%20%20if%20(%24siteGroup%20-like%20%22*Web%20Visitors%22)%0A%20%20%7B%0A%20%20%20Write-host%20%22%20%22%0A%20%20%20Write-host%20%22%20%20%20Creation%20of%20the%20Visitors%20group%22%2C%20%24SubSiteGroupVisitors%20-ForegroundColor%20green%0A%20%20%20%24gci%20%3D%20New-Object%20Microsoft.SharePoint.Client.GroupCreationInformation%0A%20%20%20%24gci.Title%20%3D%20%24siteGroup%0A%20%20%20%24gci.Description%20%3D%20%24siteGroup%0A%20%20%20%24siteGroup%20%3D%20%24context.Web.SiteGroups.Add(%24gci)%0A%0A%20%20%20%24PermissionLevel%20%3D%20%24context.Web.RoleDefinitions.GetByName(%22Read%22)%0A%0A%20%20%20%23Bind%20Permission%20Level%20to%20Group%0A%20%20%20%24RoleDefBind%20%3D%20New-Object%20Microsoft.SharePoint.Client.RoleDefinitionBindingCollection(%24context)%0A%20%20%20%24RoleDefBind.Add(%24PermissionLevel)%0A%20%20%20%24Assignments%20%3D%20%24context.Web.RoleAssignments%0A%20%20%20%24RoleAssignOneNote%20%3D%20%24Assignments.Add(%24siteGroup%2C%24RoleDefBind)%0A%20%20%20%24context.Load(%24siteGroup)%0A%20%20%20%24context.Web.Update()%0A%20%20%20%23send%20the%20request%20containing%20all%20operations%20to%20the%20server%0A%20%20%20try%7B%0A%20%20%20%20%24context.executeQuery()%0A%20%20%20%20write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%20Success%20info%3A%20Added%20visitors%20group%22%20-foregroundcolor%20green%0A%20%20%20%7D%0A%20%20%20catch%7B%0A%20%20%20%20write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%20Error%20info%3A%20%24(%24_.Exception.Message)%22%20-foregroundcolor%20red%0A%20%20%20%7D%0A%20%20%7D%0A%20%20%20%0A%20%20if%20(%24siteGroup%20-like%20%22*Web%20Members%22)%0A%20%20%7B%0A%20%20%20Write-host%20%22%20%22%0A%20%20%20Write-host%20%22%20%20%20Creation%20of%20the%20Members%20group%22%2C%20%24SubSiteGroupMembers%20-ForegroundColor%20green%0A%20%20%20%24gci%20%3D%20New-Object%20Microsoft.SharePoint.Client.GroupCreationInformation%0A%20%20%20%24gci.Title%20%3D%20%24siteGroup%0A%20%20%20%24gci.Description%20%3D%20%24siteGroup%0A%20%20%20%24siteGroup%20%3D%20%24context.Web.SiteGroups.Add(%24gci)%0A%0A%20%20%20%24PermissionLevel%20%3D%20%24context.Web.RoleDefinitions.GetByName(%22Edit%22)%0A%20%20%20%20%0A%20%20%20%23Bind%20Permission%20Level%20to%20Group%0A%20%20%20%24RoleDefBind%20%3D%20New-Object%20Microsoft.SharePoint.Client.RoleDefinitionBindingCollection(%24context)%0A%20%20%20%24RoleDefBind.Add(%24PermissionLevel)%0A%20%20%20%24Assignments%20%3D%20%24context.Web.RoleAssignments%0A%20%20%20%24RoleAssignOneNote%20%3D%20%24Assignments.Add(%24siteGroup%2C%24RoleDefBind)%0A%20%20%20%24context.Load(%24siteGroup)%0A%20%20%20%24context.Web.Update()%0A%20%20%20%23send%20the%20request%20containing%20all%20operations%20to%20the%20server%0A%20%20%20try%7B%0A%20%20%20%20%24context.executeQuery()%0A%20%20%20%20write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%20Success%20info%3A%20Added%20members%20group%22%20-foregroundcolor%20green%0A%20%20%20%7D%0A%20%20%20catch%7B%0A%20%20%20%20write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%20Error%20info%3A%20%24(%24_.Exception.Message)%22%20-foregroundcolor%20red%0A%20%20%20%7D%0A%20%20%7D%0A%20%20%20%0A%20%20if%20(%24siteGroup%20-like%20%22*Web%20Owners%22)%0A%20%20%7B%0A%20%20%20Write-host%20%22%20%22%0A%20%20%20Write-host%20%22%20%20%20Creation%20of%20the%20Owners%20group%22%2C%20%24SubSiteGroupOwners%20-ForegroundColor%20green%0A%20%20%20%24gci%20%3D%20New-Object%20Microsoft.SharePoint.Client.GroupCreationInformation%0A%20%20%20%24gci.Title%20%3D%20%24siteGroup%0A%20%20%20%24gci.Description%20%3D%20%24siteGroup%0A%20%20%20%24siteGroup%20%3D%20%24context.Web.SiteGroups.Add(%24gci)%0A%0A%20%20%20%24PermissionLevel%20%3D%20%24context.Web.RoleDefinitions.GetByName(%22Full%20Control%22)%0A%20%20%20%20%0A%20%20%20%23Bind%20Permission%20Level%20to%20Group%0A%20%20%20%24RoleDefBind%20%3D%20New-Object%20Microsoft.SharePoint.Client.RoleDefinitionBindingCollection(%24context)%0A%20%20%20%24RoleDefBind.Add(%24PermissionLevel)%0A%20%20%20%24Assignments%20%3D%20%24context.Web.RoleAssignments%0A%20%20%20%24RoleAssignOneNote%20%3D%20%24Assignments.Add(%24siteGroup%2C%24RoleDefBind)%0A%20%20%20%24context.Load(%24siteGroup)%0A%20%20%20%24context.Web.Update()%0A%20%20%20%23send%20the%20request%20containing%20all%20operations%20to%20the%20server%0A%20%20%20try%7B%0A%20%20%20%20%24context.executeQuery()%0A%20%20%20%20write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%20Success%20info%3A%20Added%20owners%20group%22%20-foregroundcolor%20green%0A%20%20%20%7D%0A%20%20%20catch%7B%0A%20%20%20%20write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%20Error%20info%3A%20%24(%24_.Exception.Message)%22%20-foregroundcolor%20red%0A%20%20%20%7D%0A%20%20%7D%0A%20%7D%0A%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Get%20the%20Site%20groups%22%20-ForegroundColor%20green%0A%0A%20%24context.Load(%24AllSiteCollectionGroups)%0A%20%20%20%20%24context.executeQuery()%0A%0A%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Update%20the%20Groups%20owners%20to%3A%22%2C%20%20%24existingRootSiteOwnerGroup.Id%20-ForegroundColor%20green%0A%20%24MyOwnersGroup%20%3D%20%24AllSiteCollectionGroups.GetByName(%24SubSiteGroupOwners)%0A%20%24MyMembersGroup%20%3D%20%24AllSiteCollectionGroups.GetByName(%24SubSiteGroupMembers)%0A%20%24MyVisitorsGroup%20%3D%20%24AllSiteCollectionGroups.GetByName(%24SubSiteGroupVisitors)%0A%20%0A%20%24MyOwnersGroup.Owner%20%3D%20%24existingRootSiteOwnerGroup%0A%20%24MyOwnersGroup.Update()%0A%20%24MyMembersGroup.Owner%20%3D%20%24existingRootSiteOwnerGroup%0A%20%24MyMembersGroup.Update()%0A%20%24MyVisitorsGroup.Owner%20%3D%20%24existingRootSiteOwnerGroup%0A%20%24MyVisitorsGroup.Update()%0A%20%24context.executeQuery()%0A%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20%26gt%3B%26gt%3B%26gt%3B%26gt%3B%26gt%3B%20in%20Case%20of%20error%2C%20you%20have%20to%20go%20on%20the%20default%20group%20page%3A%20%24(%24SubSiteFullUrl)%2F_layouts%2F15%2Fpermsetup.aspx%20%22%20-ForegroundColor%20magenta%20%20%23%20%3A%20%2F_layouts%2F15%2Fpermsetup.aspx%20%0A%20%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Define%20the%20Default%20SPWeb%20groups%22%20-ForegroundColor%20green%0A%0A%20%24context.Load(%24web)%0A%20%24context.executeQuery()%0A%0A%20Write-host%20%22%20%20Check%20the%20values%20!!!%22%0A%20Write-host%20%22%20%20%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20AssociatedVisitorGroup%3A%22%2C%20%24web.AssociatedVisitorGroup.Id%20-ForegroundColor%20Magenta%0A%20Write-host%20%22%20%20%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20AssociatedMemberGroup%3A%22%2C%20%24web.AssociatedMemberGroup.Id%20-ForegroundColor%20Magenta%0A%20Write-host%20%22%20%20%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20AssociatedMemberGroup%3A%22%2C%20%24web.AssociatedOwnerGroup.Id%20-ForegroundColor%20Magenta%0A%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Define%20the%20Visitors%20group%3A%22%2C%20%24MyVisitorsGroup.Id%20-ForegroundColor%20green%0A%20%24web.AssociatedVisitorGroup%20%3D%20%24MyVisitorsGroup%0A%20%24web.AssociatedVisitorGroup.Update()%0A%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Define%20the%20Members%20group%3A%22%2C%20%24MyMembersGroup.Id%20-ForegroundColor%20green%0A%20%24web.AssociatedMemberGroup%20%3D%20%24MyMembersGroup%0A%20%24web.AssociatedMemberGroup.Update()%0A%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Define%20the%20Owners%20group%3A%22%2C%20%24MyOwnersGroup.Id%20-ForegroundColor%20green%0A%20%24web.AssociatedOwnerGroup%20%3D%20%24MyOwnersGroup%0A%20%24web.AssociatedOwnerGroup.Update()%0A%0A%20%24web.Update()%0A%20%24context.executeQuery()%0A%0A%0A%20%23Create%20the%20Doc%20Lib%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Create%20Doc%20Lib%3A%22%2C%20%24DocLibTitle%20-ForegroundColor%20green%0A%20Create-SPWebList%20%20%24DocLibTitle%20%24DocLibTitleLong%20101%20%24context%20%0A%0A%20%24MyList%20%3D%20%24context.Web.Lists.GetByTitle(%24DocLibTitle)%0A%20%24context.Load(%24MyList)%0A%20%24context.executeQuery()%20%0A%20%24MyList.Title%20%3D%20%24DocLibTitleLong%0A%20%24MyList.OnQuickLaunch%20%3D%20%24True%0A%20%24MyList.Update()%0A%20%24context.executeQuery()%20%0A%20%0A%20%23Rename%20Shared%20Document%20library%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Modification%20of%20the%20Shared%20Document%20lib%22%20-ForegroundColor%20green%0A%20%24MyList%20%3D%20%24context.Web.Lists.GetByTitle(%22Documents%22)%0A%20%24context.Load(%24MyList)%0A%20%24context.executeQuery()%20%0A%20%24MyList.Title%20%3D%20%22Other%20Documents%22%0A%20%24MyList.OnQuickLaunch%20%3D%20%24True%0A%20%24MyList.Update()%0A%20%24context.executeQuery()%20%0A%20%0A%20%23Create%20Contact%20List%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Creation%20of%20Contacts%20list%22%20-ForegroundColor%20green%0A%20Create-SPWebList%20%20%22SubsiteContacts%22%20%22Subsite%20Contacts%22%20105%20%24context%20%0A%0A%20%24MyList%20%3D%20%24context.Web.Lists.GetByTitle(%22SubsiteContacts%22)%0A%20%24context.Load(%24MyList)%0A%20%24context.executeQuery()%20%0A%20%24MyList.Title%20%3D%20%22Subsite%20Contacts%22%0A%20%24MyList.EnableAttachments%20%3D%20%24false%0A%20%24MyList.EnableFolderCreation%20%3D%20%24false%0A%20%24MyList.OnQuickLaunch%20%3D%20%24True%0A%20%24MyList.Update()%0A%20%24context.executeQuery()%20%0A%20%0A%20%23Create%20Links%20List%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Creation%20of%20Links%20list%22%20-ForegroundColor%20green%0A%0A%20Create-SPWebList%20%20%22SubsiteLinks%22%20%22Subsite%20Links%22%20103%20%24context%20%0A%20%24MyList%20%3D%20%24context.Web.Lists.GetByTitle(%22SubsiteLinks%22)%0A%20%24context.Load(%24MyList)%0A%20%24context.executeQuery()%20%0A%20%24MyList.Title%20%3D%20%22Subsite%20Links%22%0A%20%24MyList.EnableAttachments%20%3D%20%24false%0A%20%24MyList.EnableFolderCreation%20%3D%20%24false%0A%20%24MyList.OnQuickLaunch%20%3D%20%24false%0A%20%24MyList.Update()%0A%20%24context.executeQuery()%20%0A%20%0A%20%23Create%20Calendar%20List%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Creation%20of%20Calendar%20list%22%20-ForegroundColor%20green%0A%0A%20Create-SPWebList%20%20%22SubsiteCalendar%22%20%22Subsite%20Calendar%22%20106%20%24context%20%0A%20%24MyList%20%3D%20%24context.Web.Lists.GetByTitle(%22SubsiteCalendar%22)%0A%20%24context.Load(%24MyList)%0A%20%24context.executeQuery()%20%0A%20%24MyList.Title%20%3D%20%22Subsite%20Calendar%22%0A%20%24MyList.EnableAttachments%20%3D%20%24false%0A%20%24MyList.EnableFolderCreation%20%3D%20%24false%0A%20%24MyList.OnQuickLaunch%20%3D%20%24True%0A%20%24MyList.Update()%0A%20%24context.executeQuery()%20%0A%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Start%20the%20Navigation%20Cleanup%22%20-ForegroundColor%20green%0A%20%0A%20%24QuickLaunchNodes%20%3D%20%24web.Navigation.QuickLaunch%0A%20%24context.Load(%24QuickLaunchNodes)%0A%20%20%20%20%23send%20the%20request%20containing%20all%20operations%20to%20the%20server%0A%20%20%20%20%24context.executeQuery()%0A%20for(%24i%3D%24QuickLaunchNodes.Count-1%3B%24i%20-ge%200%3B%20%24i--)%0A%20%7B%0A%20%20write-host%20%22%20%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20MenuItem%20%24(%24QuickLaunchNodes%5B%24i%5D.Title)%20-%20ID%3A%20%24(%24QuickLaunchNodes%5B%24i%5D.Id)%22%20%20-foregroundcolor%20green%0A%20%20%23write-host%20%22%20%20%20%20%20%20%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20MenuItem%20Children%20%24(%24QuickLaunchNodes%5B%24i%5D.Children)%20%22%20%20-foregroundcolor%20green%0A%0A%20%20switch%20(%24QuickLaunchNodes%5B%24i%5D.Title)%0A%20%20%7B%0A%20%20%20%22Home%22%20%7B%0A%20%20%20%20%20%24QuickLaunchNodes%5B%24i%5D.DeleteObject()%0A%20%20%20%20%20%24context.executeQuery()%0A%20%20%20%20%20write-host%20%22%20%20%20%20%20%20%20%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20MenuItem%20%24(%24QuickLaunchNodes%5B%24i%5D.Title)%20Link%20Found%20!!!!%22%20-foregroundcolor%20red%0A%20%20%20%7D%0A%20%20%20%22Recent%22%20%7B%0A%20%20%20%20%20%24QuickLaunchNodes%5B%24i%5D.DeleteObject()%0A%20%20%20%20%20%24context.executeQuery()%0A%20%20%20%20%20write-host%20%22%20%20%20%20%20%20%20%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20MenuItem%20%24(%24QuickLaunchNodes%5B%24i%5D.Title)%20Link%20Found%20!!!!%22%20-foregroundcolor%20red%0A%20%20%20%7D%0A%20%20%20%22Pages%22%20%7B%0A%20%20%20%20%20%24QuickLaunchNodes%5B%24i%5D.DeleteObject()%0A%20%20%20%20%20%24context.executeQuery()%0A%20%20%20%20%20write-host%20%22%20%20%20%20%20%20%20%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20MenuItem%20%24(%24QuickLaunchNodes%5B%24i%5D.Title)%20Link%20Found%20!!!!%22%20-foregroundcolor%20red%0A%20%20%20%7D%0A%20%20%20%22Site%20contents%22%20%7B%0A%20%20%20%20%20%24QuickLaunchNodes%5B%24i%5D.DeleteObject()%0A%20%20%20%20%20%24context.executeQuery()%0A%20%20%20%20%20write-host%20%22%20%20%20%20%20%20%20%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20MenuItem%20%24(%24QuickLaunchNodes%5B%24i%5D.Title)%20Link%20Found%20!!!!%22%20-foregroundcolor%20red%0A%20%20%20%7D%0A%20%20%7D%0A%20%7D%0A%0A%20Write-host%20%22%20%20%20Add%20the%20new%20Home%20Link%20into%20the%20Navigation%20Menu%22%2C%20%24SubSiteFullUrl%20-ForegroundColor%20green%0A%0A%20%24NavigationNode%20%3D%20New-Object%20Microsoft.SharePoint.Client.NavigationNodeCreationInformation%0A%20%24NavigationNode.Title%20%3D%20%22Subsite%20Home%22%0A%20%24NavigationNode.Url%20%3D%20%24SubSiteFullUrl%0A%20%23%24NavigationNode.AsLastNode%20%3D%20%24true%20%20%20%20%20%20%20%20%20%20%0A%20%24context.Load(%24QuickLaunchNodes.Add(%24NavigationNode))%0A%20%24context.executeQuery()%20%0A%0A%20Write-host%20%22%20%20%20Add%20the%20Link%20into%20the%20Subsite%20Navigation%20Menu%22%2C%20%24SiteIntegrationUrl%20-ForegroundColor%20green%0A%20%24QuickLaunchNodes%20%3D%20%24Myctx.web.Navigation.QuickLaunch%0A%20%24Myctx.Load(%24QuickLaunchNodes)%0A%20%24Myctx.executeQuery()%20%0A%0A%20%24NavigationNode%20%3D%20New-Object%20Microsoft.SharePoint.Client.NavigationNodeCreationInformation%0A%20%24NavigationNode.Title%20%3D%20%24SubSiteName%0A%20%24NavigationNode.Url%20%3D%20%24SubSiteFullUrl%0A%20%24NavigationNode.AsLastNode%20%3D%20%24true%20%20%20%20%20%20%20%20%20%20%0A%20%24Myctx.Load(%24QuickLaunchNodes.Add(%24NavigationNode))%0A%20%24Myctx.executeQuery()%20%0A%20%0A%7D%0Aelse%0A%7B%0A%20Write-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0A%20Write-host%20%22%20%3D%3D%3D%3D%26gt%3B%26gt%3B%20THE%20SUBSITE%20IS%20YET%20CREATED%3A%22%2C%20%24SubSiteFullUrl%20-ForegroundColor%20red%0A%0A%7D%0A%0A%0AWrite-host%20%22%20%22%0AWrite-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0A%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3E---------------------------------------------------------%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23ff6600%22%3E%3CSTRONG%3EPnP%20PowerShell%20script%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20sources%20used%20for%20this%20second%20script%20are%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%3C%2FA%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fveronicageek.com%2Foffice-365%2Fsharepoint-online%2Fcreate-libraries-folders-upload-documents-using-powershell-pnp%2F2017%2F12%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fveronicageek.com%2Foffice-365%2Fsharepoint-online%2Fcreate-libraries-folders-upload-documents-using-powershell-pnp%2F2017%2F12%2F%3C%2FA%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fthomasdaly.net%2F2017%2F03%2F06%2Fcreating-a-site-template-web-using-pnp-powershell-provisioning%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fthomasdaly.net%2F2017%2F03%2F06%2Fcreating-a-site-template-web-using-pnp-powershell-provisioning%2F%3C%2FA%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fnew-pnpweb%3Fview%3Dsharepoint-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fnew-pnpweb%3Fview%3Dsharepoint-ps%3C%2FA%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22http%3A%2F%2Fwww.sharepointdiary.com%2F2016%2F10%2Fsharepoint-online-check-if-site-collection-subsite-exists-powershell-csom.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.sharepointdiary.com%2F2016%2F10%2Fsharepoint-online-check-if-site-collection-subsite-exists-powershell-csom.html%3C%2FA%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F239489%2Fsharepoint-pnp-powershell-how-to-provision-sub-webs-with-unique-permissions-an%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F239489%2Fsharepoint-pnp-powershell-how-to-provision-sub-webs-with-unique-permissions-an%3C%2FA%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.c-sharpcorner.com%2Fblogs%2Fallow-access-requests-email-settings-in-sharepoint-site-using-pnp-powershell%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.c-sharpcorner.com%2Fblogs%2Fallow-access-requests-email-settings-in-sharepoint-site-using-pnp-powershell%3C%2FA%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EFeel%20free%20to%20adapt%20and%20use%20it%20as%20you%20need.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3Eif%20(-not%20(Get-Module%20-ListAvailable%20-Name%20SharePointPnPPowerShellOnline))%20%0A%7B%0A%20%20%20%20Install-Module%20SharePointPnPPowerShellOnline%0A%7D%0AImport-Module%20SharePointPnPPowerShellOnline%0A%0A%0A%23%20VALUE%20TO%20ADAPT%20!!!!!%0A%23%20--------------------------------------------------------------------------------------------%0A%24SiteIntegrationUrl%20%3D%22https%3A%2F%2Ftenant.sharepoint.com%2Fsites%2FSiteCollection%2FSubsite1%22%0A%24SubSiteURL%20%3D%20%22testsubsite%22%0A%24SubSiteName%20%3D%22_TESTSUBSITE%22%0A%24EmailAddressAccessRequest%20%3D%20%22emailaddresstosupport%40domain.com%22%0A%24RootSiteOwnerGroup%20%3D%20%22Root%20Site%20Collection%20Owners%22%0A%23%20--------------------------------------------------------------------------------------------%0A%0A%0A%0A%23Custom%20Function%20to%20Check%20if%20Site%20Collection%20Exists%20in%20Given%20URL%0AFunction%20Check-SiteExists(%24MySiteURL%2C%20%24MyCredentials)%0A%7B%0A%20%20%20%20Try%20%7B%0A%20%20%20Connect-PnPOnline%20-Url%20%24MySiteURL%20-Credentials%20%24MyCredentials%0A%20%20%20Return%20%24True%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20Catch%20%5BException%5D%20%7B%0A%20%20%20%20%20%20Write-host%20%22%20%26gt%3B%26gt%3B%26gt%3B%26gt%3B%20ERROR%20MESSAGE%3A%22%2C%20%24_.Exception.Message%20-f%20Red%0A%20%20%20%20%20%20Return%20%24False%0A%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%0A%7D%0A%0A%23%20---------------------------------------------------------------------------------------------------------------%0A%0A%0A%23get%20and%20save%20your%20O365%20credentials%0A%5Bstring%5D%24username%20%3D%20%22AdminSiteCollection%40tenant.onmicrosoft.com%22%0A%5Bstring%5D%24PwdTXTPath%20%3D%20%22C%3A%5CSECUREDPWD%5CExportedPWD-%24(%24username).txt%22%0A%24secureStringPwd%20%3D%20ConvertTo-SecureString%20-string%20(Get-Content%20%24PwdTXTPath)%0A%24cred%20%3D%20New-Object%20System.Management.Automation.PSCredential%20-ArgumentList%20%24username%2C%20%24secureStringPwd%0A%0A%24DocLibTitleLong%20%3D%20%22Given%20Documents%22%0A%0A%24DocLibTitle%20%3D%20-join(%24SubSiteURL%2C%20%22GivenDocuments%22)%0A%24DocLibTitleLong%20%3D%20-join(%24SubSiteName%2C%20%22%20Given%20Documents%22)%0A%24SubSiteTitle%20%3D%20-join(%24SubSiteName%2C%20%22%20-%20Subsite%20Usage%22)%0A%0A%24SubSiteOwnerOwners%20%3D%20-join(%24SubSiteName%2C%20%22%20Subsite%20Usage%20Web%20Owners%22)%0A%24SubSiteOwnerMembers%20%3D%20-join(%24SubSiteName%2C%20%22%20Subsite%20Usage%20Web%20Members%22)%0A%24SubSiteOwnerVisitors%20%3D%20-join(%24SubSiteName%2C%20%22%20Subsite%20Usage%20Web%20Visitors%22)%0A%0A%24siteUrl%20%3D%20-join(%24SiteIntegrationUrl%2C%20%22%2F%22%2C%20%24SubSiteURL)%0A%0A%23%20---------------------------------------------------------------------------------------------------------------%0A%23connect%20to%20the%20web%20site%20using%20the%20stored%20credentials%0AWrite-host%20%22%20%22%0AWrite-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0AWrite-host%20%22%20----%20CONNECT%20THE%20SITE%20---%20%22%20-ForegroundColor%20green%0AWrite-host%20%22%20%20%20CONNECTED%20SITE%3A%22%2C%20%24SiteIntegrationUrl%20%20-ForegroundColor%20Yellow%0AConnect-PnPOnline%20-Url%20%24SiteIntegrationUrl%20-Credential%20%24cred%0A%0AWrite-host%20%22%20%22%0AWrite-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0AWrite-host%20%22%20----%20START%20THE%20SUBSITE%20CREATION%20%20%3A%22%2C%20%24siteUrl%20-ForegroundColor%20green%0AWrite-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0A%0A%0A%24SiteExists%20%3D%20Check-SiteExists%20%24siteUrl%20%24cred%0Aif(%24SiteExists%20-ne%20%24true)%0A%7B%0A%20Write-host%20%22%20%20%20NEW%20SITE%20URL%20TO%20CREATE%3A%22%2C%20%24siteUrl%20%20-ForegroundColor%20Yellow%0A%20%23Create%20the%20subWeb%20for%20the%20Integration%20site%0A%20New-PnPWeb%20-Template%20%22STS%230%22%20-Title%20%24SubSiteTitle%20-Description%20%24SubSiteTitle%20-Url%20%24SubSiteURL%20-Locale%20%221033%22%20-InheritNavigation%20-BreakInheritance%0A%20Add-PnPNavigationNode%20-Title%20%24SubSiteTitle%20-Url%20%24siteUrl%20-Location%20%22QuickLaunch%22%0A%0A%20%23connect%20to%20the%20new%20web%20site%20using%20the%20stored%20credentials%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0A%20Write-host%20%22%20----%20CONNECT%20THE%20SITE%20---%20%22%20-ForegroundColor%20green%0A%20Write-host%20%22%20%20%20CONNECTED%20SITE%3A%22%2C%20%24siteUrl%20%20-ForegroundColor%20green%0A%20Connect-PnPOnline%20-Url%20%24siteUrl%20-Credentials%20%24cred%0A%20%24MyCurrentWeb%20%3D%20Get-PnPWeb%0A%0A%20%23configure%20the%20Access%20request%20system%0A%20Set-PnPRequestAccessEmails%20-Emails%20%24EmailAddressAccessRequest%0A%20%0A%20%23Create%20the%20Doc%20Lib%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Create%20Doc%20Lib%3A%22%2C%20%24DocLibTitle%20-ForegroundColor%20green%0A%20New-PnPList%20-Title%20%24DocLibTitle%20-Template%20DocumentLibrary%20-OnQuickLaunch%0A%20Set-PnPList%20-Identity%20%24DocLibTitle%20-Title%20%24DocLibTitleLong%20-EnableVersioning%20%24true%20-MajorVersions%20100%20-EnableMinorVersions%20%24false%0A%0A%20%23Rename%20Shared%20Document%20library%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Modification%20of%20the%20Shared%20Document%20lib%22%20-ForegroundColor%20green%0A%20Set-PnPList%20-Identity%20%22Documents%22%20-Title%20%22Other%20Documents%22%20-EnableVersioning%20%24true%20-MajorVersions%20100%20-EnableMinorVersions%20%24false%0A%0A%20%23Create%20Contact%20List%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Creation%20of%20Contacts%20list%22%20-ForegroundColor%20green%0A%20New-PnPList%20-Title%20%22SubsiteContacts%22%20-Template%20Contacts%20-OnQuickLaunch%20%0A%20Set-PnPList%20-Identity%20%22SubsiteContacts%22%20-Title%20%22Subsite%20Contacts%22%0A%0A%20%23Create%20Links%20List%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Creation%20of%20Links%20list%22%20-ForegroundColor%20green%0A%20New-PnPList%20-Title%20%22SubsiteLinks%22%20-Template%20Links%20%0A%20Set-PnPList%20-Identity%20%22SubsiteLinks%22%20-Title%20%22Subsite%20Links%22%0A%0A%20%23Create%20Calendar%20List%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Creation%20of%20Calendar%20list%22%20-ForegroundColor%20green%0A%20New-PnPList%20-Title%20%22SubsiteCalendar%22%20-Template%20%22Events%22%20-OnQuickLaunch%0A%20Set-PnPList%20-Identity%20%22SubsiteCalendar%22%20-Title%20%22Subsite%20Calendar%22%0A%0A%20%23Change%20the%20Navifation%20menu%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Modification%20of%20the%20Navigation%20Menu%22%20-ForegroundColor%20green%0A%20Remove-PnPNavigationNode%20-Title%20%22Pages%22%20-Location%20QuickLaunch%20-Force%20%0A%20Remove-PnPNavigationNode%20-Title%20%22Recent%22%20-Location%20QuickLaunch%20-Force%20%0A%20Remove-PnPNavigationNode%20-Title%20%22Site%20contents%22%20-Location%20QuickLaunch%20-Force%20%0A%20Remove-PnPNavigationNode%20-Title%20%22Home%22%20-Location%20QuickLaunch%20-Force%20%0A%20Add-PnPNavigationNode%20-Title%20%22Subsite%20Home%22%20-Url%20%24siteUrl%20-Location%20%22QuickLaunch%22%20-First%0A%0A%20%23Configure%20the%20permission%20set%20by%20default%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Get%20the%20OwnerGroup%20from%20the%20root%20site%22%2C%20%24RootSiteOwnerGroup%20-ForegroundColor%20green%0A%20%24owner%20%3D%20Get-PnPGroup%20-Identity%20%24RootSiteOwnerGroup%20%20%23(Get-PnPContext).Credentials.UserName%0A%20Write-host%20%22%20%20%20%20%26gt%3B%26gt%3B%26gt%3B%20Group%20ID%3A%22%2C%20%24owner.ID%20-ForegroundColor%20green%0A%20Set-PnPWebPermission%20-Identity%20%24MyCurrentWeb.Id%20-Group%20%24RootSiteOwnerGroup%20-AddRole%20%22Full%20Control%22%20%20%0A%20%0A%20%23Create%20default%20groups%20for%20the%20new%20web%0A%20%23here%2C%20by%20default%20owner%20will%20be%20the%20person%20provisioning%20the%20groups%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Creation%20of%20the%20Owners%20group%22%2C%20%24SubSiteOwnerOwners%20-ForegroundColor%20green%0A%20%24ownerGroup%20%3D%20New-PnPGroup%20-Title%20%24SubSiteOwnerOwners%20-Owner%20%24RootSiteOwnerGroup%0A%20Set-PnPGroup%20-Identity%20%24SubSiteOwnerOwners%20-SetAssociatedGroup%20Owners%20-AddRole%20%22Full%20Control%22%20%20%0A%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Creation%20of%20the%20Members%20group%22%2C%20%24SubSiteOwnerOwners%20-ForegroundColor%20green%0A%20%24memberGroup%20%3D%20New-PnPGroup%20-Title%20%24SubSiteOwnerMembers%20-Owner%20%24RootSiteOwnerGroup%0A%20Set-PnPGroup%20-Identity%20%24SubSiteOwnerMembers%20-SetAssociatedGroup%20Members%20-AddRole%20%22Edit%22%0A%0A%20Write-host%20%22%20%22%0A%20Write-host%20%22%20%20%20Creation%20of%20the%20Visitors%20group%22%2C%20%24SubSiteOwnerOwners%20-ForegroundColor%20green%0A%20%24visitorGroup%20%3D%20New-PnPGroup%20-Title%20%24SubSiteOwnerVisitors%20-Owner%20%24RootSiteOwnerGroup%0A%20Set-PnPGroup%20-Identity%20%24SubSiteOwnerVisitors%20-SetAssociatedGroup%20Visitors%20-AddRole%20%22Read%22%0A%0A%7D%0Aelse%0A%7B%0A%20Write-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0A%20Write-host%20%22%20%3D%3D%3D%3D%26gt%3B%26gt%3B%20THE%20SUBSITE%20IS%20YET%20CREATED%3A%22%2C%20%24siteUrl%20-ForegroundColor%20red%0A%0A%7D%0A%0AWrite-host%20%22%20%22%0AWrite-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0AWrite-host%20%22%20----%20DISCONNECT%20THE%20SITE%20---%20%22%20-ForegroundColor%20green%0A%23%20Quit%20the%20script%20and%20disconnect%20from%20the%20site%0ADisconnect-PnPOnline%0A%0AWrite-host%20%22%20%22%0AWrite-host%20%22%20--------------------------------------------------------------------------------------------%20%22%20-ForegroundColor%20green%0A%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3E---------------------------------------------------------%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAs%20you%20can%20see%20the%20PnP%20is%20smaller%20than%20the%20CSOM%2C%20because%20that%20solution%20is%20based%20on%20interface%20solution.%20Internally%20PnP%20is%20using%20the%20CSOM%20to%20work%20with%20Office%20365%2C%20but%20you%20have%20to%20install%20it%20and%20update%20it%20too.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EFabrice%20Romelard%20%5BMVP%5D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-203244%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdmin%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESites%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-268777%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%3A%20Create%20subsite%20using%20PowerShell%20script%20based%20on%20CSOM%20or%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-268777%22%20slang%3D%22en-US%22%3E%3CP%3ENo%20more%20authorization%20error%20while%20setting%20the%20default%20group%20!!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20the%20tip.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20will%20put%20the%20value%20back%20for%20now%20as%20I%20really%20can't%20image%20the%20side%20effect%20of%20this.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-268663%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%3A%20Create%20subsite%20using%20PowerShell%20script%20based%20on%20CSOM%20or%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-268663%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20Jean-Marie%2C%3C%2FP%3E%0A%3CP%3EThe%20part%20of%20the%20code%20%22%3CSPAN%3ESetAssociatedGroup%22%20is%20always%20the%20painful%20one.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIt's%20mainly%20related%20to%20the%20modification%20Microsoft%20is%20doing%20on%20the%20SharePoint%20side%20(with%20the%20modern%20group%20sites).%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThis%20option%20is%20blocking%20without%20any%20real%20reason%20and%20the%20only%20way%20I%20found%20to%20execute%20it%20is%20to%20unlock%20the%20site%20before%20the%20command%20execution%20with%20that%20command%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B-%26nbsp%3BSet-SPOSite%26nbsp%3B%5BRootSiteCollectionURL%5D%20-DenyAddAndCustomizePages%200%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI'm%20really%20not%20sure%20that%20will%20help%20you%20in%20that%20case%2C%20but%20in%20many%20one%20it%20was%20the%20solution%20to.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFab%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-268618%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%3A%20Create%20subsite%20using%20PowerShell%20script%20based%20on%20CSOM%20or%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-268618%22%20slang%3D%22en-US%22%3E%3CP%3EBonjour%20Fabrice%2C%3C%2FP%3E%3CP%3EI%26nbsp%3Bfollowed%20the%20pnp%20powershell%20path%20but%20I%20am%20block%20with%20an%20access%20denied%20exception.%3C%2FP%3E%3CP%3ESet-PnPGroup%20-Identity%20'Cours%20-%20Visiteurs'%20-SetAssociatedGroup%20Visitors%20-AddRole%20'Read'%3CBR%20%2F%3ESet-PnPGroup%20%3A%20Access%20denied.%20You%20do%20not%20have%20permission%20to%20perform%20this%20action%20or%20access%20this%20resource.%3CBR%20%2F%3EAu%20caract%C3%A8re%20Ligne%3A1%20%3A%209%3CBR%20%2F%3E%2B%20Set-PnPGroup%20-Identity%20'Cours%20-%20Visiteurs'%20-SetAssociatedGrou%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20WriteError%3A%20(%3A)%20%5BSet-PnPGroup%5D%2C%20ServerUnauthorizedAccessException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20EXCEPTION%2CSharePointPnP.PowerShell.Commands.Principals.SetGroup%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20group%20exist%20and%20I%20am%20connected%20(Connect-PnPOnline)%20as%20the%20tenant%20administrator%20who%20is%20also%20the%20owner%20of%20the%20sub%20site%2C%20the%20groups%2C%20etc.%20And%20I%20am%20using%20PnpPowerShellOnline%203.1.1809.0%20from%20the%20PSGallery.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20had%20%2F%20have%20the%20same%20problem%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnother%20point%2C%20my%20intent%20is%20to%20put%20the%20subsite%20provisionning%20in%20an%20Azure%20function.%20Is%20it%20better%20to%20use%20CSOM%20or%20PnP%20for%20stability%20reason.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EJean%20Marie%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-203256%22%20slang%3D%22en-US%22%3ERe%3A%20SharePoint%20Online%3A%20Create%20subsite%20using%20PowerShell%20script%20based%20on%20CSOM%20or%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-203256%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20French%20version%20of%20that%20article%3A%3C%2FP%3E%0A%3CP%3E-%26nbsp%3B%3CA%20href%3D%22http%3A%2F%2Fblogs.developpeur.org%2Ffabrice69%2Farchive%2F2018%2F06%2F12%2Foffice-365-script-powershell-de-cr-ation-de-sous-sites-bas-s-sur-csom-ou-pnp.aspx%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fblogs.developpeur.org%2Ffabrice69%2Farchive%2F2018%2F06%2F12%2Foffice-365-script-powershell-de-cr-ation-de-sous-sites-bas-s-sur-csom-ou-pnp.aspx%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFab%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Frequent Contributor

In Office 365, the SharePoint "Save as Site template" is not available anymore.

 

In many case, this strategy was used by power users to create a templated site they can save and use after for the new site respecting that format (cupcake notion).

In SharePoint Online, that option is not possible and Microsoft will not invite the users to create the subsites as much as possible, the solution concept is now flat and not anymore hierarchal.

SharePoint Online is only a branch of all the applications or tools given with an Office 365 Group.

 

But for many cases, we need to continue that usage of template (preparing the Cupcake) to industrialize some repeated activities (projects, analysis, assessment, …).

 

So now, the only way is to think about that industrialization with PowerShell Scripting.

 

Into that page, you will find 2 versions I wrote for my needs, but you can easily adapt it:

  • First script based on CSOM only
  • Second script based on PnP

---------------------------------------------------------

CSOM PowerShell script:

This one is based on many articles I found on Internet associated with my development, so the sources which help me:

 

Feel free to adapt and use it as you need.

# VALUE TO ADAPT !!!!!
# --------------------------------------------------------------------------------------------
$SiteIntegrationUrl ="https://tenant.sharepoint.com/sites/SiteCollection/Subsite1"
$SubSiteURL = "testsubsite"
$SubSiteName ="_TESTSUBSITE"
$EmailAddressAccessRequest = "emailaddresstosupport@domain.com"
$RootSiteOwnerGroup = "Root Site Collection Owners"
# --------------------------------------------------------------------------------------------


function Load-DLLandAssemblies
{
	[string]$defaultDLLPath = ""

	# Load assemblies to PowerShell session 

	$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
	[System.Reflection.Assembly]::LoadFile($defaultDLLPath)

	$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
	[System.Reflection.Assembly]::LoadFile($defaultDLLPath)

	$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
	[System.Reflection.Assembly]::LoadFile($defaultDLLPath)
}


#Custom Function to Check if Site Collection Exists in Given URL
Function Check-SiteExists($MySiteURL, $MyCredentials)
{
    #Setup context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($MySiteURL)
	$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($MyCredentials.UserName,$MyCredentials.Password)
    $Web = $Ctx.Web
    $Ctx.Load($web)
     
    Try {
            $Ctx.ExecuteQuery()
            Return $True
        }
    Catch [Exception] {
      Write-host " >>>> ERROR MESSAGE:", $_.Exception.Message -f Red
      Return $False
     }        
}


Function Create-SPWebList([string]$MyShortTitle, [string]$MyDescription, [int]$MyListTemplateType, [Microsoft.SharePoint.Client.ClientContext]$Mycontext)
{
	Write-Host " ---------------------------------------------------------"

	$lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
	$lci.Title = $MyShortTitle
	$lci.Description = $MyDescription
	$lci.TemplateType = $MyListTemplateType   #see: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx
	$list = $Mycontext.web.lists.add($lci)
	$Mycontext.load($list)
	#send the request containing all operations to the server
	try{
		$Mycontext.executeQuery()
		write-host "    >>> info: List Created $($MyShortTitle)" -foregroundcolor green
	}
	catch{
		write-host "  >>> List Creation Error info: $($_.Exception.Message)" -foregroundcolor red
	}

	Write-Host " ---------------------------------------------------------"
}


# ---------------------------------------------------------------------------------------------------------------


Load-DLLandAssemblies

#get and save your O365 credentials
[string]$username = "AdminSiteCollection@tenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\SECUREDPWD\ExportedPWD-$($username).txt"
$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd

$DocLibTitleLong = "Given Documents"

$DocLibTitle = -join($SubSiteURL, "GivenDocuments")
$DocLibTitleLong = -join($SubSiteName, " Given Documents")
$SubSiteTitle = -join($SubSiteName, " - Subsite Usage")

$SubSiteOwnerOwners = -join($SubSiteName, " Subsite Usage Web Owners")
$SubSiteOwnerMembers = -join($SubSiteName, " Subsite Usage Web Members")
$SubSiteOwnerVisitors = -join($SubSiteName, " Subsite Usage Web Visitors")

$SubSiteFullUrl = -join($SiteIntegrationUrl, "/", $SubSiteURL)

# ---------------------------------------------------------------------------------------------------------------
#connect to the web site using the stored credentials
Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
Write-host " ---- CONNECT THE SITE --- " -ForegroundColor green
Write-host "   CONNECTED SITE:", $SiteIntegrationUrl  -ForegroundColor Yellow

$Myctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteIntegrationUrl)

$Myctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.Password)
$Myctx.RequestTimeout = 1000000 # milliseconds
$MyspoRootweb = $Myctx.Web
$Myctx.Load($MyspoRootweb)
$Myctx.ExecuteQuery()

Write-Host " "
Write-Host " ---------------------------------------------------------"
Write-Host "  >>>> # Server Version:" $Myctx.ServerVersion " # <<<<<<" -ForegroundColor Green 
Write-Host " ---------------------------------------------------------"
Write-Host " "

Write-host " -------------------------------------------------------- "
Write-host "   -->> RootSite:", $MyspoRootweb.URL -ForegroundColor green

Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
Write-host " ---- START THE SUBSITE CREATION  --- " -ForegroundColor green
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green


$SiteExists = Check-SiteExists $SubSiteFullUrl $cred

if($SiteExists -ne $true)
{

	Write-host "   NEW SITE URL TO CREATE:", $SubSiteFullUrl  -ForegroundColor Yellow
	$Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
	$Subsite.WebTemplate = "STS#0"
	$Subsite.Title = $SubSiteTitle
	$Subsite.Url = $SubSiteURL
	$Subsite.Language = "1033"
	$Subsite.Description = $SubSiteTitle

	$SubWeb = $Myctx.Web.Webs.Add($Subsite)
	$Myctx.Load($SubWeb)
	Try{
		$Myctx.ExecuteQuery()
		Write-host " "
		Write-host "     >> Success info creating site for $SubSiteURL" -ForegroundColor green
	}
	Catch{
		Write-host "     >> Error info creating subsite for $SubSiteURL - $_.Exception.Message" -ForegroundColor red
	}

	$context = New-Object Microsoft.SharePoint.Client.ClientContext($SubSiteFullUrl)
	$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.Password)
	$web = $context.web
	$context.load($web)
	
	Write-host " "
	Write-host "   Implement the Top menu navigation" -ForegroundColor green
	$web.Navigation.UseShared = $true
	$context.ExecuteQuery();
	
	Write-host " "
	Write-host "   Implement the Access Request settings: ", $EmailAddressAccessRequest -ForegroundColor green
    $Web.RequestAccessEmail = $EmailAddressAccessRequest
	$web.breakroleinheritance($false, $false)
	$web.update()
	$context.ExecuteQuery();

	$AllSiteCollectionGroups = $web.SiteGroups
	$context.Load($AllSiteCollectionGroups)
    $context.executeQuery()
	
	$existingRootSiteOwnerGroup = $AllSiteCollectionGroups.getByName($RootSiteOwnerGroup)
	Write-host " "
	Write-host "   Default Root site group", $existingRootSiteOwnerGroup -ForegroundColor green

	Write-host " "
	$PermissionLevel = $context.Web.RoleDefinitions.GetByName("Full Control")

	#Bind Permission Level to Group
	$RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
	$RoleDefBind.Add($PermissionLevel)
	$Assignments = $context.Web.RoleAssignments
	$RoleAssignOneNote = $Assignments.Add($existingRootSiteOwnerGroup,$RoleDefBind)
	$context.Load($existingRootSiteOwnerGroup)
	#send the request containing all operations to the server
	try{
		$context.executeQuery()
		Write-host " "
		write-host "     >> Success info: Added Root site group with Full Control" -foregroundcolor green
	}
	catch{
		Write-host " "
		write-host "     >> Error info: $($_.Exception.Message)" -foregroundcolor red
	}

	
	#Create new groups
	$MysiteGroups = $SubSiteGroupOwners, $SubSiteGroupMembers, $SubSiteGroupVisitors
	foreach ($siteGroup in $MysiteGroups){
		if ($siteGroup -like "*Web Visitors")
		{
			Write-host " "
			Write-host "   Creation of the Visitors group", $SubSiteGroupVisitors -ForegroundColor green
			$gci = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
			$gci.Title = $siteGroup
			$gci.Description = $siteGroup
			$siteGroup = $context.Web.SiteGroups.Add($gci)

			$PermissionLevel = $context.Web.RoleDefinitions.GetByName("Read")

			#Bind Permission Level to Group
			$RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
			$RoleDefBind.Add($PermissionLevel)
			$Assignments = $context.Web.RoleAssignments
			$RoleAssignOneNote = $Assignments.Add($siteGroup,$RoleDefBind)
			$context.Load($siteGroup)
			$context.Web.Update()
			#send the request containing all operations to the server
			try{
				$context.executeQuery()
				write-host "     >> Success info: Added visitors group" -foregroundcolor green
			}
			catch{
				write-host "     >> Error info: $($_.Exception.Message)" -foregroundcolor red
			}
		}
		 
		if ($siteGroup -like "*Web Members")
		{
			Write-host " "
			Write-host "   Creation of the Members group", $SubSiteGroupMembers -ForegroundColor green
			$gci = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
			$gci.Title = $siteGroup
			$gci.Description = $siteGroup
			$siteGroup = $context.Web.SiteGroups.Add($gci)

			$PermissionLevel = $context.Web.RoleDefinitions.GetByName("Edit")
			 
			#Bind Permission Level to Group
			$RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
			$RoleDefBind.Add($PermissionLevel)
			$Assignments = $context.Web.RoleAssignments
			$RoleAssignOneNote = $Assignments.Add($siteGroup,$RoleDefBind)
			$context.Load($siteGroup)
			$context.Web.Update()
			#send the request containing all operations to the server
			try{
				$context.executeQuery()
				write-host "     >> Success info: Added members group" -foregroundcolor green
			}
			catch{
				write-host "     >> Error info: $($_.Exception.Message)" -foregroundcolor red
			}
		}
		 
		if ($siteGroup -like "*Web Owners")
		{
			Write-host " "
			Write-host "   Creation of the Owners group", $SubSiteGroupOwners -ForegroundColor green
			$gci = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
			$gci.Title = $siteGroup
			$gci.Description = $siteGroup
			$siteGroup = $context.Web.SiteGroups.Add($gci)

			$PermissionLevel = $context.Web.RoleDefinitions.GetByName("Full Control")
			 
			#Bind Permission Level to Group
			$RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
			$RoleDefBind.Add($PermissionLevel)
			$Assignments = $context.Web.RoleAssignments
			$RoleAssignOneNote = $Assignments.Add($siteGroup,$RoleDefBind)
			$context.Load($siteGroup)
			$context.Web.Update()
			#send the request containing all operations to the server
			try{
				$context.executeQuery()
				write-host "     >> Success info: Added owners group" -foregroundcolor green
			}
			catch{
				write-host "     >> Error info: $($_.Exception.Message)" -foregroundcolor red
			}
		}
	}

	Write-host " "
	Write-host "   Get the Site groups" -ForegroundColor green

	$context.Load($AllSiteCollectionGroups)
    $context.executeQuery()


	Write-host " "
	Write-host "   Update the Groups owners to:",  $existingRootSiteOwnerGroup.Id -ForegroundColor green
	$MyOwnersGroup = $AllSiteCollectionGroups.GetByName($SubSiteGroupOwners)
	$MyMembersGroup = $AllSiteCollectionGroups.GetByName($SubSiteGroupMembers)
	$MyVisitorsGroup = $AllSiteCollectionGroups.GetByName($SubSiteGroupVisitors)
	
	$MyOwnersGroup.Owner = $existingRootSiteOwnerGroup
	$MyOwnersGroup.Update()
	$MyMembersGroup.Owner = $existingRootSiteOwnerGroup
	$MyMembersGroup.Update()
	$MyVisitorsGroup.Owner = $existingRootSiteOwnerGroup
	$MyVisitorsGroup.Update()
	$context.executeQuery()

	Write-host " "
	Write-host "   >>>>> in Case of error, you have to go on the default group page: $($SubSiteFullUrl)/_layouts/15/permsetup.aspx	" -ForegroundColor magenta  # : /_layouts/15/permsetup.aspx	
	
	Write-host " "
	Write-host "   Define the Default SPWeb groups" -ForegroundColor green

	$context.Load($web)
	$context.executeQuery()

	Write-host "  Check the values !!!"
	Write-host "      >>> AssociatedVisitorGroup:", $web.AssociatedVisitorGroup.Id -ForegroundColor Magenta
	Write-host "      >>> AssociatedMemberGroup:", $web.AssociatedMemberGroup.Id -ForegroundColor Magenta
	Write-host "      >>> AssociatedMemberGroup:", $web.AssociatedOwnerGroup.Id -ForegroundColor Magenta

	Write-host " "
	Write-host "   Define the Visitors group:", $MyVisitorsGroup.Id -ForegroundColor green
	$web.AssociatedVisitorGroup = $MyVisitorsGroup
	$web.AssociatedVisitorGroup.Update()

	Write-host " "
	Write-host "   Define the Members group:", $MyMembersGroup.Id -ForegroundColor green
	$web.AssociatedMemberGroup = $MyMembersGroup
	$web.AssociatedMemberGroup.Update()

	Write-host " "
	Write-host "   Define the Owners group:", $MyOwnersGroup.Id -ForegroundColor green
	$web.AssociatedOwnerGroup = $MyOwnersGroup
	$web.AssociatedOwnerGroup.Update()

	$web.Update()
	$context.executeQuery()


	#Create the Doc Lib
	Write-host " "
	Write-host "   Create Doc Lib:", $DocLibTitle -ForegroundColor green
	Create-SPWebList  $DocLibTitle $DocLibTitleLong 101 $context 

	$MyList = $context.Web.Lists.GetByTitle($DocLibTitle)
	$context.Load($MyList)
	$context.executeQuery()	
	$MyList.Title = $DocLibTitleLong
	$MyList.OnQuickLaunch = $True
	$MyList.Update()
	$context.executeQuery()	
	
	#Rename Shared Document library
	Write-host " "
	Write-host "   Modification of the Shared Document lib" -ForegroundColor green
	$MyList = $context.Web.Lists.GetByTitle("Documents")
	$context.Load($MyList)
	$context.executeQuery()	
	$MyList.Title = "Other Documents"
	$MyList.OnQuickLaunch = $True
	$MyList.Update()
	$context.executeQuery()	
	
	#Create Contact List
	Write-host " "
	Write-host "   Creation of Contacts list" -ForegroundColor green
	Create-SPWebList  "SubsiteContacts" "Subsite Contacts" 105 $context 

	$MyList = $context.Web.Lists.GetByTitle("SubsiteContacts")
	$context.Load($MyList)
	$context.executeQuery()	
	$MyList.Title = "Subsite Contacts"
	$MyList.EnableAttachments = $false
	$MyList.EnableFolderCreation = $false
	$MyList.OnQuickLaunch = $True
	$MyList.Update()
	$context.executeQuery()	
	
	#Create Links List
	Write-host " "
	Write-host "   Creation of Links list" -ForegroundColor green

	Create-SPWebList  "SubsiteLinks" "Subsite Links" 103 $context 
	$MyList = $context.Web.Lists.GetByTitle("SubsiteLinks")
	$context.Load($MyList)
	$context.executeQuery()	
	$MyList.Title = "Subsite Links"
	$MyList.EnableAttachments = $false
	$MyList.EnableFolderCreation = $false
	$MyList.OnQuickLaunch = $false
	$MyList.Update()
	$context.executeQuery()	
	
	#Create Calendar List
	Write-host " "
	Write-host "   Creation of Calendar list" -ForegroundColor green

	Create-SPWebList  "SubsiteCalendar" "Subsite Calendar" 106 $context 
	$MyList = $context.Web.Lists.GetByTitle("SubsiteCalendar")
	$context.Load($MyList)
	$context.executeQuery()	
	$MyList.Title = "Subsite Calendar"
	$MyList.EnableAttachments = $false
	$MyList.EnableFolderCreation = $false
	$MyList.OnQuickLaunch = $True
	$MyList.Update()
	$context.executeQuery()	

	Write-host " "
	Write-host "   Start the Navigation Cleanup" -ForegroundColor green
	
	$QuickLaunchNodes = $web.Navigation.QuickLaunch
	$context.Load($QuickLaunchNodes)
    #send the request containing all operations to the server
    $context.executeQuery()
	for($i=$QuickLaunchNodes.Count-1;$i -ge 0; $i--)
	{
		write-host "     >>> MenuItem $($QuickLaunchNodes[$i].Title) - ID: $($QuickLaunchNodes[$i].Id)"  -foregroundcolor green
		#write-host "          >>> MenuItem Children $($QuickLaunchNodes[$i].Children) "  -foregroundcolor green

		switch ($QuickLaunchNodes[$i].Title)
		{
			"Home" {
					$QuickLaunchNodes[$i].DeleteObject()
					$context.executeQuery()
					write-host "           >>> MenuItem $($QuickLaunchNodes[$i].Title) Link Found !!!!" -foregroundcolor red
			}
			"Recent" {
					$QuickLaunchNodes[$i].DeleteObject()
					$context.executeQuery()
					write-host "           >>> MenuItem $($QuickLaunchNodes[$i].Title) Link Found !!!!" -foregroundcolor red
			}
			"Pages" {
					$QuickLaunchNodes[$i].DeleteObject()
					$context.executeQuery()
					write-host "           >>> MenuItem $($QuickLaunchNodes[$i].Title) Link Found !!!!" -foregroundcolor red
			}
			"Site contents" {
					$QuickLaunchNodes[$i].DeleteObject()
					$context.executeQuery()
					write-host "           >>> MenuItem $($QuickLaunchNodes[$i].Title) Link Found !!!!" -foregroundcolor red
			}
		}
	}

	Write-host "   Add the new Home Link into the Navigation Menu", $SubSiteFullUrl -ForegroundColor green

	$NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
	$NavigationNode.Title = "Subsite Home"
	$NavigationNode.Url = $SubSiteFullUrl
	#$NavigationNode.AsLastNode = $true          
	$context.Load($QuickLaunchNodes.Add($NavigationNode))
	$context.executeQuery()	

	Write-host "   Add the Link into the Subsite Navigation Menu", $SiteIntegrationUrl -ForegroundColor green
	$QuickLaunchNodes = $Myctx.web.Navigation.QuickLaunch
	$Myctx.Load($QuickLaunchNodes)
	$Myctx.executeQuery()	

	$NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
	$NavigationNode.Title = $SubSiteName
	$NavigationNode.Url = $SubSiteFullUrl
	$NavigationNode.AsLastNode = $true          
	$Myctx.Load($QuickLaunchNodes.Add($NavigationNode))
	$Myctx.executeQuery()	
	
}
else
{
	Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
	Write-host " ====>> THE SUBSITE IS YET CREATED:", $SubSiteFullUrl -ForegroundColor red

}


Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green

---------------------------------------------------------

PnP PowerShell script

The sources used for this second script are:

 

Feel free to adapt and use it as you need.

if (-not (Get-Module -ListAvailable -Name SharePointPnPPowerShellOnline)) 
{
    Install-Module SharePointPnPPowerShellOnline
}
Import-Module SharePointPnPPowerShellOnline


# VALUE TO ADAPT !!!!!
# --------------------------------------------------------------------------------------------
$SiteIntegrationUrl ="https://tenant.sharepoint.com/sites/SiteCollection/Subsite1"
$SubSiteURL = "testsubsite"
$SubSiteName ="_TESTSUBSITE"
$EmailAddressAccessRequest = "emailaddresstosupport@domain.com"
$RootSiteOwnerGroup = "Root Site Collection Owners"
# --------------------------------------------------------------------------------------------



#Custom Function to Check if Site Collection Exists in Given URL
Function Check-SiteExists($MySiteURL, $MyCredentials)
{
    Try {
			Connect-PnPOnline -Url $MySiteURL -Credentials $MyCredentials
			Return $True
        }
    Catch [Exception] {
      Write-host " >>>> ERROR MESSAGE:", $_.Exception.Message -f Red
      Return $False
     }        
}

# ---------------------------------------------------------------------------------------------------------------


#get and save your O365 credentials
[string]$username = "AdminSiteCollection@tenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\SECUREDPWD\ExportedPWD-$($username).txt"
$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd

$DocLibTitleLong = "Given Documents"

$DocLibTitle = -join($SubSiteURL, "GivenDocuments")
$DocLibTitleLong = -join($SubSiteName, " Given Documents")
$SubSiteTitle = -join($SubSiteName, " - Subsite Usage")

$SubSiteOwnerOwners = -join($SubSiteName, " Subsite Usage Web Owners")
$SubSiteOwnerMembers = -join($SubSiteName, " Subsite Usage Web Members")
$SubSiteOwnerVisitors = -join($SubSiteName, " Subsite Usage Web Visitors")

$siteUrl = -join($SiteIntegrationUrl, "/", $SubSiteURL)

# ---------------------------------------------------------------------------------------------------------------
#connect to the web site using the stored credentials
Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
Write-host " ---- CONNECT THE SITE --- " -ForegroundColor green
Write-host "   CONNECTED SITE:", $SiteIntegrationUrl  -ForegroundColor Yellow
Connect-PnPOnline -Url $SiteIntegrationUrl -Credential $cred

Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
Write-host " ---- START THE SUBSITE CREATION  :", $siteUrl -ForegroundColor green
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green


$SiteExists = Check-SiteExists $siteUrl $cred
if($SiteExists -ne $true)
{
	Write-host "   NEW SITE URL TO CREATE:", $siteUrl  -ForegroundColor Yellow
	#Create the subWeb for the Integration site
	New-PnPWeb -Template "STS#0" -Title $SubSiteTitle -Description $SubSiteTitle -Url $SubSiteURL -Locale "1033" -InheritNavigation -BreakInheritance
	Add-PnPNavigationNode -Title $SubSiteTitle -Url $siteUrl -Location "QuickLaunch"

	#connect to the new web site using the stored credentials
	Write-host " "
	Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
	Write-host " ---- CONNECT THE SITE --- " -ForegroundColor green
	Write-host "   CONNECTED SITE:", $siteUrl  -ForegroundColor green
	Connect-PnPOnline -Url $siteUrl -Credentials $cred
	$MyCurrentWeb = Get-PnPWeb

	#configure the Access request system
	Set-PnPRequestAccessEmails -Emails $EmailAddressAccessRequest
	
	#Create the Doc Lib
	Write-host " "
	Write-host "   Create Doc Lib:", $DocLibTitle -ForegroundColor green
	New-PnPList -Title $DocLibTitle -Template DocumentLibrary -OnQuickLaunch
	Set-PnPList -Identity $DocLibTitle -Title $DocLibTitleLong -EnableVersioning $true -MajorVersions 100 -EnableMinorVersions $false

	#Rename Shared Document library
	Write-host " "
	Write-host "   Modification of the Shared Document lib" -ForegroundColor green
	Set-PnPList -Identity "Documents" -Title "Other Documents" -EnableVersioning $true -MajorVersions 100 -EnableMinorVersions $false

	#Create Contact List
	Write-host " "
	Write-host "   Creation of Contacts list" -ForegroundColor green
	New-PnPList -Title "SubsiteContacts" -Template Contacts -OnQuickLaunch 
	Set-PnPList -Identity "SubsiteContacts" -Title "Subsite Contacts"

	#Create Links List
	Write-host " "
	Write-host "   Creation of Links list" -ForegroundColor green
	New-PnPList -Title "SubsiteLinks" -Template Links 
	Set-PnPList -Identity "SubsiteLinks" -Title "Subsite Links"

	#Create Calendar List
	Write-host " "
	Write-host "   Creation of Calendar list" -ForegroundColor green
	New-PnPList -Title "SubsiteCalendar" -Template "Events" -OnQuickLaunch
	Set-PnPList -Identity "SubsiteCalendar" -Title "Subsite Calendar"

	#Change the Navifation menu
	Write-host " "
	Write-host "   Modification of the Navigation Menu" -ForegroundColor green
	Remove-PnPNavigationNode -Title "Pages" -Location QuickLaunch -Force 
	Remove-PnPNavigationNode -Title "Recent" -Location QuickLaunch -Force 
	Remove-PnPNavigationNode -Title "Site contents" -Location QuickLaunch -Force 
	Remove-PnPNavigationNode -Title "Home" -Location QuickLaunch -Force 
	Add-PnPNavigationNode -Title "Subsite Home" -Url $siteUrl -Location "QuickLaunch" -First

	#Configure the permission set by default
	Write-host " "
	Write-host "   Get the OwnerGroup from the root site", $RootSiteOwnerGroup -ForegroundColor green
	$owner = Get-PnPGroup -Identity $RootSiteOwnerGroup  #(Get-PnPContext).Credentials.UserName
	Write-host "    >>> Group ID:", $owner.ID -ForegroundColor green
	Set-PnPWebPermission -Identity $MyCurrentWeb.Id -Group $RootSiteOwnerGroup -AddRole "Full Control"  
	
	#Create default groups for the new web
	#here, by default owner will be the person provisioning the groups
	Write-host " "
	Write-host "   Creation of the Owners group", $SubSiteOwnerOwners -ForegroundColor green
	$ownerGroup = New-PnPGroup -Title $SubSiteOwnerOwners -Owner $RootSiteOwnerGroup
	Set-PnPGroup -Identity $SubSiteOwnerOwners -SetAssociatedGroup Owners -AddRole "Full Control"  

	Write-host " "
	Write-host "   Creation of the Members group", $SubSiteOwnerOwners -ForegroundColor green
	$memberGroup = New-PnPGroup -Title $SubSiteOwnerMembers -Owner $RootSiteOwnerGroup
	Set-PnPGroup -Identity $SubSiteOwnerMembers -SetAssociatedGroup Members -AddRole "Edit"

	Write-host " "
	Write-host "   Creation of the Visitors group", $SubSiteOwnerOwners -ForegroundColor green
	$visitorGroup = New-PnPGroup -Title $SubSiteOwnerVisitors -Owner $RootSiteOwnerGroup
	Set-PnPGroup -Identity $SubSiteOwnerVisitors -SetAssociatedGroup Visitors -AddRole "Read"

}
else
{
	Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
	Write-host " ====>> THE SUBSITE IS YET CREATED:", $siteUrl -ForegroundColor red

}

Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
Write-host " ---- DISCONNECT THE SITE --- " -ForegroundColor green
# Quit the script and disconnect from the site
Disconnect-PnPOnline

Write-host " "
Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green

---------------------------------------------------------

 

As you can see the PnP is smaller than the CSOM, because that solution is based on interface solution. Internally PnP is using the CSOM to work with Office 365, but you have to install it and update it too.

 

Fabrice Romelard [MVP]

4 Replies

Bonjour Fabrice,

I followed the pnp powershell path but I am block with an access denied exception.

Set-PnPGroup -Identity 'Cours - Visiteurs' -SetAssociatedGroup Visitors -AddRole 'Read'
Set-PnPGroup : Access denied. You do not have permission to perform this action or access this resource.
Au caractère Ligne:1 : 9
+ Set-PnPGroup -Identity 'Cours - Visiteurs' -SetAssociatedGrou ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Set-PnPGroup], ServerUnauthorizedAccessException
+ FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Principals.SetGroup

 

The group exist and I am connected (Connect-PnPOnline) as the tenant administrator who is also the owner of the sub site, the groups, etc. And I am using PnpPowerShellOnline 3.1.1809.0 from the PSGallery.

 

Do you had / have the same problem ?

 

Another point, my intent is to put the subsite provisionning in an Azure function. Is it better to use CSOM or PnP for stability reason.

 

Jean Marie

Dear Jean-Marie,

The part of the code "SetAssociatedGroup" is always the painful one.

It's mainly related to the modification Microsoft is doing on the SharePoint side (with the modern group sites).

This option is blocking without any real reason and the only way I found to execute it is to unlock the site before the command execution with that command:

 - Set-SPOSite [RootSiteCollectionURL] -DenyAddAndCustomizePages 0

 

I'm really not sure that will help you in that case, but in many one it was the solution to.

 

Fab

 

No more authorization error while setting the default group !!

 

Thanks for the tip.

 

I will put the value back for now as I really can't image the side effect of this.

Related Conversations
Extentions Synchronization
Deleted in Discussions on
3 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies