I need help with PowerShell script to disable search indexing on a list of sites and subsites.

Vincent Collogan 46 Reputation points
2024-10-17T17:33:42.5033333+00:00

I have cobbled together this script to disable search indexing of sites listed in the sites.txt file, but I'm having trouble figuring out how to apply the same change to all subsites under the sites.txt list. Any PowerShell SMEs who are familiar with contexts out there who can help?

#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"

#Sites list
$sites = Get-Content -Path "d:\temp\sites.txt"

Foreach ($site in $sites)
{
 
    Try {     
    #Setup the context  
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($site)  

    #Get the Web  
    $Web = $Ctx.Web  
    $Ctx.Load($Web)  
    $Ctx.ExecuteQuery()  

    If($Web.NoCrawl)  
    {  
        Write-host -f Yellow $site "already excluded from search index."  
    }  
    Else  
    {  
        #Exclude site from search  
        $Web.NoCrawl = $True  
        $Web.Update()  
        $Ctx.ExecuteQuery() 
        Write-host -f Green $site "excluded from search index successfully"  
    }  
    }  
    catch {  
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red  
    } 
}

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,622 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,640 questions
{count} votes

Accepted answer
  1. Yanli Jiang - MSFT 27,321 Reputation points Microsoft Vendor
    2024-10-18T06:01:06.1366667+00:00

    Hi @Vincent Collogan ,

    Welcome to Q&A forum!

    To disable search indexing on a list of sites and their subsites, you can modify your existing script to include a recursive function that processes each subsite. Here's an example of how you can achieve this:

    # 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 disable search indexing recursively
    function Disable-SearchIndexing {
        param (
            [Microsoft.SharePoint.Client.ClientContext]$Ctx,
            [Microsoft.SharePoint.Client.Web]$Web
        )
    
        # Load the current web
        $Ctx.Load($Web)
        $Ctx.ExecuteQuery()
    
        # Check if the web is already excluded from search
        if ($Web.NoCrawl) {
            Write-Host -ForegroundColor Yellow "$($Web.Url) already excluded from search index."
        } else {
            # Exclude site from search
            $Web.NoCrawl = $True
            $Web.Update()
            $Ctx.ExecuteQuery()
            Write-Host -ForegroundColor Green "$($Web.Url) excluded from search index successfully."
        }
    
        # Load subsites
        $Ctx.Load($Web.Webs)
        $Ctx.ExecuteQuery()
    
        # Iterate through each subsite
        foreach ($subWeb in $Web.Webs) {
            Disable-SearchIndexing -Ctx $Ctx -Web $subWeb
        }
    }
    
    # Sites list
    $sites = Get-Content -Path "d:\temp\sites.txt"
    Foreach ($site in $sites) {
        Try {
            # Setup the context  
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($site)  
            $Web = $Ctx.Web  
            Disable-SearchIndexing -Ctx $Ctx -Web $Web
        } catch {
            Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red  
        }
    }
    

    In this script, the Disable-SearchIndexing function is called for each site and its subsites, effectively applying the search indexing change recursively.

    Hope this can help.

    Good day!


    If the answer is helpful, please click "Accept as Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.