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?

Example

    $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) 
    
    $clientContext.ExecuteQuery()
    $results.Value[0].ResultRows

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

Resources

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.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

Be the first to comment

Leave a Reply