The Categorizer

the categorizer logo

Create a “Get-Joke” Function in Windows PowerShell


PowerShell Script (.ps1)

# Scripts Library - TheCategorizer.com

function Get-Joke() {

    # Choosing an API (Application Programming Interface) for delivering Jokes
    $source = "https://v2.jokeapi.dev/joke/Any?safe-mode"

    # Requesting the resource and collecting JSON (JavaScript Object Notation) Response
    $request = Invoke-WebRequest -Uri $source | ConvertFrom-Json

    # Iterating over the data fetched via HTTP (HyperText Transfer Protocol) Request and rendering it on PowerShell-Console/CLI (Command Line Interface)
    foreach ($i in $request) {
        Write-Host $i.setup
        Write-Host $i.delivery
    }

}

NOTE: Once the function is created, you can invoke it by its name, “Get-Joke,” and the PowerShell Command Line Interface (CLI) will output a joke fetched over the internet. The created function will be valid for running session only.


More Scripts


More Articles