0

I have several SharePoint online sites (hundreds) every about 4 of them are created with the same name/title but they differ in the url web address. Now I need to delete specific one of each 4 which I have the URL address in a spreadsheet, so is it possible to delete them with specifying the url in the Powershell script instead of the name/title? I hope it's possible because it will take forever to check each site individual and delete the correct one.

For example the sites look like this:

AAA - .../siteaaa1

AAA - .../siteaaa2

AAA - .../siteaaa3

AAA - .../siteaaa4

ABB - .../siteabb1

ABB - .../siteabb2

and so on..

Will be great help if someone know how.

Many thanks

2
  • are these subsites or site collections ? – Gautam Sheth Jul 1 '17 at 20:45
  • They are subsites – Blueman Jul 1 '17 at 21:42
0

To delete an individual subsite using urls, you can use the below code in powershell:

$SiteURL = "https://tenant.sharepoint.com/sites/site/siteAA1"

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$User = "admin@tenant.onmicrosoft.com"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

$Context.Credentials = $Creds

$web = $Context.Web  

$Context.Load($web)  
$Context.ExecuteQuery() 

$web.DeleteObject() 
$Context.ExecuteQuery() 
Write-Host $web.Title "Site Deleted"

Or if you know the urls, you can specify them in an array and then delete them via server relative url as below:

#enter the site collection url
$SiteURL = "https://tenant.sharepoint.com/sites/site/"


#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$User = "admin@tenant.onmicrosoft.com"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Context.Credentials = $Creds

#enter server relative path of the subsite in the array
#for first level subsite, it will be "siteaa1" - url would be ""https://tenant.sharepoint.com/sites/site/siteaa1"
#for second level subsite, it will be "subsite/siteaa1" - url would be ""https://tenant.sharepoint.com/sites/site/subsite/siteaa1"

$array = @("siteaaa1", "siteaaa2", "siteaaa3")
for ($i=0; $i -lt $array.length; $i++) 
{

    $web = $Context.Site.OpenWeb($array[$i]);

    $Context.Load($web)  
    $Context.ExecuteQuery() 

    $web.DeleteObject() 
    $Context.ExecuteQuery() 
    Write-Host $web.Title "Site Deleted"

}

Download & install the SharePoint client component SDK which will give you the CSOM dlls.

Also download the SP online management shell to run the above code.

SharePoint 2013 Client component SDK

SharePoint online management shell

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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