Get Search Results in SharePoint 2013 using PowerShell

I’ve started playing with some of the search APIs in SharePoint 2013 lately, and wanted to transpose my experiments into PowerShell to see just how easy it was to do. I have been developping a C# client that runs directly on the server, prompts the user to enter a keyword and lists all search results received back from the SharePoint Search Service Application. I managed to replicate this client application in PowerShell by creating a 10 lines script. I basically converted over the C# code into PowerShell.

Before getting started, we need to make sure we load the Microsoft.Office.Server.Search.Query Assembly within our PowerShell session. Our code will by using objects from that namespace.

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server.Search.Query”)

My code uses the Microsoft.Office.Server.Search.Query.KeywordQuery object to retrieve search results back. This object takes in a reference to a Site Collection to perform the search against.

$site = Get-SPSite http://localhost

$keywordQuery = New-Object Microsoft.Office.Server.Search.Query.KeywordQuery($site)

Next step is to prompt the user to input a keyword for the search results. Once obtained, we will assign this keyword to QueryText property our newly created KeywordQuery object.

$keyword = Read-Host “Search Term”

$keywordQuery.QueryText = $keyword

We now have an object that represent our query to send to the server, but in order to execute this Query, we need to instantiate a second object, the Microsoft.Office.Server.Search.Query.SearchExecutor. This object exposes a method name ExecuteQuery() to which we wil pass our query object in order to get search results back.

$searchExec = New-Object Microsoft.Office.Server.Search.Query.SearchExecutor

$searchResults = $searchExec.ExecuteQuery($keywordQuery)

The last step for us is to look at the Table property of the returned object in order to get a list of items that have been identified as matching our search criterias. For clarity’s sake, we will only be exposing the Title, Path, Author, LastModifiedTime and IsDocument property of each search result.

$table = $searchResults.Table

$table | Select Title, Path, Author, LastModifiedTime, IsDocument

So to prove that my code works, I will execute a search using the SharePoint Search interface, search for the word “Gadgets”. Here is what the interface returns for results.

20150110.png

We can see from the screenshot above that search for the word “Gadgets” returns 2 results. Now, let us put our PowerShell script together:

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server.Search.Query”)
Write-Host “`r`n”

$site = Get-SPSite http://localhost
$keywordQuery = New-Object Microsoft.Office.Server.Search.Query.KeywordQuery($site)
$keyword = Read-Host “Search Term”
$keywordQuery.QueryText = $keyword
$searchExec = New-Object Microsoft.Office.Server.Search.Query.SearchExecutor
$searchResults = $searchExec.ExecuteQuery($keywordQuery)

Write-Host “`r`n”
$table = $searchResults.Table
Write-Host $table.Length” Results Found” -BackgroundColor “Green” -ForegroundColor “Black”
$table | select Title, Path, Author, LastModifiedTime, IsDocument

Now, if we were to execute the above PowerShell script against my local SharePoint farm, here is what the result would look like:

2015011002.png

We have therefore been able to obtain the exact same results for our SharePoint 2013 Search Web interface compared with our PowerShell version. This is a neat little script that could prove to be very useful in many scenarios.

Leave a Reply

Your email address will not be published. Required fields are marked *