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


  1. 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?

    1. The APPId must have permissions granted to the sites! Double check if your AppID has permissions to the sites. How to use Connect-PnPOnline with AppID and AppSecret in SharePoint Online


Please Login and comment to get your questions answered!

Powered by Blogger.