PowerShell to Search SharePoint and Export Results to CSV using Keyword Query

Requirement: Search a SharePoint site and export the search results to a CSV file using PowerShell.

PowerShell to Export SharePoint Search Results to CSV

At times, it can be helpful to export the search results to a familiar format like Excel for further analysis and processing. In this blog post, we will show you how to use PowerShell to search for information in SharePoint and export the search results to a CSV file. This makes it easy to analyze and report on the data that you find.

Here is the PowerShell to search all documents in a SharePoint Online site collection and export search results to CSV:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Parameters
$SiteURL = "https://intranet.crescent.com/"
$CSVFile = "C:\Temp\SearchResults.csv"

#Get the site collection
$Site = Get-SPSite $SiteURL

#Frame Query to search: Get all documents sorted by Last modified
$keywordQuery = New-Object Microsoft.Office.Server.Search.Query.KeywordQuery($Site)
$SearchQuery = "site:https://intranet.crescent.com ContentType:document NOT FileExtension:aspx"
$keywordQuery.QueryText = $SearchQuery
$keywordQuery.SortList.Add("LastModifiedTime","Asc")

#Execute Search
$SearchExecutor = New-Object Microsoft.Office.Server.Search.Query.SearchExecutor
$searchResults = $SearchExecutor.ExecuteQuery($keywordQuery)

#Get Search Results
$Table = $SearchResults.Table
$Table | select Title, Path, Author, LastModifiedTime

#Export Search results to Excel
$Table | Export-Csv $CSVFile -NoTypeInformation

Write-Host -f Green "Search Results Exported to CSV File!"

Once you have executed this script, the search results will be exported to an Excel file, allowing you to sort, filter, and analyze the data in a familiar spreadsheet format. Here is the sample search result CSV:

export sharepoint search results to csv using powershell

By following the scripts provided in this article, you can quickly and easily export your search results to a CSV file. Whether you need to track metrics, analyze trends, or just keep a record of your search results, exporting to Excel can help you to get the most out of your SharePoint search experience. Once you have exported your search results to Excel, you can use the powerful filtering and sorting capabilities of Excel to quickly drill down to the information you need, without having to navigate back and forth between SharePoint pages.

Here is how to search SharePoint Online using Keyword query: PowerShell to Search SharePoint Online Site using Keyword Query

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

6 thoughts on “PowerShell to Search SharePoint and Export Results to CSV using Keyword Query

  • Hi Salaudeen,
    Thank you for all those wonderful posts!
    I tried to use this script to perform a keyword search and I can only get 500 results even though I set the MaxRowLimit to 1000 at the keyWordQuery object and at the SSA level! Is there any other trick to make it work?

    Also, the search is performed at the Web Application Url instead of against a specific site collection?!

    Thanks in advance for any further insights!

    -Saru.

    Reply
  • $keywordQuery = New-Object Microsoft.Office.Server.Search.Query.KeywordQuery($Site). I get an error that can not find type.
    Also where can give my keyword for search

    Reply
  • Yes I am getting the same, just 50 results each time.

    Reply
    • The default RowLimit on Keyword query is 50! You can set the Row limit to something like “$KeyWordQuery.RowLimit = 500”

      Reply
  • I have more than 14000 records return with a search word but I only am getting approx 50 records in CSV

    Reply
    • Besides setting the KeywordQuery.Rowlimit, You may have to also set the search service application’s Row limit:

      $SSA= Get-SPEnterpriseSearchServiceApplication
      $SSA.MaxRowLimit = 10000
      $SSA.Update()

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *