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:
- $siteCollectionURL = read-host “Enter the site collection URL “
- $path = read-host “Enter the path where you want to save the output file “
- $sites = get-spsite $siteCollectionURL
- Foreach($web in $sites.allwebs)
- {
- $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.
- $InputFile = $scriptbase + "\" + "SubSites.txt"
- $TotalLines = (get-content $InputFile | measure-object)
- $path = get-content $InputFile
- write-host "Reading text file bottom up" -fore yellow
- for($i=0 ; $i -le $TotalLines.count; $i++)
- {
- write-host $path[$TotalLines.count - $i]
- }
Complete Code
- $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
- $LogFile = ".\ReadTextFileBottomUpPatch-$LogTime.rtf"
- # Add SharePoint PowerShell Snapin
- if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
- Add-PSSnapin Microsoft.SharePoint.Powershell
- }
- $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
- Set-Location $scriptBase
- #Deleting any .rtf files in the scriptbase location
- $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
- if($FindRTFFile)
- {
- foreach($file in $FindRTFFile)
- {
- remove-item $file
- }
- }
- start-transcript $logfile
- $InputFile = $scriptbase + "\" + "SubSites.txt"
- $TotalLines = (get-content $InputFile | measure-object)
- $path = get-content $InputFile
- write-host "Reading text file bottom up" -fore yellow
- for($i=0 ; $i -le $TotalLines.count; $i++)
-
- {
- write-host $path[$TotalLines.count - $i]
- }
- 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.