Search Keyword In SharePoint Online Site Using PowerShell

Introduction

In this blog, I would like to share the steps to search keywords in SharePoint Online site using PowerShell

I have a requirement where I want to get all the search results matching with search keywords from all the site collection and export them in a CSV file.

Below steps to follow,

  • Create connection 
  • Search keyword
  • Export result in CSV file

Powershell Script

$SiteURL ="https://abc.sharepoint.com"  

$keyword="Sachin"  

$currentTime=$(get-date).ToString("yyyyMMddHHmmss");  
$outputFilePath="C:\PnPScript\results-"+$currentTime+".csv"  
#$credentials=Get-Credential  
 
## Connect to SharePoint Online site  
 Connect-PnPOnline -Url $SiteURL -UseWebLogin
 Write-Host "Site connected sucessfully" -ForegroundColor Green
## Executes an arbitrary search query against the SharePoint search index 
  
$results=Submit-PnPSearchQuery -Query $keyword -MaxResults 10 // here it will export 10 items in search result 
  
## 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  
 Write-Host "Result exported sucessfully" -ForegroundColor Yellow

Script Output

Below result is exported in CSV file based on our search keyword.

Search Keyword In SharePoint Online Site Using PowerShell

Summary

In this blog, we have learned how to export search results to a CSV in SharePoint Online using PowerShell