Export Keyword Search Results To CSV In SharePoint Online Using PnP PowerShell

Introduction

 
In this blog, you will learn how to export keyword search results to a csv in SharePoint Online using PowerShell.
 
Prerequisites
 
Ensure SharePoint PnP PowerShell Online cmdlets are installed. Click here for more details on how to install.
 

Steps Involved

 
Open Notepad.
 
Copy the below code and save the file as SearchResults.ps1.
  1. ###### Input Parameters ######  
  2. $url="https://c986.sharepoint.com/sites/Flow"  
  3. $keyword="test"  
  4. $currentTime=$(get-date).ToString("yyyyMMddHHmmss");  
  5. $outputFilePath=".\results-"+$currentTime+".csv"  
  6. $credentials=Get-Credential  
  7.  
  8. ## Connect to SharePoint Online site  
  9. Connect-PnPOnline -Url $url -Credentials $credentials  
  10.  
  11. ## Executes an arbitrary search query against the SharePoint search index  
  12. $results=Submit-PnPSearchQuery -Query $keyword -MaxResults 10  
  13.  
  14. ## Get the results in the hash table  
  15. $hashTable=@()  
  16. foreach($resultRow in $results.ResultRows)  
  17. {  
  18.     $obj=New-Object PSObject  
  19.     $resultRow.GetEnumerator()| ForEach-Object{ $obj | Add-Member Noteproperty $_.Key $_.Value}  
  20.     $hashTable+=$obj;  
  21.     $obj=$null;  
  22. }  
  23.  
  24. ## Export to CSV  
  25. $hashtable | export-csv $outputFilePath -NoTypeInformation  
  26.  
  27. ## Disconnect the context  
  28. Disconnect-PnPOnline  
Open Windows PowerShell and navigate to the location where the file is placed.
 
Run the following command.
  1. .\SearchResults.ps1  
CSV file is generated with all the required details.
 
Export Keyword Search Results To CSV In SharePoint Online Using PnP PowerShell
 
Reference
 
https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/submit-pnpsearchquery?view=sharepoint-ps 
 

Summary

 
Thus, in this blog, you saw how to export keyword search results to csv in SharePoint Online using PowerShell.