Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Adding a PowerShell Profile Calendar

Posted on February 1, 2020February 1, 2020

Some of you may be aware of my PSCalendar module which you can install from the PowerShell Gallery. The module contains commands that you can use to display a console-based calendar.  The calendar commands let you specify days to highlight. These might be days with special events or appointments. I typically use the Show-Calendar command as it writes to the host and colorizes output.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Show-Calendar in an Ubuntu session

This command also has a parameter that lets you specify a position in your console. In other words, you can tell PowerShell where to display the calendar. I recently fixed a bug with the command that was producing less than optimal results. Now, I can use my PowerShell prompt function to display a calendar. The first thing you need to do is install at least version 1.10.0 of the PSCalendar module. Make sure it works.

Validating the PSCalendar module

Now you can add a calendar to your console via a prompt function. Here's the default PowerShell prompt function with additional code to display the calendar.

#requires -modules @{ModuleName="PSCalendar";ModuleVersion="1.10.0"}  

Function prompt {

  #define a buffercell fill
  $fill = [system.management.automation.host.buffercell]::new(" ",$host.ui.RawUI.BackgroundColor,$host.ui.RawUI.BackgroundColor,"complete")
 
  #define a rectangle with an upper left corner X distance from the edge
  $left =$host.ui.RawUI.WindowSize.width - 42

  #need to adjust positioning based on buffer size of the console
  #is the cursor beyond the window size, ie have we scrolled down?
    if ($host.UI.RawUI.CursorPosition.Y -gt $host.UI.RawUI.WindowSize.Height) {
        $top = $host.ui.RawUI.CursorPosition.Y - $host.UI.RawUI.WindowSize.Height
    }
    else {
        $top = 0
    }
  #    System.Management.Automation.Host.Rectangle new(int left, int top, int right, int bottom)
  $r = [System.Management.Automation.Host.Rectangle]::new($left, 0, $host.ui.rawui.windowsize.width,$top+10)

  #clear the area for the calendar display
  $host.ui.rawui.SetBufferContents($r,$fill)

  #show the calendar in the upper right corner of the console
  $pos = [system.management.automation.host.coordinates]::new($left,0)
  Show-Calendar -Position $pos

  "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";

# .Link
# https://go.microsoft.com/fwlink/?LinkID=225750
# .ExternalHelp System.Management.Automation.dll-help.xml

}

I save this as a separate PowerShell script file and dot source it in my profile script. This function isn't designed for the PowerShell ISE and might not work well in the VS Code PowerShell terminals. I can't even guarantee it will work in your PowerShell consoles. Take this code as a proof-of-concept and not production-ready.

The function displays the calendar for the current month in the upper right corner of your console. The prompt function clears that area and re-writes the calendar. This will work best if your console is at least 120 characters wide. Otherwise, you'll need to play with the code.

The prompt function runs every time you press Enter.

PowerShell Profile Calendar

I'm running in the Windows Terminal but this function should work in a traditional PowerShell console where you might have a large scroll buffer.

The calendar will highlight dates in green. I set some values using $PSDefaultParameterValues in my profile.

$PSDefaultParameterValues["*-*calendar:Highlightdate"]="1/18","1/8","1/16"

You can figure out where to get the dates that matter to you and populate the hashtable. I hope you'll let me know what you think about all of this. And now that I think I have a handle on console buffers and rectangles, I might revisit some older prompt functions. Hopefully, I'll have some more fun stuff for you to play with. In the meantime, don't miss that next event. Enjoy!


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Adding a PowerShell Profile Calendar”

  1. Pingback: Showing a Calendar in your Powershell Prompt – Curated SQL

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d