Dot Net Mafia

Group site for developer blogs dealing with (usually) Ionic, .NET, SharePoint, Office 365, Mobile Development, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog



Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for Ionic, SharePoint, and Office 365.

Creating Enterprise Search Scopes with PowerShell

I posted a while back on how to do your Managed Property mappings in PowerShell, so I wanted to follow up with how to add search scopes next.  I have to give props to the SDK team because they have done a pretty good job documenting all of these PowerShell commands.  They even provide examples which I like a lot.  When trying out command sto create scopes and their associated rules, I ran into a few things that I wanted to share.  You will want to put all of these commands into a .ps1 script file.  You can create your script file in PowerShell-ISE or just use notepad.  We start out by getting a reference to the search application.

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"

At this point, we are ready to create a new scope.  It’s pretty simple.  To create a scope, we use the New-SPEnterpriseSearchQueryScope command.  Here is my command to create a scope called My Scope.  One thing to note here is that DisplayInAdminUI is a required parameter.  It turns out you can create hidden scopes with PowerShell even though you can’t do it through the UI.

$scope = New-SPEnterpriseSearchQueryScope -Name "My Scope" -Description "My scope created in PowerShell" -SearchApplication $searchapp -DisplayInAdminUI $true


We now want to create scope rules for this scope.  I assign the resulting object from the above command into a variable called $scope so that we can pass it to the scope parameter of the New-SPEnterpriseSearchQueryScopeRule command.  When creating a scope rule through the UI, you have four choices: All Content, Property Query, Content Source, and Web Address.  We’ll start with the simplest one, All Content.  For this, we just specify a RuleType value of AllContent.  All commands to create new scope rules require a URL.  It’s not entirely clear what this URL does since it’s not something you enter in the UI when you create a rule.  From the SDK, it simply states that it “specifies the results URL that is associated with the query rule.”  I guess you could give it a path to the results page in your search center.  For now, I just specify the path to the server and it seems to work.

New-SPEnterpriseSearchQueryScopeRule -RuleType AllContent -url http://sp2010 -scope $scope


Next, we’ll create a scope rule using the PropertyQuery Rule Type.  It requires a few more parameters.  The ManagedProperty parameter specifies the name of your managed property.  In my example, I am going to use a property called Color.  The PropertyValue parameter specifies the value.  I want to see products that are red, so I’ll specify Red here.  With any scope rule, you can specify whether the results from the rule should be Included, Required, or Excluded.  We specify this with the FilterBehavior parameter.  This parameter is required when using this PropertyType (note the SDK says it is optional).  Also, when using a property query, the SearchApplication parameter is required too so just pass it the value $searchapp and it will work fine.  Here is the command.

New-SPEnterpriseSearchQueryScopeRule -RuleType PropertyQuery -ManagedProperty Color -PropertyValue Red -FilterBehavior Include -url http://sp2010 -scope $scope -SearchApplication $searchapp


When you execute the command it gives you some basic info about the rule you set up as well as an estimated count.

Setting up a scope with a RuleType of Url took me a bit longer to figure out. This is because there is a parameter called UrlScopeType and the SDK didn’t say what value was expected there.  I had to do some digging.  The SDK, didn’t say what values it was expecting nor did the Get-Help command.  I did some reflecting and finally found an enum that had the answer.  The values it wants are Folder, HostName, or Domain. This of course makes sense when you go back and look at the UI and see the parameters you specify there.


The other parameter you need to know about here is MatchingString.  You specify the value to the folder, hostname, or domain you want to use.  In this case I am setting up a rule for a particular subsite.

New-SPEnterpriseSearchQueryScopeRule -RuleType Url -MatchingString http://sp2010/bcs -UrlScopeRuleType Folder -FilterBehavior Include -url http://sp2010 -scope $scope


So now we can create rules for all content, a web address, and a property query. However, if you have ever set up a scope before, you know there is one more type.  That type is a content source.  The SDK, didn’t have this type listed so I looked around in reflector again and found that we could specify a value of ContentSource for the RuleType parameter.  However, when I tried to specify that parameter, it didn’t work.  I took a look at the code and discovered that there is no code implemented to create a content source scope rule.  After doing some experimenting in PowerShell, I did discover the answer, but I’ll save that for the next post where I will show you how I figured it out.

Remember you can add multiple rules at a time to one scope.  Just put them all in one script file and run it.  Also if you need to delete your Scope, you can use the Remove-SPEnterpriseSearchQueryScope command but you have to pass it an actual scope object which you can get with the Get-SPEnterpriseSearchQueryScope command.  Here is how I deleted my scopes as I was testing.

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
Get-SPEnterpriseSearchQueryScope -Identity "My Scope" -SearchApplication $searchapp | Remove-SPEnterpriseSearchQueryScope

You can do so much in SharePoint with PowerShell.  This is just one more thing, I won’t have to manually configure any more.  The SDK does a great job documenting all of the commands out there (although I would like to see that info on the UrlScopeType parameter added some time :) ).  Try some of them out and you’ll be amazed at what you can accomplish.



Twitter Trackbacks for Creating Enterprise Search Scopes with PowerShell - Corey Roth - - Tip of the Day [] on said:

Pingback from  Twitter Trackbacks for                 Creating Enterprise Search Scopes with PowerShell - Corey Roth - - Tip of the Day         []        on

August 2, 2010 3:15 PM

Peter said:

Hi Corey, thanks for this blog post, good stuf..

Do you know the powershell command/s to use to update the display groups after successfully creating a search scope?



August 8, 2010 8:36 PM

CoreyRoth said:

@Peter I don't believe there is one.  I've got an idea though.  Give me a bit and I'll have something for you.

August 8, 2010 11:52 PM

CoreyRoth said:

@Peter  Ok, you inspired me.  I created some cmdlets to create scope display groups.  Check out the following post.

August 9, 2010 3:12 PM

Peter said:

Thanks Corey.  This looks fantastic.  I'll give it a whirl.  What a turnaround!

August 9, 2010 4:18 PM

Peter said:

Corey, I downloaded the Codeplex tookit and tried it out, great stuff!  

I then went back and read your blog post on creating cmdlet's and had a crack myself at a couple for the toolkit if you'd like (and which have helped me out for my current work)...  I saw you noted them as possible Future Commands.   I've created : Remove-SPEnterpriseSearchQueryScopeDisplayGroupScope (simply removes a scope from a given display group) and Get-SPEnterpriseSearchQueryScopeDisplayGroup (returns a display group to work with).



August 9, 2010 6:50 PM

CoreyRoth said:

@Peter That's excellent.  I was actually planning on writing those two commands tonight so thanks for saving me the time. :-)

I don't have the code set up in TFS yet, so if you want to just post the snippets to a discussion on CodePlex or ping me on twitter and I'll shoot you my E-mail address and I'll get them included in the build.


Corey (@coreyroth)

August 9, 2010 7:03 PM

Peter said:

Hi Corey, I've just added the code as a discussion on CodePlex... if you could run your eye over it and make any updates as you wish.

I made another small change to the New-SPEnterpriseSearchQueryScopeDisplayGroupScope cmdlet also, just to check if the scope already exists in the display group first.  I'll add this as a discussion item also.

// add the scope to the display group if it hasn't already been added





August 9, 2010 7:49 PM

Issues creating SharePoint 2010 search scopes programmatically at SharePoint Config said:

Pingback from  Issues creating SharePoint 2010 search scopes programmatically at SharePoint Config

April 7, 2011 11:14 AM

Creating SharePoint 2010 search scopes programmatically at SharePoint Config said:

Pingback from  Creating SharePoint 2010 search scopes programmatically at SharePoint Config

April 8, 2011 8:49 AM

Adalto said:


Can I use PowerShell to copy all Shared Scopes from a Search Service App to another?


April 13, 2011 12:00 PM

CoreyRoth said:

Unfortunately, there isn't really a very direct way of copying scopes from one application to another using PowerShell.

April 13, 2011 12:43 PM

Adalto said:


Did you do that? I don't know if I try a C# app or PowerShell or whatever =D

I just don't want to rewrite all those rules =/

Thx Roth

April 13, 2011 1:28 PM

CoreyRoth said:

I heard you.  I usually create scopes and property mappings with PowerShell to begin with so I can deploy them later.  Take a look at the SharePoint Enterprise Search migration tool to move scopes.

April 13, 2011 2:03 PM

FAST Search Scopes « Sladescross's Blog said:

Pingback from  FAST Search Scopes « Sladescross's Blog

June 16, 2011 6:00 AM

Girum Wl said:

Nice post. Thank you.

October 19, 2011 10:43 AM

Mate said:

Great help, saved my day! Thanks from the Nordic Countries!

November 7, 2012 3:24 AM

Graham said:

Great blog article, thanks.

I have an interesting issue when I tried to delete the 'All Sites' scope.  It won't.  Is this because its a default scope?  I have a suspicion its corrupt hence my need to delete it.


January 14, 2013 10:13 PM

CoreyRoth said:

@Graham I don't believe that you can delete it so I think that is why you are seeing the issue.

January 15, 2013 2:13 PM

Leave a Comment


About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2018 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems