Pretty useless cat facts and PowerShell – (there is also a picture of a cat)

Does this seem familiar? You are sitting in a meeting and the discussion is heated. Your thoughts wander and suddenly you are directly addressed. That’s exactly when you should have had a cat fact at hand to defuse the situation. We show you how to have a cat fact ready every time you need one.

Photo by Pixabay on Pexels.com

The first thing you need is a cat API, thank god there is already one out there. The site https://catfact.ninja/ provides a nice Rest API. Cats and ninjas, that’s probably the best combination ever. Below ist the function we will use for all the following snippets. If you use Get-CatFact -TargetLanguage you can choose all the short codes from Google translate. If you do not use that parameter english will be the default language.

Function Get-CatFact{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$false)]  
        [String]
        [ValidateSet("af","ga","sq","it","ar","ja","az","kn","eu","ko","bn","la","be","lv","bg","lt","ca","mk","zh-CN","ms","zh-TW","mt","hr","no","cs","fa","da","pl","nl","pt","en","ro","eo","ru","et","sr","tl","sk","fi","sl","fr","es","gl","sw","ka","sv","de","ta","el","te","gu","th","ht","tr","iw","uk","hi","ur","hu","vi","is","cy","id","yi")]
        $TargetLanguage = "En"
    )

    $Result = Invoke-WebRequest -Uri "https://catfact.ninja/fact" | ConvertFrom-Json
    $Text = $Result.fact
    
    if($TargetLanguage.ToLower() -ne "en"){
        $Uri = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=$($TargetLanguage)&dt=t&q=$Text".Replace(" ", "%20").Replace("\", "\\")
        $Response = Invoke-WebRequest -Uri $Uri -Method Get
        [String]$Text = ($Response.Content.Split("`"")[1]) -replace "`"",""
    }
    return $Text
}

Replace your clipboard with a random cat fact every couple of seconds.

No clue why anyone would want to do that but here we go:

while($true){
    Get-CatFact | Set-Clipboard
    Start-Sleep -Seconds 5
}

Annoy yourself with a cat fact everytime you start your computer

To have a nifty pop up for your cat fact, or if you just want to annoy your coworker follow these steps. And paste the script in to the startup location (Windows + R and type shell:startup).

$Wshell = New-Object -ComObject Wscript.Shell
$Output = $Wshell.Popup((Get-CatFact))

Attach the cat fact to your context menu

To instantly have a new fact at hand just add your script to your context menu.

#Add this and the Get-CatFact function to a script.
Get-CatFact | Set-Clipboard

After setting up your script add the following to your registry. Change the highlighted line to your script path.

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\Get a cat fact]
@="&Get a cat fact"
"Icon"="%SystemRoot%\System32\shell32.dll,258"
[HKEY_CLASSES_ROOT\Directory\Background\shell\Get a cat fact\command]
@="powershell.exe -windowstyle hidden -file C:\\Scripts\\Catfacts.ps1"

Leave a Reply

Powered by WordPress.com.

Up ↑

%d bloggers like this: