{"id":16981,"date":"2010-09-28T00:01:00","date_gmt":"2010-09-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/28\/how-to-use-sharepoint-2010-windows-powershell-cmdlets-to-manage-search-crawls\/"},"modified":"2010-09-28T00:01:00","modified_gmt":"2010-09-28T00:01:00","slug":"how-to-use-sharepoint-2010-windows-powershell-cmdlets-to-manage-search-crawls","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-to-use-sharepoint-2010-windows-powershell-cmdlets-to-manage-search-crawls\/","title":{"rendered":"How to Use SharePoint 2010 Windows PowerShell Cmdlets to Manage Search Crawls"},"content":{"rendered":"<p><strong>Summary<\/strong>: Guest Blogger Corey Roth explains how to use the Windows PowerShell cmdlets that come with SharePoint 2010 to manage search crawls.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"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?<\/p>\n<p>&#8212; RC<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/> Hello RC, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. SharePoint 2010 is a huge product. I am running it at home, and it is a great product&mdash;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. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4744.HSG9281001_48D30ECB.jpg\"><img decoding=\"async\" height=\"244\" width=\"184\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8473.HSG9281001_thumb_3C64EBA2.jpg\" alt=\"Photo of Corey Roth\" border=\"0\" title=\"Photo of Corey Roth\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/p>\n<p>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 <a href=\"http:\/\/www.dotnetmafia.com\">DotNetMafia<\/a> 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 <a href=\"http:\/\/www.tulsasharepoint.com\/default.aspx\">Tulsa SharePoint Interest Group<\/a><\/p>\n<p><strong>Using Windows PowerShell to start and stop SharePoint search crawls<\/strong><\/p>\n<p>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 <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee906563.aspx\">search commands<\/a> available in <strong>Microsoft.SharePoint.PowerShell<\/strong>, 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 <a href=\"http:\/\/sppsck.codeplex.com\/\">SharePoint PowerShell Community Toolkit<\/a>. 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.<\/p>\n<p>It is pretty simple when you think about it. We can use the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ff607730.aspx\">Get-SPEnterpriseSearchCrawlContentSource<\/a><i> <\/i>command to get a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.office.server.search.administration.contentsource.aspx\">ContentSource<\/a> object. After we have a reference to this object, we can start, stop, and pause crawls to our heart&rsquo;s content. This class has a few methods that you might be interested in: <strong>StartFullCrawl<\/strong><i>, <\/i><strong>StartIncrementalCrawl<\/strong><i>, <\/i><strong>StopCrawl<\/strong><i>, <\/i><strong>PauseCrawl<\/strong><i>, <\/i>and <strong>ResumeCrawl<\/strong>. What each method does should go without explanation.<\/p>\n<p>Let us take a look at an example to start an incremental crawl on the default content source, <strong>Local SharePoint Sites<\/strong>. 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 <strong>Search Service Application<\/strong>. You can verify the name of yours from your Service Applications page in Central Administration. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1537.HSG9281002_66D11CBF.jpg\"><img decoding=\"async\" height=\"39\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0044.HSG9281002_thumb_7B566C3D.jpg\" alt=\"Image of default name of search service application\" border=\"0\" title=\"Image of default name of search service application\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/p>\n<p>After you have the name, you can use the <strong>Get-SPEnterpriseSearchServiceApplication<\/strong> command:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$searchapp<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-SPEnterpriseSearchServiceApplication<\/span><span style=\"color: #808080\"> &#8220;Search Service Application&#8221;<\/span> <\/div>\n<\/blockquote>\n<p>And after you have the service application, you can retrieve a list of all available content sources:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Get-SPEnterpriseSearchCrawlContentSource<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-SearchApplication<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$searchapp<\/span> <\/div>\n<\/blockquote>\n<p>The following image shows the results.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8468.HSG9281003_25566A66.jpg\"><img decoding=\"async\" height=\"109\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6712.HSG9281003_thumb_4BB84DB1.jpg\" alt=\"Image of results of command\" border=\"0\" title=\"Image of results of command\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/p>\n<p>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 <strong>Identity<\/strong> parameter. I assign it to a new variable called <strong>$contentsource<\/strong>, so I can call the appropriate method on it:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$contentsource<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-SPEnterpriseSearchCrawlContentSource<\/span><span style=\"color: #808080\"> &#8220;Local SharePoint Sites&#8221; <\/span><span style=\"color: #000000\">-SearchApplication<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$searchapp<\/span> <\/div>\n<\/blockquote>\n<p>Now that we have the content source, we can take what we know about the <strong>ContentSource<\/strong> object and start the crawl using the <strong>StartIncrementalCrawl()<\/strong> method:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$contentsource<\/span><span style=\"color: #000000\">.StartIncrementalCrawl()<\/span> <\/div>\n<\/blockquote>\n<p>Here is the .ps1 file:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$searchapp<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-SPEnterpriseSearchServiceApplication<\/span><span style=\"color: #808080\"> &#8220;Search Service Application&#8221;&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$contentsource<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-SPEnterpriseSearchCrawlContentSource<\/span><span style=\"color: #808080\"> &#8220;Local SharePoint Sites&#8221; <\/span><span style=\"color: #000000\">-SearchApplication<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$searchapp<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$contentsource<\/span><span style=\"color: #000000\">.StartIncrementalCrawl()<\/span> <\/div>\n<\/blockquote>\n<p>If we check the content sources screen in Central Administration, we can verify that the crawl started successfully, as shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0844.HSG9281004_6AFAF484.jpg\"><img decoding=\"async\" height=\"224\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5241.HSG9281004_thumb_4354785A.jpg\" alt=\"Image showing crawl started successfully\" border=\"0\" title=\"Image showing crawl started successfully\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/p>\n<p>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:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$contentsource<\/span><span style=\"color: #000000\">.StopCrawl()<\/span> <\/div>\n<\/blockquote>\n<p>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 <strong>foreach-object<\/strong> command, we can do just that. Remember that when using <strong>foreach-object<\/strong>, we can reference the object in the loop by using <strong>$_<\/strong>. This means that we can call <strong>$_.PauseCrawl()<\/strong> to pause the crawl on all of the content sources. Here is the whole script:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$searchapp<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-SPEnterpriseSearchServiceApplication<\/span><span style=\"color: #808080\"> &#8220;Search Service Application&#8221;&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$contentsources<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-SPEnterpriseSearchCrawlContentSource<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-SearchApplication<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$searchapp<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">foreach-object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.PauseCrawl()<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span> <\/div>\n<\/blockquote>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4251.HSG9281005_5BE415AA.jpg\"><img decoding=\"async\" height=\"229\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5153.HSG9281005_thumb_3B5CD5F8.jpg\" alt=\"Image showing that crawls have been paused\" border=\"0\" title=\"Image showing that crawls have been paused\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/p>\n<p>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 <strong>Type<\/strong> is listed. This tells us what type of content source each one is. My <strong>File Share<\/strong> content source has a type of <strong>File<\/strong> so I know I can use that value with the <strong>where-object<\/strong>. I simply compare <strong>$_.Type<\/strong> to the value <strong>File<\/strong> and then pipe the results into the <strong>foreach-object<\/strong> cmdlet like we did before:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$searchapp<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-SPEnterpriseSearchServiceApplication<\/span><span style=\"color: #808080\"> &#8220;Search Service Application&#8221;&nbsp; <br \/><\/span><span style=\"color: #000000\">Get-SPEnterpriseSearchCrawlContentSource<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-SearchApplication<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$searchapp<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">where-object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #0000ff\">Type<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #808080\"> &#8220;File&#8221; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">foreach-object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.StartFullCrawl()<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span> <\/div>\n<\/blockquote>\n<p>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. <\/p>\n<p>&nbsp;<\/p>\n<p>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. <\/p>\n<p>We invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Guest Blogger Corey Roth explains how to use the Windows PowerShell cmdlets that come with SharePoint 2010 to manage search crawls. &nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[178,56,3,59,45],"class_list":["post-16981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-corey-roth","tag-guest-blogger","tag-scripting-guy","tag-sharepoint","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Guest Blogger Corey Roth explains how to use the Windows PowerShell cmdlets that come with SharePoint 2010 to manage search crawls. &nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=16981"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16981\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=16981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}