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. ## Connect to SharePoint Online site
  8. Connect-PnPOnline -Url $url -Credentials $credentials
  9. ## Executes an arbitrary search query against the SharePoint search index
  10. $results=Submit-PnPSearchQuery -Query $keyword -MaxResults 10
  11. ## Get the results in the hash table
  12. $hashTable=@()
  13. foreach($resultRow in $results.ResultRows)
  14. {
  15. $obj=New-Object PSObject
  16. $resultRow.GetEnumerator()| ForEach-Object{ $obj | Add-Member Noteproperty $_.Key $_.Value}
  17. $hashTable+=$obj;
  18. $obj=$null;
  19. }
  20. ## Export to CSV
  21. $hashtable | export-csv $outputFilePath -NoTypeInformation
  22. ## Disconnect the context
  23. 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.