![]()
SharePoint Search Queries allow users to retrieve content efficiently from lists, libraries, and sites. With PnP PowerShell, you can perform advanced search queries using Keyword Query Language (KQL) and retrieve structured data with refiners.
This guide covers:
Running basic and advanced search queries
Filtering search results using refiners
Using managed properties for precise searches
Exporting search results
Step 1: Prerequisites
1.1 Install and Update PnP PowerShell
Ensure you have PnP PowerShell installed:
Install-Module PnP.PowerShell -Scope CurrentUser
To update:
Update-Module PnP.PowerShell
1.2 Connect to SharePoint Online
Run the following command to authenticate:
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/YourSite" -Interactive
Replace "yourtenant" and "YourSite" accordingly.
Result: You are now connected to SharePoint.
Step 2: Run a Basic Search Query
To search for a keyword in SharePoint:
Submit-PnPSearchQuery -Query "Project" -All
Result: Returns all SharePoint items that contain the keyword “Project”.
Step 3: Search for Files in a Document Library
To search for specific file types in a document library:
Submit-PnPSearchQuery -Query "FileType:pdf" -All
Result: Retrieves all PDF files stored in SharePoint.
Step 4: Use Keyword Query Language (KQL) for Advanced Searches
4.1 Search by Title or Filename
Submit-PnPSearchQuery -Query "Title:'Annual Report' OR Filename:'Annual_Report.docx'" -All
Result: Finds files with title or filename containing "Annual Report".
4.2 Search within a Specific Site Collection
Submit-PnPSearchQuery -Query "Path:https://yourtenant.sharepoint.com/sites/YourSite" -All
Result: Returns all content within the specified site.
4.3 Search for Modified Documents
Submit-PnPSearchQuery -Query "LastModifiedTime>=2024-01-01" -All
Result: Retrieves files modified after January 1, 2024.
Step 5: Search Using Managed Properties
Managed properties help in refining search results.
5.1 Search by Author Name
Submit-PnPSearchQuery -Query "Author:'John Doe'" -All
Result: Finds all documents created by John Doe.
5.2 Search by Custom Metadata
If you have a custom column (e.g., “Department”), you can search for items tagged under it:
Submit-PnPSearchQuery -Query "Department:HR" -All
Result: Retrieves files where the Department field contains "HR".
Step 6: Apply Refiners to Search Results
Refiners help categorize search results.
6.1 Retrieve Available Refiners
Submit-PnPSearchQuery -Query "*" -Refiners "FileType,Author"
Result: Lists unique file types and authors in SharePoint.
6.2 Filter Search Results Using Refiners
Submit-PnPSearchQuery -Query "FileType:docx" -Refiners "ModifiedBy" -All
Result: Retrieves all Word documents and groups them by modified user.
Step 7: Export Search Results to CSV
To store the search results for analysis, export them to a CSV file:
$searchResults = Submit-PnPSearchQuery -Query "FileType:xlsx" -All
$searchResults.PrimarySearchResults | Export-Csv -Path "C:\SearchResults.csv" -NoTypeInformation
Result: Exports all Excel files in SharePoint to "C:\SearchResults.csv".
Step 8: Automate Search Queries Using PowerShell Script
To run a scheduled search report, create a PowerShell script and execute it daily.
$SiteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
$Query = "FileType:pdf"
$OutputPath = "C:\SharePointSearchResults.csv"
Connect-PnPOnline -Url $SiteUrl -Interactive
$Results = Submit-PnPSearchQuery -Query $Query -All
$Results.PrimarySearchResults | Export-Csv -Path $OutputPath -NoTypeInformation
Result: Automates the SharePoint search and saves results daily.
