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

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

PowerShell to Export SharePoint Search Results to CSV:

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 = "http://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:http://intranet.crescent.com ContentType:document NOT FileExtension:aspx"
$keywordQuery.QueryText = $SearchQuery

#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!"

and the result CSV:

export sharepoint search results to csv using powershell

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

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!

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

  • July 29, 2020 at 10:21 AM

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

    • July 29, 2020 at 11:27 AM

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

  • February 4, 2020 at 7:04 AM

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

    • July 29, 2020 at 11:31 AM

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

      $SSA= Get-SPEnterpriseSearchServiceApplication
      $SSA.MaxRowLimit = 10000


Leave a Reply