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.
- ###### Input Parameters ######
- $url="https://c986.sharepoint.com/sites/Flow"
- $keyword="test"
- $currentTime=$(get-date).ToString("yyyyMMddHHmmss");
- $outputFilePath=".\results-"+$currentTime+".csv"
- $credentials=Get-Credential
-
- ## Connect to SharePoint Online site
- Connect-PnPOnline -Url $url -Credentials $credentials
-
- ## Executes an arbitrary search query against the SharePoint search index
- $results=Submit-PnPSearchQuery -Query $keyword -MaxResults 10
-
- ## Get the results in the hash table
- $hashTable=@()
- foreach($resultRow in $results.ResultRows)
- {
- $obj=New-Object PSObject
- $resultRow.GetEnumerator()| ForEach-Object{ $obj | Add-Member Noteproperty $_.Key $_.Value}
- $hashTable+=$obj;
- $obj=$null;
- }
-
- ## Export to CSV
- $hashtable | export-csv $outputFilePath -NoTypeInformation
-
- ## Disconnect the context
- Disconnect-PnPOnline
Open Windows PowerShell and navigate to the location where the file is placed.
Run the following command.
CSV file is generated with all the required details.
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.