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.