Show-Calendar, cal for PowerShell

This is a PowerShell implementation of the cal command found in Unix/Linux. Its task is quite simply to output a calendar view of the choosen month(s).

UPDATED 22.12.2014

I have made an update to the function. It now supports right-aligning the days in the output. In fact, Right alignment are now the default value. (Thanks Kirk!). I have also added support for using abbreviated names for days.

The biggest change however are the fact that the function is now culture-aware, and the default culture is the current culture. It supports specifying a specific culture, or you can use the Invariant Culture.

I have also decided to do something I thought I’d never do, I have used Format-Table for the output. This makes using Kirks FormatPx module even more desirable, as this is now the only way to get a proper PowerShell object out of this function.

The reason I made this change was that without FormatPx, the default output (list view) was not well suited for a calendar, and having to use Format-Table after the command each time, made it cumbersome to use.

A small note regarding the culture awareness: Since the days and the months are read from a CultureInfo object, they will display in the language of that culture. Since I use Week, Month and Year also as property names, and since these will still be visible in English, I have added a translation hash table to the script. I have added translations for a handful of different languages already, but if yours is missing, and you would like it added, please let me know, and I’ll add it to the code!


Original post text

You can choose the starting month and year, and how many months to show, as well as parameters to mark day(s) as well as a specific date. The current date will also be marked in the calendar.

Since this is PowerShell the days are of course datetime objects and not just text, but because of this the default output formatting is not ideal for a calendar view. I have built-in support for the excellent FormatPx module by Kirk Munro, so if you are using this module the output will look like this:

cal001

If you don’t have FormatPx and don’t want to use it, I suggest using Format-Table to get the calendar view somewhat readable on the screen. The following image shows how that will look:

cal002

But seriously, if you haven’t checked out FormatPx yet, head on over to Kirks github project and give it a try!

And now for some code:

2 comments

    1. Thanks Jeffrey. Interesting to see how differently we have approached the same goal. The good thing about your code is that since you are not actually outputting the words ‘Week’, ‘Month’ and ‘Year’ as I do, you are not getting into the trouble of these being in a different language than the name of the days. Speaking of culture.. it was a lot more work than I thought it would be to get it totally culture/language aware, but I’m quite satisfied with the result. Hopefully there are not too many nasty bugs in there! 🙂

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s