Introduction
In the previous part of this series, you have learned how to create content source and select the appropriate option for each category. In this continuation, you will understand how to create PowerShell scripts to automate Crawls.
We can create Schedules to run at specific intervals using schedules, but what if you want to create Automated PowerShell script for these content sources using Crawling? Here is the detailed answer.
Description
As the first step, open PowerShell ISE as Administrator and add PSSnapin for Microsoft.SharePoint.PowerShell if it's not added already.
The below script will check if the Microsoft.SharePoint.PowerShell is added or not, if not this will do it.
- $snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }
- if ($snapin -eq $null) {
- Write-Host "[INIT] Loading SharePoint Powershell Snapin"
- Add-PSSnapin "Microsoft.SharePoint.Powershell"
- }
Next, you have to select the search application that is running in the farm.
Get-SPEnterpriseSearchCrawlContentSource and the cmdlet will get all the search application content source available, but you need to provide Search Application name.
- #Get all content sources and their type, crawl status and last Crawl completed.
- Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application"

Then next, from user input, Content Source ID is saved.
- #Get user input on Content Source
- $ContentSource = Read-Host -Prompt 'Input your Content Source
Below code will display the content source which we have selected for confirmation
- $searchApp =
- Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application" |
- Where-Object { $_.ID -eq "$ContentSource" }
- #Display the selected content Source
- Write-Host 'You have selected the content Source:' $searchApp.Name -ForegroundColor Yellow

Below condition will check if the Crawl status is Idle or not.



Join the conversation! Your thoughts help the community grow.