Reindex SharePoint sites
Keeping your SharePoint environment up-to-date is crucial, especially after making schema changes - this script streamlines the process of reindexing sites, libraries, or lists.
Summary
When using SharePoint columns in search solutions, you may need to map crawled properties to managed properties and update the search schema. For example, you can map the crawled property ows_Foo to RefinableString100 and use it as a filter in KQL. For more information, refer to How Do Site Columns Become Managed Properties.
If your schema change applies to existing content, you must schedule a re-index operation for the affected content in order to have the data populated into the search index.
This PnP PowerShell script streamlines the process of re-indexing sites, libraries, or lists to ensure that search results remain accurate and relevant.
#Prompt for caution
Read-Host -Prompt "Are you sure you know what you are doing, and have read the text at https://pnp.github.io/script-samples/spo-reindex-sites/README.html?"
#Set Parameters
$AdminCenterURL = "https://contoso-admin.sharepoint.com"
# Connect to SharePoint admin center
Connect-PnPOnline -Url $AdminCenterURL -Interactive
#Get SharePoint sites
$m365Sites = Get-PnPTenantSite -Detailed | Where-Object {($_.Url -like '*/teams-*' -or $_.Template -eq 'TEAMCHANNEL#1') -and $_.Template -ne 'RedirectSite#0' } #filter to exclude redirect sites and to include team channel sites in the list
$m365Sites | ForEach-Object {
# Connect to SharePoint site
Connect-PnPOnline -Url $_.Url -Interactive
#Request Reindex
Request-PnPReIndexWeb
Write-host "Reindexing: " $_.Url
}
# Disconnect SharePoint online connection
Disconnect-PnPOnline#Prompt for caution
Read-Host -Prompt "Are you sure you know what you are doing, and have read the text at https://pnp.github.io/script-samples/spo-reindex-sites/README.html?"
#Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
m365 login
}
#Get SharePoint sites
$m365Sites = m365 spo site list | ConvertFrom-Json | Where-Object {$_.Url -like '*/sites/*' -and $_.Template -ne 'RedirectSite#0' } #filter to include sites with "/sites/" managed path and to exclude the redirect sites
$m365Sites | ForEach-Object {
Write-host "Requesting reindexing for: " $_.Url
#Request Re-indexing of SharePoint site
m365 spo web reindex --url $_.Url
}
#Disconnect SharePoint online connection
m365 logoutContributors
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.
Tool documentation & references
- Want to learn more about PnP PowerShell and the cmdlets
Check out the PnP PowerShell site to get started and for the reference to the cmdlets.
- Want to learn more about CLI for Microsoft 365 and the commands
Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.