How to Use SharePoint 2010 Windows PowerShell Cmdlets to Manage Search Crawls

How to Use SharePoint 2010 Windows PowerShell Cmdlets to Manage Search Crawls

  • Comments 8
  • Likes


Summary: Guest Blogger Corey Roth explains how to use the Windows PowerShell cmdlets that come with SharePoint 2010 to manage search crawls.

 

Hey, Scripting Guy! Question Hey, Scripting Guy! I need to be able to manage search crawls on my SharePoint 2010 server. Is this something that can be easily accomplished with the Windows PowerShell cmdlets that come with SharePoint 2010?

-- RC

 

Hey, Scripting Guy! Answer Hello RC,

Microsoft Scripting Guy Ed Wilson here. SharePoint 2010 is a huge product. I am running it at home, and it is a great product—but I am not a SharePoint 2010 expert. Not by a long stretch. The cool thing is that if you know Windows PowerShell 2.0, you do not have to be a SharePoint expert to administer the product. However, you should know what you are doing, especially on production systems. I think I will let Corey Roth answer your question.

Photo of Corey Roth

Corey Roth is a software consultant specializing in SharePoint for clients in the energy sector. He has more than 10 years of experience delivering solutions in the energy, travel, advertising, and consumer electronics verticals. Corey specializes in delivering ECM and search solutions to clients using SharePoint. Corey has always focused on rapid adoption of new Microsoft technologies including SharePoint 2010, Visual Studio 2010, .NET Framework 4.0, and Silverlight. He is a member of the DotNetMafia where he blogs about the latest technology and SharePoint. He is dedicated to the community and speaks regularly at user groups and SharePoint Saturdays. He is also the vice president of the Tulsa SharePoint Interest Group

Using Windows PowerShell to start and stop SharePoint search crawls

You can do just about anything you need when it comes to configuring SharePoint search by using Windows PowerShell. One thing you might want to do is kick off a search crawl or stop one that is in progress. If you take a look at the search commands available in Microsoft.SharePoint.PowerShell, you will see there is nothing listed that directly starts a crawl. At first, I thought that this might be something I should add to the SharePoint PowerShell Community Toolkit. However, after I thought about it for a bit, I realized a custom cmdlet simply was not necessary. With the Windows PowerShell commands out there, we have everything we need.

It is pretty simple when you think about it. We can use the Get-SPEnterpriseSearchCrawlContentSource command to get a ContentSource object. After we have a reference to this object, we can start, stop, and pause crawls to our heart’s content. This class has a few methods that you might be interested in: StartFullCrawl, StartIncrementalCrawl, StopCrawl, PauseCrawl, and ResumeCrawl. What each method does should go without explanation.

Let us take a look at an example to start an incremental crawl on the default content source, Local SharePoint Sites. I recommend creating a new script file (.ps1) to test all of your commands. When working with anything in search using Windows PowerShell, we always get a reference to the search service application first. You might have named your search service application something different, but by default it is called Search Service Application. You can verify the name of yours from your Service Applications page in Central Administration. This is shown in the following image.

Image of default name of search service application

After you have the name, you can use the Get-SPEnterpriseSearchServiceApplication command:

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"

And after you have the service application, you can retrieve a list of all available content sources:

Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $searchapp

The following image shows the results.

Image of results of command

This is useful because we can see what all of the content sources are doing, so we will come back to that later. However, for now we want a specific content source so that we can start the incremental crawl. I want the Local SharePoint Sites content source, so we just pass that as the Identity parameter. I assign it to a new variable called $contentsource, so I can call the appropriate method on it:

$contentsource = Get-SPEnterpriseSearchCrawlContentSource "Local SharePoint Sites" -SearchApplication $searchapp

Now that we have the content source, we can take what we know about the ContentSource object and start the crawl using the StartIncrementalCrawl() method:

$contentsource.StartIncrementalCrawl()

Here is the .ps1 file:

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application" 
$contentsource = Get-SPEnterpriseSearchCrawlContentSource "Local SharePoint Sites" -SearchApplication $searchapp 
$contentsource.StartIncrementalCrawl()

If we check the content sources screen in Central Administration, we can verify that the crawl started successfully, as shown in the following image.

Image showing crawl started successfully

As you might have guessed, you can use the same technique to do a full crawl, stop a crawl, or pause/resume a crawl. Pretty simple, right? For example, if you want to stop the crawl, use the same first two lines of the script file and then execute:

$contentsource.StopCrawl()

As you may know, the content source screen in Central Administration also has the ability to start, stop, and pause all crawls at the same time. Can we do that with Windows PowerShell? Of course! By returning all content sources and using the foreach-object command, we can do just that. Remember that when using foreach-object, we can reference the object in the loop by using $_. This means that we can call $_.PauseCrawl() to pause the crawl on all of the content sources. Here is the whole script:

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application" 
$contentsources = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $searchapp | foreach-object { $_.PauseCrawl() }

Taking a look at the content sources pages again, we can verify that my crawls have been paused. They are shown in the following image.

Image showing that crawls have been paused

We can take this a step further. What if I wanted to start a full crawl on all content sources that crawled file shares? Not a problem! If you go back to the screenshot of the content sources, you will see that a property named Type is listed. This tells us what type of content source each one is. My File Share content source has a type of File so I know I can use that value with the where-object. I simply compare $_.Type to the value File and then pipe the results into the foreach-object cmdlet like we did before:

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application" 
Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $searchapp | where-object { $_.Type -eq "File" } | foreach-object { $_.StartFullCrawl() }

The possibilities are endless. As you can see, manipulating crawls is quite easy to do with Windows PowerShell. Try it out the next time you need to kick off a crawl.

 

RC, that is all there is to using Windows PowerShell and the SharePoint 2010 cmdlets to manage search crawls. Guest Blogger Week will continue tomorrow when we will talk about a SharePoint 2010 deployment module.

We invite you to follow us on Twitter and Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Google Docs, crawls all documents availalbe and give query suggestion. How can we do it same in SahrePoint 2010 with Fast Search.

  • Both SharePoint 2010 Enterprise Search and FAST will make query suggestions over time as more searches have been executed.  What do you mean exactly by crawls all available documents?

  • Nice post. The way you written is appreciated  sharepointsolution2010.blogspot.com/.../use-of-sharepoint-2010.html

  • Great and well thought out post. Cheers.

  • Very interesting indeed, and almost everything I need. "Almost" is because I'm looking for a way to not just start a crawl but to also detect when it's finished. Hats off to you if you can provide that nugget.

  • Do you have a command that will clear out all the content from one content source?

    I have a BCS DB content source that uses delete flags. But somebody truncated all deleted rows from the database and now I find no way of removing the dead documents without clearing the entire index or deleting and recreating the content source.

  • Scripting guys,

    Is there a way around the problem of adding content source and having it say it already exists in another content source

     e.g.  The start address "http://mysites.ipfs.com/" already exists in this or another content source.

  • Thank you - Simply just thank you :)