Quick Spellcheck with PowerShell

I’m a person that always needs to google words just to see if they even exist. I made a quick Powershell for that just to check whether or not the word even exists (by using the Wikipedia API – Which normalizes most inputs by itself)

The wikipedia API query will look like this:

https://$lang.wikipedia.org/w/api.php?action=query&titles=$word&format=xml

It’s really easy – at the beginning of the link you set the language your word will be. I’ll set that with the var $lang.
After the domain you’ll tell it to use the public API (Documentation here. )
I want to do a query (action=query) on all sites that have a title containing my $word variable. (titles=$word) and I want it in XML because it’s quite easy to handle in Powershell (format=xml).

Now I’ll need some input for my variables:

function check-word {

    Param(
      [string]$word,
      [string]$lang
    )
...
}

That way the quick spellchecker can now be used like that: check-word wordpress de. This tells the API to look for German pages containing the word „wordpress“.

Next thing is to get the Data from the API:

$url = "https://$lang.wikipedia.org/w/api.php?action=query&titles=$word&format=xml"
$webq = Invoke-WebRequest $url

You could do this in one line but for troubleshooting and understanding it’s better to keep it separated.

$webq will look like this:
webq-output1

In content you’ll see it normalized the query.

If I mistype the query it will look like this:
webq-output2

As you see here Content contains the word missing. That’s the API’s way of telling you that it doesn’t exist.
Now we can work with that.
Next step is to check for that word missing:

$webqc = $webq | select-object content
$exists = $webqc -match "missing"

No here comes the tricky thing: $exits will either contain $true or $false. But it will contain $true if the word DOES NOT exist. And it will contain $false if the word exists.

It’s so important because we want to see a little output and have to think reversed:

if ($exists) {Write-Output "Not Correct"} else {"Correct"}

So after all this there’s not much left to do, here’s the full script:

function check-word {
    Param(
      [string]$word,
      [string]$lang
    )
    
    $url = "https://$lang.wikipedia.org/w/api.php?action=query&titles=$word&format=xml"
    $webq = Invoke-WebRequest $url
    $webqc = $webq | select-object content
    # Variable will contain $true if word does not exist - $false if word exists
    $exists = $webqc -match "missing"
    
    if ($exists) {Write-Output "Not Correct"} else {"Correct"}
}

Put it in your $profile and you’ll always have quick quick spellcheck handy if you just need to see if you wrote that word correctly.
If you want to use it in a work environment make sure to check out how to authenticate your shell against a proxy here: Automatic Proxy Authentication from $Profile

If you got any questions or tips and tricks on how to make this script better, leave a comment!