Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Search.dll"
$siteURL = ""
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
#Not required for on premise site - Start
$userId = ""
$pwd = Read-Host -Prompt "Enter password" -AsSecureString
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
$ctx.credentials = $creds
#Not required for on premise site - End
try{
$searchquery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
$searchquery.QueryText = "nakkeeran"
#Result source GUID's for specific set of results can be identified
#using the Search Service Application's Result Source Section.
#The result source can be people, document, video, etc.
$searchquery.SourceId = "00000000-0000-0000-000000000000" #Document
$searchExec = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)
$searchResults = $searchExec.ExecuteQuery($searchquery)
$ctx.ExecuteQuery()
}
catch{
write-host "$($_.Exception.Message)" -foregroundcolor red
}This code snippet will help us in getting the search results by defining result sources.
