SharePoint (2003 thru Online): Add/Remove sites (bulk list) from Search Index thru PowerShell

Thursday, May 9, 2024

Add/Remove sites (bulk list) from Search Index thru PowerShell

 We need to remove 150 sites from Search Index. We have to make sure they don't show up in search results by turning off that option for each site. The normal way of doing this is too slow for so many sites. We use a faster PowerShell script method to handle 150 sites at once.

Regular method

By default, the option Allow this site to appear in Search results is set to Yes for all Site collections in Sharepoint online. This means the site content can show up in search results.

On the site, select Settings (Wheel) icon., and then select Site settings. If you don't see Site settings, select Site information, and then select View all site settings.

Under Search, click Search and offline availability.

In the Indexing Site Content section, under Allow this site to appear in Search results, select Yes to allow the content of the site to appear in search results.


If you don't want the content to show up in search results, choose No.

NOTE: Search results are always security trimmed, so users will only see content they have permission to see.


PowerShell Script Method

List all the sites in a CSV file with URL as Header (As shown below).


# symbol in PowerShell script is used for comments.

# Connect to Admin Center
$adminSiteUrl = "https://gurram-admin.sharepoint.com/"
$adminConnection = Connect-PnPOnline -Url $AdminSiteUrl -Interactive

$CSVImport = "D:\Sites.csv"
$SitesCollections = Import-CSV -Path $CSVImport
 
ForEach($Site in $SitesCollections)
   {
         $siteCollectionConnection = Connect-PnPOnline -Url $Site.URL -Interactive
         Set-PnPSite -Identity $Site.URL -DenyAndAddCustomizePages $false
         $Web = Get-PnPWeb -Connection $siteCollectionConnection
         #Indexing Site Content = Yes(false), = No(true)
         $Web.NoCrawl = $false
         $Web.Update()
         Invoke-PnPQuery
         #To unlock the site (-LockState Unlock), To lock the site (-LockState ReadOnly)
         #we did not use the below option, because we want to delete sites later
         Set-PnPSite -Identity $Site.URL -LockState ReadOnly
         #to know the completed site
         write-host $Site.URL
    }


This script completed successfully in less than 10 minutes.

No comments:

Post a Comment