Hopefully, now you can see that through the power of PowerShell, in combination with the built-in. Future improvements might be to allow the option of having a background graphic and being able to more easily abort the timer in the middle of it running. You’ve now created a fully functional countdown timer that will automatically scale to the size of the screen and allow you to configure how long it runs for and what primary message it displays. This occurs for reasons outside scope of this article ShowDialog ( ) | Out-Null #absorbs cancel message at end. To fix some of the more obvious problems and create something a bit nicer looking, save the following script as Coutndown Timer_2.ps1. It’s not pretty, but it gets the job done. You may have to tweak some of the numbers in the scripts to see the same results.) (Note that the scripts in this article were tested on Dell and Lenovo laptops, an old HP desktop, and an Azure VM but no guarantee that the forms will look the same on all machines. You should see a screen that looks like this. Once you’ve done that, you simply loop through for the length of your $delay and the form will count down. text) property and height and width.įinally, you need to put a new object on this form, a label. Once you have your form object, you can start to assign some properties, in this case, the title (in the. This innocuous statement will mean huge ramifications later. Net managed code, this should look familiar. To anyone who has worked with C# or VB.Net or any other. The interesting part of this script is not the loop it’s the New-Object line. You should not use this script to launch a space shuttle, but for everyday use it’s fine. Note that due to how PowerShell works, the one-second delay in start-sleep has some slop to it.
It starts with the selected $delay value and counts down, sleeping for 1 second at a time until it hits 0.
Building a Countdown Timer with PowerShell - Simple Talk Skip to content