Creating Scope Rules on SearchScopes using PowerShell

I happened to maintain around 200 rules on 4 search scopes. Adding rules to Search Scopes is a slow process so I use this script to do it.

$scopeName = 'TestScopeName'
$siteUrl = 'http://intranet.contoso.com'
$searchSvcApp = 'Search Service Application'
$site= Get-SPSite $siteUrl
$searchContext = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($site)
$scopes = New-Object Microsoft.Office.Server.Search.Administration.Scopes($searchContext)
$myscope = $scopes.GetScope($siteUrl, $scopeName)
$ssa = Get-SPEnterpriseSearchServiceApplication -Identity $searchSvcApp

And now to actually add the rule to the search scope.

New-SPEnterpriseSearchQueryScopeRule -Url $siteUrl -Scope $myscope -RuleType Url -UrlScopeRuleType Folder -FilterBehavior Include -MatchingString 'http://intranet.contoso.com/ListOrLibraryName/' -SearchApplication $ssa
Test Search Scope Name

Test Search Scope Name