top of page

Working With Search And Refiners Using CSOM PowerShell On SharePoint


In this article, you will learn how to search with refiners and query on SharePoint 2013 / Online sites using PowerShell CSOM.

Steps Involved

We will see how we can filter out the search results using filter type. The following steps explain the flow.

1. Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.Publishing.dll.

Add-Type -

Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.


Add-Type -

Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.



Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.


2. Initialize client context object with the site URL. $siteURL = "" $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

3. If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context. 

# Not required for on premise site - Start

$userId = "" $pwd = Read-Host -Prompt "Enter password" -AsSecureString  

$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  

$ctx.credentials = $creds  

# Not required for on premise site - End

4. If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server.

5. First we will identify the refinement data to be passed to get the final result set.

  • Initialize the new Keyword query object.

  • Set the query text and refiners to be applied.

  • Get the results using SearchExecutor object and execute query method. Following piece of code will fetch us all the refinement filter type values.

$searchquery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)  

$searchquery.QueryText = "*"

$searchquery.Refiners = "FileType"

$searchExec = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)      

$searchResults = $searchExec.ExecuteQuery($searchquery)      


6. Get the refinement results. Get the filter type value for ppt document items. Then build the refinement filter query.

#Gets all the refinement results. Value[0] will fetch us the search result set, which we are 

not interested now as its not filtered data. $refinementResults = $searchResults.Value[1]  

$pptRefinement = "" foreach ($refinementOption in $refinementResults.ResultRows)  {                

  write-host "RefinerName : " $refinementOption.RefinementName "RefinementToken : " 


# Gets refinement token value for powerpoint documents.

if($refinementOption.RefinementName -contains "ppt"){          

$pptRefinement = $refinementOption.RefinerName + ":" + 


}  }  

7. Get the final result set using the refinement filters which we have obtained in the above step.

  • Initialize the keyword query object with necessary query text and refiners.

  • Then add the refinement filter data. (For example, FileType:"ppt refiner value")

  • Execute the query using the executor object and initialized query.

  • Finally Execute query using the context.

  • Using for each loop, get the necessary results.

$searchquery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)  

$searchquery.QueryText = "your keyword"

$searchquery.Refiners = "FileType"

#Add necessary refinement filters here. In my case, I have added only ppt filter query $searchQuery.RefinementFilters.Add($pptRefinement)  

$searchExec = New- Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)      

$searchResults = $searchExec.ExecuteQuery($searchquery)      


foreach ($resultItem in $searchResults.Value[0].ResultRows)  {                 

Write-Host $resultItem.Title  


Like wise other properties can also be retrieved.


Thus you have learned how to access the search results using the refiners with CSOM PowerShell commands on SharePoint 2013/ SharePoint online.



bottom of page