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 a Content Source Scope Rule using PowerShell

Two weeks ago, I posted about how to create scopes and scope rules using PowerShell.  At the end of the post I mentioned that there was no rule type for creating scope rules by content type.  I knew there had to be a way to do this, so I created a scope rule using the UI and then I used the Get-SPEnterpriseSearchQueryScopeRule cmdlet to return all the scope rules on my content source.

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$scope = Get-SPEnterpriseSearchQueryScope -Identity "File Share" -SearchApplication $searchapp
Get-SPEnterpriseSearchQueryScopeRule -Scope $scope


From this result, the answer was immediately obvious to me.  ContentSource is a managed property and we already know how to create scope rules like that using the PropertyQuery rule type.  Remember how we can query by content source?  Let’s give it a try and see what happens.  I am going to create a new scope and have it use my BCS content source called Products.  Here is what the syntax looks like.

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$scope = New-SPEnterpriseSearchQueryScope -Name "My Scope" -Description "My scope created in PowerShell" -SearchApplication $searchapp -DisplayInAdminUI $true
New-SPEnterpriseSearchQueryScopeRule -RuleType PropertyQuery -ManagedProperty ContentSource -PropertyValue Products -FilterBehavior Include -url http://sp2010 -scope $scope -SearchApplication $searchapp

I’ll only mention the relevant parameters today.  If you are curious about the other ones, see the previous post where I go into detail on each one.  On the New-SPEnterpriseSearchQueryScopeRule method, we set the RuleType parameter to ContentSource.  Then, we set the PropertyValue to the name of the content source.  In my case, I set it to Products.  Executing the script above, I get the following result.


It appears to have worked, but let’s verify it in the UI.


Excellent.  The new scope was created with the content source rule and it has 43 items included.  Now, I can pretty much script all of my search settings in PowerShell.  Once you have your scopes created, if you want to create scope display groups be sure and check out the SharePoint PowerShell Community Toolkit.



Twitter Trackbacks for Creating a Content Source Scope Rule using PowerShell - Corey Roth - - Tip of the Day [] on said:

Pingback from  Twitter Trackbacks for                 Creating a Content Source Scope Rule using PowerShell - Corey Roth - - Tip of the Day         []        on

August 11, 2010 5:57 PM

Steve said:


Great article!  With the information above I was able to add a new scope for a content source but I can't seem to get a subset of results back for just the specified content source/scope.  Instead I get results back from all content sources.  It is like it is ignoring my scope when I select it next to the search box in the Fast Search Center site.

I have enabled the scope dropdown in the UI and it shows my new scope.  I have verified that ContentSource is a Managed Property/Crawled Property (FAST SearchAdmin >> Managed Properties).  Any thoughts?  Here is the commands I used to create my scope and rule:

$searchapp = Get-SPEnterpriseSearchServiceApplication "FASTQuerySSA"

$scope = New-SPEnterpriseSearchQueryScope -Name "ContentSrcScope" -Description "A scope for a single content source" -SearchApplication $searchapp -DisplayInAdminUI $true

New-SPEnterpriseSearchQueryScopeRule -RuleType PropertyQuery -ManagedProperty ContentSource -PropertyValue "ContentSrc" -FilterBehavior Include -url -scope $scope -SearchApplication $searchapp

August 12, 2010 2:40 PM

CoreyRoth said:

So your content source is called ContentSrc correct? What does it look like in the UI, does the scope rule show created.  Is there an error next to the scope?  If so, does it go away after compiling the scope rules?  I haven't tested my script in FAST yet, but it should be the same.  I'll try it when I get a chance.

August 12, 2010 3:01 PM

Steve said:

Update:  Even though I can see the new scope in my scopes drop down, when I run the command Get-SPEnterpriseSearchQueryScope the CompilationState is still QueryExpanded and it shows that theLastCompilationTime is 1/1/0001.  Meaning it hasn't been compiled yet.  Could this be my problem?  If so how do you force a compile?

August 12, 2010 3:10 PM

CoreyRoth said:

@Steve You can compile it through the UI.  Look for the start compilation link in your service application.  You can also download the SharePoint PowerShell Community Toolkit and execute the command in PowerShell.

August 12, 2010 3:18 PM

Steve said:

To answer your questions, yes my content source is called ContentSrc (I made it generic for this post).  There is no error next to the scope in Central Admin.  Update Status shows Ready but Items shows 0 (zero).  

When I run the command Get-SPEnterpriseSearchQueryScope the CompilationState is QueryExpanded.  So, I am guessing it is not compiled and therefore will not return any results.  Correct?

So how do you compile a scope?  I have been looking for the last hour or so and can't find anything.

August 12, 2010 3:25 PM

Steve said:

I don't have the option to compile the scope from my FASTContentSSA.  When I select the drop down for my scope I do not see an option to compile.  All I see is Edit Properties and Rules.., Make Copy as Shared, and Delete.  I wonder if this is due to FAST and you have to compile a different way?  

Also, I see on the command New-SPEnterpriseSearchQueryScope there is an option called -CompilationType but the docs out on TechNet don't give you any clue as to what type of data that parameter expects.  I am going to look at that more.  I will also take a look at the toolkit.

August 12, 2010 3:44 PM

Lee C. said:


Thanks for the helpful info!  You got me really close, bu there is one thing preventing me from scripting all of my Search Server crawling setup.  I'm hoping you can help me to conquer the remaining item.

I cannot figure out how to script Crawl Rules that use forms authentication (FormRuleAccess).  How can I supply the Form URL, username, and password via PowerShell script?

November 30, 2010 3:06 PM

CoreyRoth said:

@Lee that's a good question.  I suspect, you will have to create the crawl rule and get an object back and then look in the API to see how to set the rest of those properties.  I'll try to take a look when I get a chance.

December 6, 2010 2:53 PM

Lee C. said:

If I could find it in the API documentation I would not have asked for help.  :]  This is the only API doc I have found:

Do you know of a better one?

December 7, 2010 10:02 AM

CoreyRoth said:

@Lee Have you looked at the details of Net-SPEnterpriseSearchCrawlRule?

December 8, 2010 9:10 AM

Gareth M said:

Thanks for this great article Corey, saved me lots of time.  

The only thing I would add is to do a Scope Update from the CA: Search Service app main screen, so that the scope can be used straightaway rather than waiting for the schedule to kick in.

December 10, 2010 6:04 AM

FAST Search Scopes « Sladescross's Blog said:

Pingback from  FAST Search Scopes « Sladescross's Blog

June 18, 2011 2:20 AM

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