From: http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2010/07/29/creating-enterprise-search-scopes-with-powershell.aspx
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. 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 useNew-spenterprisesearchqueryscopeCommand. Here is my command to create a scope calledMy scope. One thing to note here is thatDisplayinadminuiIs 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 toScopeParameter ofNew-spenterprisesearchqueryscoperuleCommand. 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 specifyRuletypeValueAllcontent. 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 cocould 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 usingPropertyqueryRule type. It requires a few more parameters.ManagedpropertyParameter specifies the name of your managed property. In my example, I am going to use a property calledColor.PropertyvalueParameter specifies the value. I want to see products that are red, so I'll specifyRedHere. With any scope rule, you can specify whether the results from the rule shocould be encoded, required, or excluded. we specify this withFilterbehaviorParameter. this parameter is required when using this propertytype (note the SDK says it is optional). Also, when using a property query,SearchapplicationParameter is required too so just pass it the value$ SearchappAnd 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 withRuletypeOfURLTook me a bit longer to figure out. This is because there is a parameter calledUrlscopetypeAnd 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 areFolder,Hostname, OrDomain. 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 isMatchingstring. 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 particle 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 coshould specify a valueContentsourceForRuletypeParameter. 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 useRemove-spenterprisesearchqueryscopeCommand but you have to pass it an actual scope object which you can get withGet-spenterprisesearchqueryscopeCommand. 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 wowould 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.