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.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
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.
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.
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!
1 thought on “Adding a PowerShell Profile Calendar”
Comments are closed.