Calendars + PowerShell, Wolfram|Alpha & REST

by Doug Finke on October 13, 2013

Display A Calendar at the PowerShell Command Line

cal

I always have a PowerShell console open. Sometimes I just want a simple output of a calendar. In Unix, you can type cal and it prints a row with the days of the week at the top and the days of the month, printed in the correct columns.

PowerShell, Wolfram|Alpha & REST

While this approach takes a dependency on a web query. It’s interesting what can be done with the REST API that Wolfram|Alpha lets you query its computational engine.

The script below takes a month and optionally a year, appending  the string calendar before executing the query.

The results are returned in JSON and PowerShell’s Invoke-RestMethod handles converting that. Using dot notation gets you to the plaintext property containing the following string.

Su | Mo | Tu | We | Th | Fr | Sa
|  |  |  | 1 | 2 | 3
4 | 5 | 6 | 7 | 8 | 9 | 10
11 | 12 | 13 | 14 | 15 | 16 | 17
18 | 19 | 20 | 21 | 22 | 23 | 24
25 | 26 | 27 | 28 | 29 | 30 | 31

Using the PowerShell ConvertFrom-Csv cmdlet and setting the –Delimiter parameter to ‘|’, it’s converted to PowerShell PSObjects. DONE!

function cal {            
    param(            
        $month = (            
          Write-Output Jan Feb Mar `
               Apr May Jun `
               Jul Aug Sep `
               Oct Nov Dec            
        )[(Get-Date).Month-1],             
        $year            
    )            

    $q     = "$month $year calendar"            

    $appId =  "WT6VQU-9AR42L3YLE"            
    $url   =  "http://api.wolframalpha.com/v2/query"            
    $url   += "?input=$q&appid=$appId"            

    "`r`n$month $year"            
    (Invoke-RestMethod $url).queryresult.pod[1].subpod.plaintext |            
        ConvertFrom-Csv -Delimiter '|' |            
        Format-Table -AutoSize            
}

Note: I’ve left my appId in so it is easy to try it out. I’ll be changing it soon so Wolfram|Alpha doesn’t complain. You can easily request an appid to keep things running.

{ 2 comments… read them below or add one }

Lee Holmes 10.14.13 at 2:13 pm
Doug Finke 10.16.13 at 9:53 am

Thanks for the link Lee.

I had planned on searching for and/or writing a calendar.ps1. I randomly hit on Wolfram showing calendars and decided to hit their REST API to see how they returned it.

I was surprised and that ConvertFrom-Csv could be used.

Doug

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>