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

A WPF Countdown Timer

Posted on October 19, 2022October 19, 2022

Last year I released a PowerShell module called PSClock. The module contains a command to create a transparent WPF form displaying a clock. Shortly after, someone posted a request for a countdown timer. Not an unreasonable request and one I finally got around to implementing. However, I already had a module with a number of timer and countdown-related commands called PSTimers I had been thinking of a WPF-based timer, so this solved two problems.

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!

I've published v1.0.1 of the PSTimers module to the PowerShell Gallery with a new command, Start-PSCountdownTimer. This command is based on Start-PSClock and will launch a WPF-based countdown timer. The timer's appearance and behavior is customizable via parameters.

Start-PSCountdownTimer -Seconds 300 -FontSize 48 -Color Green -Message "The PowerShell magic begins in "
pscountdown timer

At 50 seconds, the font color will change to Yellow, and at 30 seconds, to Red. These settings can also be set with parameters.

The timer uses a synchronized hashtable.

PS C:\> $pscountDownClock

Name                           Value
----                           -----
FontStyle                      Normal
Warning                        30
StartingPosition
AlertColor                     Yellow
WarningColor                   Red
FontWeight                     Normal
Message                        The PowerShell magic begins in
OnTop                          False
Seconds                        300
FontSize                       48
CurrentPosition                {276, 964}
Running                        True
Runspace                       System.Management.Automation.Runspaces.LocalRunspace
Alert                          50
Color                          Green
Started                        10/19/2022 12:35:00 PM
FontFamily                     Segoi UI

After the clock is running, you can modify the hashtable to adjust the WPF form.

PS C:\> $PScountdownclock.fontsize = 60
PS C:\> $PScountdownclock.color = "chartreuse"

If you want to stop the clock early you can run Stop-PSCountdownTimer or set $PSCountdownClock.Running to $False.

The countdown timer runs in a separate runspace, so your prompt isn't blocked. But this also means that it can't easily interact with the PowerShell session that launched it. If you would like something to happen at the end of the timer, I suggest writing an automation script around the command.

<#
A proof-of-concept script that adds an action at the end of the PSCountdownTimer
#>
Start-PSCountdownTimer -seconds 60 -message "The PowerShell magic begins in " -FontSize 64 -Color SpringGreen
Do {
    Start-Sleep -Seconds 1
} While ($PScountdownclock.Running)
Clear-Host
Write-Host "Are you ready for some PowerShell?" -ForegroundColor magenta -BackgroundColor gray

Add-Type -AssemblyName PresentationCore
$filename = "c:\work\01-Start.mp3"
#the media player launches with no UI. Use the object's methods to control it.
$global:mediaplayer = New-Object system.windows.media.mediaplayer
$global:mediaPlayer.Open($filename)
$global:mediaplayer.Play()

# mediaplayer.stop()
# $mediaplayer.close()

I think I'll start using this in my classes and conference presentations.

The module should work cross-platform, but the WPF-based commands require Windows. If you try it out, I'd love to know what you think.


Behind the PowerShell Pipeline

Share this:

  • Share on X (Opens in new window) X
  • Share on Facebook (Opens in new window) Facebook
  • Share on Mastodon (Opens in new window) Mastodon
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
  • Print (Opens in new window) Print
  • Email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

5 thoughts on “A WPF Countdown Timer”

  1. i5513 says:
    October 19, 2022 at 1:53 pm

    Hello,

    Not related to this post, but about powershell. I have read your lastest articles

    I would like to have a progress bar which tell you which function are being executed

    Something like a function decorator :

    Function xxx {
    Param ( $a, $b)
    Write-progress -parentid 1 “Running Xxxx $a $b”
    A
    B
    C
    …
    Write-progress -parentid 1 “Running Xxxx $a $b” -Completed
    }

    With 2 depth levels maybe ok.

    Think in a scriptstacktrace progress bar

    Is it possible with ast or something in powershell

    Thanks!

    1. Jeffery Hicks says:
      October 19, 2022 at 2:11 pm

      You can add Write-Progress to any of your commands, of course. But I can’t think of a way off the top of my head how you could have the shell detect what command you are running and then overlay a progress bar. And even if you could launch the progress bar, it would be impossible to increment it since you’d likely have no idea how long the command is going to run. I think you’d have to write your own PowerShell host to implement this feature.

  2. Pingback: A WPF Countdown Timer - The Lonely Administrator - Syndicated Blogs - IDERA Community
  3. Pingback: A WPF-Based Countdown Timer – Curated SQL
  4. i5513 says:
    October 27, 2022 at 6:08 pm

    Thank you Jeffery!

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

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