Get Deleted Sites , Create New Site , Delete Site , Restore Site from Deleted in SharePoint Using PowerShell

PowerShell For SharePoint Online SharePoint Online

In today’s article we will see how we can retrieve information about the sites we have deleted using PowerShell .
Then we will see how we can delete a site again using PowerShell and how we can restore a site again using PowerShell.
We open SharePoint Online Management Shell with administrator privileges and run the following code to detect the deleted sites
Note here that for the discovery we should use Get-SPODeletedSite but to avoid returning an error we should use Connect-SPOService for connecting to SharePoint Online and not Connect-PnPOnline -Url $Site -UseWebLogin

To identify the deleted sites I run the following code

#Initialisation of Parameters
$Site = "https://mytenant-admin.sharepoint.com"

#Connect to SharePoint Online
Connect-SPOService -Url $Site
 
#Find Deleted Sites Using PowerShell
Get-SPODeletedSite -IncludePersonalSite

After executing the code you can see that I have obtained information about the sites listed as well as some other details such as the date of deletion of the site and the days remaining until the final deletion of the site from the system

Now it remains to delete one of the available sites using PowerShell
We actually run the following code and we have

The process of deletion is not easy .First we have to install the exchange module in SharePoint Shell and then connect to the exchange . Then we have to proceed to delete the Microsoft 365 Group that belongs to the specific site we wish to delete and then we have to connect to sharepoint online with Connect-SPOService and execute the Remove-SPOSite deletion command

#Initialisation of Parameters
$Site = "https://mytenant-admin.sharepoint.com"
$SiteForDeletion = "https://mytenant.sharepoint.com/sites/DemoForDeletion"

#Connect to Exchange Online
Connect-ExchangeOnline

#Delete Group 365 Using PowerShell
Remove-UnifiedGroup -Identity "Demo For Deletion"

#Connect to SharePoint Online
Connect-SPOService -Url $Site
 
#Connect to SharePoint Online
Remove-SPOSite -Identity $SiteForDeletion -noWait

In the next stage we want to restore our deleted site back to its original state
To complete the process we need to execute the following PowerShell

#Restore Deleted Site
Restore-SPODeletedSite -Identity $SiteForDeletion

Related articles can be found in the following Microsoft Docs links

https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/?view=sharepoint-ps&WT.mc_id=M365-MVP-5004210

https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/get-spodeletedsite?view=sharepoint-ps&WT.mc_id=M365-MVP-5004210

https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/restore-spodeletedsite?view=sharepoint-ps&WT.mc_id=M365-MVP-5004210

https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/remove-sposite?view=sharepoint-ps&WT.mc_id=M365-MVP-5004210