Use PowerShell to Execute SharePoint Search Queries

In this example, I’m narrowing my search to one library and a search term.

At a high level, the script is searching the FoodSite for the word GoodTaco.


function Query-SPSearch {
		[Parameter()][Int32]$Count = 10

	$QueryXml = @"

<QueryPacket xmlns="urn:Microsoft.Search.Query" >
            <QueryText type="STRING">$KeywordQuery</QueryText>
	$ServicePath = "/_vti_bin/search.asmx"
	$SearchWS = New-WebServiceProxy -Uri ($WebApplicationPath + $ServicePath) -UseDefaultCredential
	$Results = $SearchWS.QueryEx( $QueryXml )
	# we excluded all other result sets, but just in case get the one we want:

Query-SPSearch -WebApplicationPath "" -KeywordQuery "GoodTaco AND path:" -Count 20 | Format-Table Title, Author, Path

Use PowerShell to Query The SharePoint Search Index

Needed to write a script to validate that the SharePoint crawler was picking up all items in a library.
One of my document libraries had over a 100,000 document, and I needed to make sure all of them were being indexed.
Document library can support millions of documents, IF you use a good foldering structure.

function GetSearchIndex ($site, $file)
		$fOutPut = $null
		$kq = new-object Microsoft.Office.Server.Search.Query.KeywordQuery($site)
		$kq.ResultTypes= [Microsoft.Office.Server.Search.Query.ResultType]::RelevantResults
		$kq.QueryText = $file
		$kq.HiddenConstraints = 'scope:"All Sites"'
		$kq.RowLimit = 10
		$res = $kq.Execute()
		$table = new-object System.Data.DataTable
		if($table.Rows.Count -eq 0)
			$fOut = "Failed"
			$fOut = "Passed"
		return $fOut

$file = "c:\indexCheck.txt"
$cfcSite = Get-SPWeb ""
$nv = $cfcSite.Lists["bigLibrary"]

$spQuery = New-Object Microsoft.SharePoint.SPQuery 
$spQuery.ViewAttributes = "Scope='Recursive'"
$spQuery.RowLimit = 2000 
$caml = '<OrderBy Override="TRUE"><FieldRef Name="ID"/></OrderBy>' 
$spQuery.Query = $caml 

    $listItems = $nv.GetItems($spQuery)
    $spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
    foreach($item in $listItems)
        $sResult = GetSearchIndex "" $item.Name
		if($sResult -eq "Failed")
			$item.Name | Out-File $file -Append			
while ($spQuery.ListItemCollectionPosition -ne $null)

Query the list/library in batches of 2,000.
Looping through the returned items.
Call function to see if the item is in the index (query SharePoint).
From the function, return a value of Passed or Failed.
If Failed is true, log it to a text file on the C:\ drive.