$resultSourceID=$resultSource.ID
$resultSourceLevel="SPSite"
$webURL =$web.Url + "/searchcentre"
$web=Get-SPWeb $webURL
$pageUrl="$webURL/Pages/results.aspx?k=test"
$page=$web.lists["Pages"].Items | ? {$_.Name -eq "results.aspx"}
$page.File.CheckOut();  
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpart = $webpartmanager.WebParts | ? { $_.Title -eq 'Search Results' }
$dataProvider = ConvertFrom-Json $webpart.DataProviderJSON
$dataProvider.SourceID=$resultSourceID
$dataProvider.SourceLevel=$resultSourceLevel
$dataProvider.SourceName=$resultSourceName
$webpart.DataProviderJSON = ConvertTo-Json $dataProvider -Compress
$webpartmanager.SaveChanges($webpart)
$page.File.CheckIn("Changed the Result Source")
$page.File.Publish("Changed the Result Source")
Write-Host "Site Collection scope for the search results in the Search Results webpart is set to 'MyResultSource' successfully"  -ForegroundColor green