How to: Query SharePoint Search with PowerShell

Posted Wednesday, January 8, 2014 12:34 PM by CoreyRoth

I’ve been doing some content analysis with PowerShell lately and I thought it would be useful if I could query SharePoint search.  With the addition of Search to the Managed Client Object Model this task is pretty easily.  I have already written a post on how to do this in a console application.  Let’s adapt that code for use in PowerShell.

Create a new script using your favorite script editing tool such as PowerShell ISE.  We’ll call mine SearchQuery.ps1.  The first thing we want to do in this script is add a parameter so that the search query can be passed via command line.  We’re going to give this parameter a name of $queryText.  The second parameter is a path to a site collection.  We need this for our ClientContext object.

Param([string] $queryText, [string] $siteCollectionUrl)

Now we need to load the following assemblies like we did with our console application.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
  • Microsoft.SharePoint.Client.Search.dll

To do this in PowerShell, we use the following statements:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")

[

System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

[

System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Search")

Now, we get a reference to a ClientContext object to handle all of our client side requests.  W

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteCollectionUrl)

Once we have this object, we can use it to get a KeywordQuery object.

$keywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($clientContext)

We then use the QueryText property to specify our search query.  In this case, we are just passing the value of $queryText received from the command line.

$keywordQuery.QueryText = $queryText

If you haven’t used PowerShell ISE, it will also provide you IntelliSense on your objects similar to Visual Studio.

PowerShellISEIntellisense

Now, we need a SearchExecutor class to execute our query.  It requires a ClientContext as well.

$searchExecutor = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($clientContext)

To execute the query, we use the ExecuteQuery method.  It will return us a $results object which we will access in a similar manner as we did with our client application.  Pass it our KeywordQuery object.

$results = $searchExecutor.ExecuteQuery($keywordQuery)

This doesn’t execute the query though.  Since this is a client object model, we still need to call ExecuteQuery() on our ClientContext object to make the query happen.

$clientContext.ExecuteQuery()

Just like in other examples, the individual results are buried in $results.Value[0].ResultRows.  What’s also unfortunate is that each row comes back with a series of Key / Value pairs which makes binding this data in bulk somewhat painful.  We can example the object and format it in a table but it doesn’t give us much to work with.

$results.Value[0].ResultRows

PowerShellSearchResultsNoFormat

If you know exactly what fields you are looking to access though you can just iterate through the results with a foreach statement.

foreach($result in $results.Value[0].ResultRows)

{

Write-Host $result["Title"] Write-Host $result["Path"] Write-Host $result["Write"]

Write-Host

}

It’s not ideal, but it works alright for small batches of results.  Here’s what my results look like now.

PowerShellSearchResultsSomeFormat

This gives us some search results that we can work with.  Now you use them in PowerShell as you need.  It should even work on remote servers provided the user account you are running under has permissions to the SharePoint site.

Comments

# How to: Query SharePoint Search with PowerShell...

Monday, January 13, 2014 3:18 PM by How to: Query SharePoint Search with PowerShell...

Pingback from  How to: Query SharePoint Search with PowerShell...

# How to: Query SharePoint Search with PowerShell...

Tuesday, January 14, 2014 3:50 AM by How to: Query SharePoint Search with PowerShell...

Pingback from  How to: Query SharePoint Search with PowerShell...

# re: How to: Query SharePoint Search with PowerShell

Thursday, January 23, 2014 6:40 AM by Jimmy

Awesome work. I am trying a similar route to MMS Taxonomy, but no success. Maybe this can be your next challenge.

# SharePoint 2013: Recopilatorio de enlaces interesantes (XXIX)!

Saturday, February 1, 2014 6:08 AM by Blog de Juan Carlos González en Geeks.MS

Como estrenamos mes nuevo, toca un nuevo recopilatorio de enlaces sobre SharePoint 2013. Como siempre

# SharePoint 2013: Recopilatorio de enlaces interesantes (XXIX)! | Pasi??n por la tecnolog??a...

Pingback from  SharePoint 2013: Recopilatorio de enlaces interesantes (XXIX)! | Pasi??n por la tecnolog??a...

# Friday Five - January 30, 2014

Sunday, February 2, 2014 2:13 PM by The Microsoft MVP Award Program Blog

1. How to: Query SharePoint Search with PowerShell By SharePoint MVP Corey Roth 2. Creating Reports

# Friday Five – January 30, 2014 | MSDN Blogs

Monday, February 3, 2014 4:59 AM by Friday Five – January 30, 2014 | MSDN Blogs

Pingback from  Friday Five  – January 30, 2014 | MSDN Blogs

# Friday Five – January 30, 2014 | MSDN Blogs

Monday, February 3, 2014 5:59 AM by Friday Five – January 30, 2014 | MSDN Blogs

Pingback from  Friday Five  – January 30, 2014 | MSDN Blogs

# Friday Five – January 30, 2014 | MSDN Blogs

Monday, February 3, 2014 6:59 AM by Friday Five – January 30, 2014 | MSDN Blogs

Pingback from  Friday Five  – January 30, 2014 | MSDN Blogs

# re: How to: Query SharePoint Search with PowerShell

Saturday, April 29, 2017 8:56 AM by David Sterling

Well done - this gave me some added functionality to the standard wake up script; hitting search for each site collection really speeds the system up.

Leave a Comment

(required)
(required)
(optional)
(required)