1

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?

3 Answers 3

1

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
}
2
  • Thanks. This worked. But now I'm getting an error for 'A duplicate field name " " was found'. Do you know how I can skip the field name in the loop if it already exists?
    – Tyra
    Sep 28, 2018 at 14:17
  • it could be something in the template, check the fields schema. Not sure you can debug it in powershell Oct 1, 2018 at 6:42
0

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
}

Ref: https://medium.com/@jcgm1978/office-365-how-to-get-the-list-of-modern-team-sites-using-get-sposite-ec445a589de5

0

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="[email protected]"

    $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

        }
2
  • I get an error: Get-UnifiedGroup : The term 'Get-UnifiedGroup' is not recognized as the name of a cmdlet, function, script file, or operable program.
    – Tyra
    Sep 25, 2018 at 13:01
  • I have updated the Answer with cmdlets update Sep 25, 2018 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.