Run A Search Query And Export To CSV
Summary
Perform a search query (such as "Show me all News Posts in this tenant") and export the results to CSV.
This script is designed as a starter for you to expand by modifying the query used, and adding whichever managed properties you want to appear in the CSV file, in the order you want.
Any content you can retrieve through search you can use in this script, so as long as you can build the query for it.
The key to this script is the Submit-PnPSearchQuery
cmdlet, which you can also modify in this script, for example to set the Result Source. See more information on the usage of this cmdlet here.
Instructions
- Open your favourite text/script editor and copy/paste the script template below
- Modify the search query to your requirements, add the desired Managed Properties to the Select Properties list
- Also update the
PSCustomObject
with the properties that you require in the resulting CSV file - Open a PowerShell terminal
- Connect to your SharePoint tenancy using PnP PowerShell
- Run the script
- Retrieve the generated CSV file
$itemsToSave = @()
$query = "PromotedState:2"
$properties = "Title,Path,Author"
$search = Submit-PnPSearchQuery -Query $query -SelectProperties $properties -All
foreach ($row in $search.ResultRows) {
$data = [PSCustomObject]@{
"Title" = $row["Title"]
"Author" = $row["Author"]
"Path" = $row["Path"]
}
$itemsToSave += $data
}
$itemsToSave | Export-Csv -Path "SearchResults.csv" -NoTypeInformation
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
Contributors
Author(s) |
---|
James Love |
Smita Nachan |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.