0

I was looking for a easy way to delete hundreds of test site in a modern SharePoint online env. MS flow didn't to the trick, so I was looking into PowerShell.

Set-Location -Path C:\scripts


#Connect to SPO
$tenantName= "companyName"
$username = "[email protected]"
$password = "SuperSavePassword" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password

Connect-SPOService -Url https://$tenantName-admin.sharepoint.com/ -Credential $cred
Connect-MsolService -Credential $cred
Connect-AzureAD -Credential $cred
Connect-PnPOnline -Url https://$tenantName-admin.sharepoint.com/ -Credentials $cred 

Anytime I'm trying to Get-PnPUnifiedGroup I get an error saying s'thing like:

Get-PnPUnifiedGroup : Exception while invoking endpoint https://login.microsoftonline.com/2092f441-2e68-4313-9252-4794a2e05343/oauth2/token

Yet, my account is a global admin on the tenant.

EDIT:

I found another way, in which I don't have to use 'PNPunifiedGroup'

$sites= Import-Csv C:\Users\xxx\Downloads\DemoToDelete.csv
ForEach ($s in $sites)
{
$group = Get-MSolGroup -SearchString $s.Title
Remove-MsolGroup -ObjectId $group.ObjectId
Remove-SPOSite -Identity $s.SiteUrl -Confirm:$False
}

This deleted like 50% of the sites provided in .csv

2 Answers 2

0

You can delete the modern team site and associated groups as below using PnP PowerShell.

You first need to delete the SharePoint site collection and then remove the O365 group using the Graph endpoints by connecting to it via scopes

$tenantName= "companyName"
$username = "[email protected]"
$password = "SuperSavePassword" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password

Connect-PnPOnline "https://tenant-admin.sharepoint.com" -Credentials $cred
Remove-PnPTenantSite https://tenant.sharepoint.com/sites/ModernTeamSite
Disconnect-PnPOnline

Connect-PnPOnline "https://tenant-admin.sharepoint.com" -Scopes "Group.ReadWrite.All", "Directory.ReadWrite.All" -Credentials $cred
$unifiedGroup = Get-PnPUnifiedGroup -Identity "Group Name"
Remove-PnPUnifiedGroup -Identity $unifiedGroup.ID 
Disconnect-PnPOnline

Reference - Delete an Office365 group and SharePoint site collection

5
  • Thanks for your fast reply. Unfortunately in SharePoint Online, first you have to delete the O365group and than the SPOsite. But I can't seem to get it working. I updated my code in the original post.
    – Emiel
    Commented Jun 19, 2019 at 14:02
  • Did you try the code I posted ? In the get pnpunifiedgroup method, you need to pass display name of the group Commented Jun 19, 2019 at 14:04
  • Yeah, but not quite what I'm looking for. Remove-PnPTenantSite doesn't work since the site is connected to a O365group. And I'm deleting like 100 sites, so I need to use a foreachloop, for which I only have the siteUrl...
    – Emiel
    Commented Jun 19, 2019 at 14:57
  • @Emiel, If you are a global admin, use AzureAD PowerShell. The might be helpful:morgantechspace.com/2019/02/… Commented Jun 19, 2019 at 20:16
  • @Emiel also curious why not just use the New Admin Centre where Groups are visiting and just select the test one and delete? Commented Jun 19, 2019 at 20:17
0

And MS fixed it :-) SP-admin center now features "Bulk Edit" on active sites.

2
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review Commented Aug 2, 2019 at 9:07
  • I think it kind of provide an answer... Since the ultimate script isn't necessary anymore and you simple can delete multiple SPO-sites through GUI..
    – Emiel
    Commented Aug 2, 2019 at 13:32

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.