Enable/Disable Search Crawling on Sites and Libraries
Summary
This sample allows you to enable or disable the Search crawling on a site or a library. You can use this to control the search indexing of a site or a library. But if you disable it, web parts and search experiences will not work against the locations you disable. However, this can be used to remove locations from Microsoft 365 Copilot.
Warning
Please be aware this script contains a command that will remove content from search, ensure you test and understand the implications of running the script. If this is an active site, it can negatively impact the user experience.
# Connect to SharePoint Online site
Connect-PnPOnline https://contoso.sharepoint.com/sites/SearchEnableDisableTest -Interactive
# Note: No Search Crawl is false because it is crawling the site by default - love these MS negative logic
$status = Get-PnPWeb -Includes NoCrawl
Write-Host "Current WEB status - Crawling: $(!$status.NoCrawl)"
# Disable No Site Scripting
# If you don't do this, you may get an access denied error
Set-PnPSite -NoScriptSite $false
# Disable Search Crawl
#----------------------------
Set-PnPWeb -NoCrawl
$status = Get-PnPWeb -Includes NoCrawl
Write-Host "Current WEB status - Crawling: $(!$status.NoCrawl)"
# Enable Search Crawl
#----------------------------
Set-PnPWeb -NoCrawl:$false
$status = Get-PnPWeb -Includes NoCrawl
Write-Host "Current WEB status - Crawling: $(!$status.NoCrawl)"
# Enable No Site Scripting
Set-PnPSite -NoScriptSite $true
#-----------------------------------------------------------
# Library Level
#-----------------------------------------------------------
# Remember - No Search Crawl is false because it is crawling the site by default
$listName = "Documents"
# Get the current status of the list
$list = Get-PnPList -Identity $listName -Includes NoCrawl
Write-Host "Current DOCUMENT LIBRARY ($($listName)) status - Crawling: $(!$list.NoCrawl)"
# Disable Search Crawl on list
#-------------------------------
Set-PnPList -Identity $listName -NoCrawl
$list = Get-PnPList -Identity $listName -Includes NoCrawl
Write-Host "Current DOCUMENT LIBRARY ($($listName)) status - Crawling: $(!$list.NoCrawl)"
# Enable Search Crawl on list
#-------------------------------
Set-PnPList -Identity $listName -NoCrawl:$false
$list = Get-PnPList -Identity $listName -Includes NoCrawl
Write-Host "Current DOCUMENT LIBRARY ($($listName)) status - Crawling: $(!$list.NoCrawl)"
# Disconnect SharePoint online connection
Disconnect-PnPOnline
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
Contributors
Author(s) |
---|
Paul Bullock |
Ganesh Sanap |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.