How to Search SharePoint Online using PnP PowerShell?

Requirement: Run Search Query in SharePoint Online using PowerShell

How to Search SharePoint Online using PowerShell?

Using Keyword query to execute search and export results to CSV with CSOM is explained in my other post: How to Search SharePoint Online using PowerShell-Keyword Query?, Now, I need to get the List Item ID of the latest item created in a SharePoint Online list:

#Config Variables
$SiteURL = ""
$ListURL= ""
$SearchQuery = "Path:" + $ListURL
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Perform Search     
$SearchResults = Submit-PnPSearchQuery -Query $SearchQuery -All -SelectProperties ListItemID -SortList @{Created="Descending"}
#Get the Last Created Item's ID
Write-host "The Latest Item ID is:" $SearchResults.ResultRows[0]["ListItemID"]

PnP PowerShell to Search SharePoint Online site:

#Config Variables
$SiteURL = ""
$SearchQuery = "Title:document* Path:" + $SiteURL

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Run Search Query     
$SearchResults = Submit-PnPSearchQuery -Query $SearchQuery -All

$Results = @()
foreach($ResultRow in $SearchResults.ResultRows)  
    #Get All Properties from search results
    $Result = New-Object PSObject  
    $ResultRow.GetEnumerator()| ForEach-Object { $Result | Add-Member Noteproperty $_.Key $_.Value}  

To get All available properties of the search results, use: $SearchResults.ResultRows[0] that will get you all properties of the search results: 

  • Author
  • CollapsingStatus
  • contentclass
  • ContentTypeId
  • Culture
  • deeplinks
  • Description
  • DisplayAuthor
  • docaclmeta
  • DocId
  • EditorOWSUSER
  • FileExtension
  • FileType
  • GeoLocationSource
  • HitHighlightedProperties
  • HitHighlightedSummary
  • importance
  • IndexDocId
  • IsContainer
  • IsDocument
  • IsExternalContent
  • LastModifiedTime
  • LinkingUrl
  • OriginalPath
  • ParentLink
  • PartitionId
  • Path
  • PictureThumbnailURL
  • piSearchResultId
  • ProgId
  • RenderTemplateId
  • ResultTypeId
  • ResultTypeIdList
  • SecondaryFileExtension
  • SectionIndexes
  • SectionNames
  • ServerRedirectedEmbedURL
  • ServerRedirectedPreviewURL
  • ServerRedirectedURL
  • SiteDescription
  • SiteId
  • SiteLogo
  • SiteName
  • Size
  • SPWebUrl
  • Title
  • UniqueId
  • UrlZone
  • ViewsLifeTime
  • ViewsRecent
  • WebId
  • WebTemplate
  • WorkId
  • Write

Get All Excel Files in the Site Collection and Export Search Results to CSV

Let’s search for all Excel files from a SharePoint Online site and export search results to a CSV file.

$SiteUrl = ""
$SearchQuery = "*.xlsx Path:" + $SiteURL
$CSVFile = "C:\Temp\ExcelFiles.csv"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteUrl -UseWebLogin

#Execute Search 
$SearchResults = Submit-PnPSearchQuery -Query $SearchQuery -All -TrimDuplicates $False -SelectProperties Filename, Author, Size, ListItemID, LastModifiedTime

#Collect Data from search results
$Results = @()
ForEach ($ResultRow in $SearchResults.ResultRows) 
    $Results += [pscustomobject] @{
        Filename   = $ResultRow["Filename"]
        Author = $ResultRow["Author"]
        Size = $ResultRow["Size"]
        LastModified = $ResultRow["LastModifiedTime"]
        ListItemID     = $ResultRow["ListItemID"]
        ParentFolder = $ResultRow["ParentLink"]
        URL       = $ResultRow["Path"]
#Export results to CSV
$Results | Export-Csv -Path $CSVFile -NoTypeInformation

This script searches the given search query and exports search results to a CSV file.

PowerShell to export search results to CSV in SharePoint Online

Please note, this approach queries files from search indexed, but there could be a lag in indexing schedules index may not real-time. Here is a reference on Submit-PnPSearchQuery

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

3 thoughts on “How to Search SharePoint Online using PnP PowerShell?

  • October 4, 2020 at 5:53 PM

    Thank you for your valuable information. It was very helpful.
    One comment on this is that it would be better to add encoding option when downloading to Excel like this to avoid weird characters in case of using Asian characters.

    $Results | Export-Csv -Path $CSVFile -NoTypeInformation -Encoding Default

  • March 11, 2020 at 3:02 PM

    I am trying to do the same thing but i connecting via AppID. I get access denied when i run the search cmdlet. Any possible solution for this?


Leave a Reply