How to Read Text File Bottom Up To Delete Subsites in SharePoint

Introduction

Consider a scenario in SharePoint where you want to delete all the subsites under a specific site collection. This can be done manually, but consider a case where you have multiple subsites within subsites under a site collection. In this case you can use a PowerShell script to do it. This article explains how to read the text file from the bottom up to do it.

Planning

Before executing you need to plan the approach. In this section I explain the approach for deleting subsites and what the use is of reading the text file from the bottom up. The following is the plan we will execute:

  • Take the site collection backup. If the site collection is huge then take a content database backup
  • Export the subsite details to a text file
  • Develop a PowerShell script to read the text file and delete the subsites

Implementation

Use the following procedure to do it.

Step 1

Take a site collection backup using the backup-spsite command. If the site collection is huge then take a content database backup.

It is always important to take a backup before deleting anything in SharePoint. If a problem occurs then you can roll back the changes using the backup file.

Step 2

Export the subsite details to a text file and call it “SubSites.txt”. This article focuses on exporting the subsite details to a text file. Our main focus will be on reading the text file from the bottom up and what the purpose is of that in this context.

You can export the subsite details with the following small code:

  1. $siteCollectionURL = read-host “Enter the site collection URL “  
  2. $path = read-host “Enter the path where you want to save the output file “  
  3. $sites = get-spsite $siteCollectionURL  
  4. Foreach($web in $sites.allwebs)  
  5. {  
  6.    $web.url | out-file $path\SubSites.txt  

Step 3

Now you have a list of all the sites under the site collection.

Note: This list will have the root level web. You need to remove the unwanted subsites and the root level web from the list and keep only the subsites that you need to delete.

Now you can develop a script to parse using the “SubSites.txt” file and delete each one of them, but one catch here is the last level subsite must be deleted before deleting the parent site, for example consider the following scenario.

You will delete the following sites:

http://sitecollection/subsite1
http://sitecollection/subsite1/abc
http://sitecollection/subsite1/xyz


“subsite1” can be deleted if and only if “subsite1/abc” and “subsite1/xyz” are deleted. The exported file in Step 1 will have the sites in this format, the parent site first and followed by the child sites.

So in this case when you develop a script to remove the subsites you need to read/parse using the exported text file from the bottom up (child site to parent site).

The following is the piece of code that helps you to read the text file from the bottom up.

  1. $InputFile = $scriptbase + "\" + "SubSites.txt"  
  2. $TotalLines = (get-content $InputFile | measure-object)  
  3. $path = get-content $InputFile  
  4. write-host "Reading text file bottom up" -fore yellow  
  5. for($i=0 ; $i -le $TotalLines.count; $i++)  
  6. {  
  7.    write-host $path[$TotalLines.count - $i]  

Complete Code

  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\ReadTextFileBottomUpPatch-$LogTime.rtf"  
  3. # Add SharePoint PowerShell Snapin  
  4. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {  
  5.    Add-PSSnapin Microsoft.SharePoint.Powershell  
  6. }  
  7. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  8. Set-Location $scriptBase  
  9. #Deleting any .rtf files in the scriptbase location  
  10. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  11. if($FindRTFFile)  
  12. {  
  13.    foreach($file in $FindRTFFile)  
  14.    {  
  15.       remove-item $file  
  16.    }  
  17. }  
  18. start-transcript $logfile  
  19. $InputFile = $scriptbase + "\" + "SubSites.txt"  
  20. $TotalLines = (get-content $InputFile | measure-object)  
  21. $path = get-content $InputFile  
  22. write-host "Reading text file bottom up" -fore yellow  
  23. for($i=0 ; $i -le $TotalLines.count; $i++)  
  24.   
  25. {  
  26.    write-host $path[$TotalLines.count - $i]  
  27. }  
  28. stop-transcript 

Execution procedure

Step 1: Take a backup of the site collection using backup-spsite. Take a content database backup if the site collection is larger.
Step 2: Export the subsite details to the “SubSites.txt” file.
Step 3: Develop a script to remove the subsites. The catch here is to read the text file from the bottom up. I am focusing only on the area to read the “Subsites.txt” file bottom up.
Step 4: Launch the SharePoint management shell.
Step 5: Consider your “SubSites.txt” file is as in the following:



Execute the PowerShell script



Conclusion


Thus in this article I have explained how to read the text file from the bottom up.

Up Next
    Ebook Download
    View all
    Learn
    View all