Skip to content

Instantly share code, notes, and snippets.

@eugrus
Created September 11, 2025 10:00
Show Gist options
  • Save eugrus/9661c2262a8fb239ac92b59bc8784c43 to your computer and use it in GitHub Desktop.
Save eugrus/9661c2262a8fb239ac92b59bc8784c43 to your computer and use it in GitHub Desktop.
param (
[string]$Query
)
$apiKey = "" # https://console.cloud.google.com/apis/credentials
$cseId = "" # https://programmablesearchengine.google.com/
$results = @()
$encodedQuery = [System.Uri]::EscapeDataString($Query)
for ($start = 1; $start -le 91; $start += 10) {
$url = "https://www.googleapis.com/customsearch/v1?key=$apiKey&cx=$cseId&q=$encodedQuery&num=10&start=$start"
Write-Host "Request URL: $url"
try {
$response = Invoke-RestMethod -Uri $url -Method Get
if ($response.items) { $results += $response.items }
} catch {
Write-Error "Request failed: $($_.Exception.Message)"
}
Start-Sleep -Milliseconds 200
}
$safeQuery = ($Query -replace '[:\\\/\?\*\|"<>\+]', '_')
$date = Get-Date -Format "yyyy-MM-dd"
$outFile = "{0}_{1}_results.csv" -f $date, $safeQuery
$results | Select-Object title, link, snippet | Export-Csv $outFile -NoTypeInformation
Write-Output "Saved $($results.Count) results to $outFile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment