Sreeram MUNIRAJA

Sreeram MUNIRAJA

  • NA
  • 87
  • 22.2k

Delete SharePoint Sub sites

Jun 5 2018 5:11 AM
Hi All,
I have power shell code to delete SharePoint Sub sites. But this code delete the site perminantly. But i want to go site into Recycle Bin once delete the site.
 
Can you please change below my scrript?
 
#Variables for Processingn
$SiteUrl = "https://tenant.sharepoint"
$UserName="test @gmail.com"
$Password ="********"
try{
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$context.Credentials = $credentials
#get the subsite
$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()
#Delete subsite
$web.DeleteObject()
$context.ExecuteQuery()
Write-Host "SubSite Deleted:" $web.Title -foregroundcolor Green
}
catch{
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
 
Best regards 

Answers (5)