Provisioning a Site template to several SharePoint Group Sites with PowerShell
-
24-01-2021 - |
Question
I am trying to search for site collections which are of the type "Office 365 Group site collections (GROUP#0)" and then want to apply the Provisioning Template to all the group sites returned in the search. How can I do this in PowerShell using looping/iteration? I am new to PowerShell so can someone please help on this?
Solution
You first need to connect to your SharePoint Online tenant using the Connect-PnPOnline
. After that, you need to fetch all the modern group/team sites and then you need to connect to each one of then and apply your template.
You can follow the below script. Ensure that you have SharePoint Admin rights else it wont work and you will get access denied or other assorted errors.
$cred = Get-Credential
Connect-PnPOnline "https://your-tenant.sharepoint.com" -Credentials $cred
$ModernGroupSites = Get-PnPTenantSite -Template 'GROUP#0'
foreach ($site in $ModernGroupSites)
{
Connect-PnPOnline -Url $site.Url -Credentials $cred
#ensure that you are passing the correct path
Apply-PnPProvisioningTemplate -Path template.xml
}
OTHER TIPS
I believe you are filtering Office 365 Group sites and you are going to provision the site template on this.
$GroupSite = Get-SPOSite -Template Group#0
ForEach ($sites in $GroupSite)
{
# Condition
}
You can use Powershell commands as below to get all the Groups in Tenant if you are a tenant administrator role.
$Groups = Get-UnifiedGroup | Sort-Object WhenCreated
then you can loop through the Groups to get all the site collection related to the Group using below code
ForEach ($G in $Groups) {
Connect-PnPOnline -Url $G.SharePointSiteUrl -Credentials $SPCredentials
#Apply your Provisioning template on this URL
}
Edit: You need to get cmdlets using below commands, full code as below
$UserName="abc@def.onmicrosoft.com"
$Password = ConvertTo-SecureString "YourPassword@01" -AsPlainText -Force
$SPCredentials = New-Object -typename System.Management.Automation.PSCredential -argumentlist $UserName,$Password
#connect to Exchange Online and Download cmdlets
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $SPCredentials -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
$Groups = Get-UnifiedGroup | Sort-Object WhenCreated
ForEach ($G in $Groups) {
Connect-PnPOnline -Url $G.SharePointSiteUrl -Credentials $SPCredentials
#Apply your Provisioning template on this URL
}