Tags

, ,

Creating search scope for a site in PowerShell is basically a two steps operation. First step consists in creating a search scope inside a search application, while second step consists in creating a search rule and making this rule to search inside a site. There is nothing complicated in this, as you will see in description below.

First thing you need to do is to make PowerShell prompt for the folder (URL) where site is located.

Write-Host "Enter Site URL:"
$url = Read-Host

Once I created a variable $url with the value assigned dynamically by the user, I can proceed to create the search scope.

$searchapp = Get-SPEnterpriseSearchServiceApplication
$src = Get-SPEnterpriseSearchQueryScope  -Identity "Search scope name" -SearchApplication $searchapp
if($src -ne $null) {
	Remove-SPEnterpriseSearchQueryScope -Identity  "Search scope name"  -SearchApplication $searchapp
}
$src = New-SPEnterpriseSearchQueryScope -Name "Search scope name" -SearchApplication $searchapp -Description "Search scope name" -DisplayInAdminUI $true

Scope name of course is going to be changed to fit your needs. Or you can assign the scope name dynamically by applying the same technique as I applied for site URL.

The last step is to add search rule for making new search scope pointing to site URL.

$rule = New-SPEnterpriseSearchQueryScopeRule -Scope $src -RuleType "Url" -Url $url -FilterBehavior "Require" -MatchingString $url -UrlScopeRuleType "Folder"

I hope everything is clear. For a complete view, see full code below.

Write-Host "Enter Site URL:"
$url = Read-Host

$searchapp = Get-SPEnterpriseSearchServiceApplication
$src = Get-SPEnterpriseSearchQueryScope  -Identity "Search scope name" -SearchApplication $searchapp
if($src -ne $null) {
	Remove-SPEnterpriseSearchQueryScope -Identity  "Search scope name"  -SearchApplication $searchapp
}
$src = New-SPEnterpriseSearchQueryScope -Name "Search scope name" -SearchApplication $searchapp -Description "Search scope name" -DisplayInAdminUI $true

$rule = New-SPEnterpriseSearchQueryScopeRule -Scope $src -RuleType "Url" -Url $url -FilterBehavior "Require" -MatchingString $url -UrlScopeRuleType "Folder"
Advertisements