SharePoint Online: Remove List from Search using PowerShell

By default, all SharePoint Online lists and libraries appear in search results. There are some cases you want to remove a particular list’s contents from appearing in search results. Say e.g. You want to hide images library’s content from appearing in search results.

You can configure a specific list to be excluded from search. To exclude a list or library from the search index using the user interface, go to :

  • List Settings >> Click on “Advanced Settings” 
  • Scroll down and under the Search section, select “Yes”  for “Allow items from this document library to appear in search results?” Option.
    sharepoint online remove list from search
  • Click OK to commit changes.

This setting allows or disallows SharePoint Online crawlers to index the list. Wait for 4 to 6 Hours and this list should be removed from search results.

How to Remove SharePoint Online List from Search using PowerShell?

Let’s use PowerShell to exclude the list or library from SharePoint Online search.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName = "Documents"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the list
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    
    #Exclude from Search Results
    $List.NoCrawl = $True
    $List.Update() 

    $Ctx.ExecuteQuery() 

    Write-host "List has been excluded from Search Index!" -f Green
}
Catch {
    write-host -f Red "Error Excluding List from Search Index!" $_.Exception.Message
} 

Let’s make the script as re-usable function. To make list searchable, simply set the “NoCrawl” parameter to “True”

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to Enable or Disable Search Crawl
Function Set-SPOListCrawl()
{
    Param ([string]$ListName,[bool]$NoCrawl)

    Try {
        #Get the list
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
    
        #Enable/Disable List from Search Results
        $List.NoCrawl = $NoCrawl
        $List.Update() 

        $Ctx.ExecuteQuery() 

        Write-host "List Search Crawl Settings has been Updated!" -f Green
    }
    Catch {
        write-host -f Red "Error Setting List Search Crawler Settings!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Call the Function to Exclude a list from Search Crawl
Set-SPOListCrawl -ListName "Documents" -NoCrawl $True

This removes the content of the list or library from search results. Here is my another article on excluding list or library from search index in SharePoint on-premises: How to Exclude SharePoint Site or List from Search Index?

SharePoint Online: Remove List from Search using PnP PowerShell

#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName = "Team Documents"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Context
$Context = Get-PnPContext
 
#Get the List
$List = Get-PnPList -Identity $ListName

If($List)
{
    #Set "NoCrawl" flag to exclude list from search
    $List.NoCrawl = $True 
    $List.Update() 
    $Context.ExecuteQuery()
    Write-host -f Green "List Excluded from Search Index Successfully. Changes takes effect after the next crawl!"
}
else
{
    Write-host -f Yellow "Could not find List '$ListName'"
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply