Changing the Search scope in SharePoint

When a customer with tons of sites needs to change the search scope, there are several ways to approach this.

One solution is to create a PnP PowerShell script. In this example we’ll change the search scope to tenant (Organization).

Steps to success:

Export list of sites as a CSV-file
  1. Create a service account with SharePoint Administrator privileges.
  2. Download a list of all the sites on your tenant from the SharePoint admin panel as a CSV-file (Export). If you need to exclude sites, this is the time to do it – remove the sites you don’t want to touch from the downloaded list.
  3. If there are several hundred sites, split your CSV file and do the next step in chunks.
  4. Edit the fields CSVPath, ServiceAccount and TenantName in the script below.
  5. Run the script and login from the service account when prompted.
#Set Parameters
$CSVPath = "./AllSites.csv"
$ServiceAccount = ""
$TenantName = "tenantname"

#Fetch sites from the CSV file
$SiteList = Import-CSV $CSVPath

$i = 0;
$TotalRecords = $SiteList.Count

Connect-PnPOnline -Url https://$ -Interactive
#Iterate through each row from the CSV-file and deploy in script.
try {
ForEach ($Record in $SiteList)
Write-Progress -activity "Adding Site '$($Record.'Url')'" -status "$i out of $TotalRecords completed"

Set-PnPTenantSite -Identity $Record.'URL' -Owners "$ServiceAccount"
Connect-PnPOnline -Url $Record.'URL' -Interactive

#change default search scope
Set-PnPSearchSettings -SearchScope Tenant -SearchBoxPlaceholderText "Søk her..."
Remove-PnPSiteCollectionAdmin -Owners "$ServiceAccount"
catch {
	Write-Host "There was an error, see details below:"
	Write-Host $_	//Print the potential errors

Drink some coffee and enjoy your new search scope.



, ,




Leave a Reply

Your email address will not be published. Required fields are marked *