Search notes:

PowerShell cmdLet Invoke-RestMethod

Querying Wikidata endpoint

The following example demonstrates how the invoke-restMethod can be used to query the Wikidata endpoint with a SPARQL query.
$sparqlQuery =  @'

select distinct
   ?ownerLabel
   ?portalLabel
   ?url
{

   { ?owner   wdt:P8402  ?portal        } .      # wdt:P8402 = Open data portal

   { ?owner   wdt:P131    wd:Q39        }  union # located in Switzerland
   { ?owner   wdt:P131    wd:Q11943     }  union # located in Canton of Zurich
   { ?owner   wdt:P131    wd:Q72        }  union # located in Zurich
   { ?owner   wdt:P17     wd:Q39        }  union # country Switzerland
   { ?owner   wdt:P31     wd:Q70208     }  union # instance of municipality in switzerland
   { ?owner   wdt:P31     wd:Q1005418   }        # instance of federal office of Switzerland

   optional { ?portal  wdt:P856  ?url   }        # wdt:P856 = official website

   service wikibase:label { bd:serviceParam wikibase:language "en" . }
}
limit 100
'@


$body = @{ query = $sparqlQuery }

$result = invoke-restMethod                              `
  -uri     'https://query.wikidata.org/sparql'           `
  -body    $body                                         `
  -headers @{ accept = 'application/sparql-results+json' }

foreach ($r in $result.results.bindings){
    '{0,-30} {1,-40} {2}' -f  $r.ownerLabel.value,  $r.portalLabel.value,  $r.url.value
}
When executed, the query result is:
Canton of St. Gallen           Open data portal St.Gallen               https://daten.sg.ch
Zürich                         Open data portal of the City of Zurich   https://data.stadt-zuerich.ch/
Federal Office of Transport    Open data platform mobility Switzerland  https://opentransportdata.swiss
Basel-Stadt                    Data portal Basel-Stadt                  https://data.bs.ch
Swiss Federal Archives         opendata.swiss                           https://opendata.swiss
Swiss Federal Archives         opendata.swiss                           https://opendata.swiss/en/
Canton of Zürich               Geoinformation kanton Zuerich            https://opendata.swiss/de/dataset?organization=geoinformation-kanton-zuerich
Switzerland                    opendata.swiss                           https://opendata.swiss
Switzerland                    opendata.swiss                           https://opendata.swiss/en/
Thurgau                        Open Government Data Thurgau             https://ogd.tg.ch/
Swiss Federal Railways         SBB open data platform                   https://data.sbb.ch

See also

Using invoke-restMethod to access sqlformat.org to format SQL statements.
REST
invoke-webRequest
Powershell command noun: restMethod

Index