3 things about SharePoint Search Query with Powershell

Powershell Search Query

Did you ever used the search query in Powershell? Powershell can send a search query against the SharePoint Search. It is great in order to automate some tasks, in which you need data from various site collection which can be collected by simply using the search. So how would it look like?


    $SearchQuery = "ManagedProperty:Value"
    $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $keywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($clientContext)
    $keywordQuery.QueryText = $SearchQuery 
    $keywordQuery.RowLimit = 500;
    $searchExecutor = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($clientContext)
    $results = $searchExecutor.ExecuteQuery($keywordQuery) 

3 things you should pay attention

So that is easy. But you should pay attention to three things if you use a Powershell script in order to make a search query.

#1 Choose the right user which executes the script
It is really important. The search results are based on the permission on the user who sends the request. So if you have a employee who would search for “secret documents” he would find less than if your SharePoint Search Account or Admin Account would do the same query. So if you automate tasks with it keep in mind that the user who runs the query should have correct permissions. Not too much but not too less.

#2 RowLimit
If you get always 50 search results it might be hint, that there is a limit. And that’s it. By default the search results row limit is set to 50 elements. You can raise it up to 500.

#3 Check your results
You should always check your results. Is the numbers of result rows correct and is the content of your search results correct? Could it be that there is an information which should not be due to permissions which are not correctly set? Better check twice


If you need a complete step by step instruction, here is a good how to guide

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Karsten Schneider
About Karsten Schneider 296 Articles
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

Be the first to comment

Leave a Reply

Your email address will not be published.