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.
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 "
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.
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!
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.
Thank you Jeffery!