Weekend Scripter: Build a Clock Widget by Using PowerShell and WPF

Summary: Boe Prox shows how to use Windows PowerShell to build a clock widget.

Honorary Scripting Guy, Boe Prox, here today filling in for my good friend, The Scripting Guy. Here's a bit about me:

Boe Prox is a Microsoft MVP in Windows PowerShell and a senior Windows system administrator. He has worked in the IT field since 2003, and he supports a variety of different platforms. He is a contributing author in PowerShell Deep Dives with chapters about WSUS and TCP communication. He is a moderator on the Hey, Scripting Guy! forum, and he has been a judge for the Scripting Games since 2010. He recently presented talks on the topic of WSUS and PowerShell at the Mississippi PowerShell User Group. He is an Honorary Scripting Guy, and he has submitted a number of posts as a guest blogger, which discuss a variety of topics. To read more, see these Hey, Scripting Guy! Blog posts.

Boe’s blog: Learn Powershell |Achieve More
CodePlex projects: PoshWSUSPoshPAIGPoshChat, and PoshEventUI

I wanted to take the time today to show you how to build a clock widget by using Windows PowerShell. Although we use Windows PowerShell daily to perform great feats of strength in our environments and to provide amazing reports to our managers (among other things), sometimes it is just nice to kick back and make something fun. In this case, the fun thing that I will show you how to build is a clock widget!

Note  You can download the full script from the Script Center Repository: Clock Widget.

Building a GUI can be a little time tedious because it usually means writing code to build out the front-end GUI and then more code to connect to controls, handle events, and perform various tasks. But fear not! All it takes is a little bit of time and you can knock this widget out! Plus, there are a variety of tools that can help make the process much simpler depending on your choice of GUI (such as Windows Presentation Foundation (WPF) and WinForms).

With that, let’s get started building this out! The first thing that I want to knock out is how the GUI will look and then work towards building out the back-end script. My preferred choice for GUIs is using WPF, and I usually take the approach of writing out the XAML. It’s a little more time consuming, and it requires some extra testing to make sure that everything lines up, but you can also take advantage of Visual Studio by creating the WPF GUI and then copying the XAML code to the Windows PowerShell ISE to finish the script. Here is the XAML code that will make up the GUI front end:

$xaml = @"




        WindowStyle = 'None' WindowStartupLocation = 'CenterScreen' SizeToContent = 'WidthAndHeight'

        ShowInTaskbar = "False" ResizeMode = "NoResize" Title = "Weather" AllowsTransparency = "True"

        Background = "Transparent" Opacity = "1" Topmost = "True">

    <Grid x:Name = "Grid" Background = "Transparent">

        <TextBlock x:Name = "time_txtbox" FontSize = "72" Foreground = "$($Clockhash.TimeColor)"

        VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,-26,0,0">


                    <DropShadowEffect Color = "Black" ShadowDepth = "1" BlurRadius = "5" />



        <TextBlock x:Name = "ampm_txtbx" FontSize=  "20" Foreground = "$($Clockhash.TimeColor)"

        Margin  = "133,0,0,0" HorizontalAlignment="Left">


                    <DropShadowEffect Color = "Black" ShadowDepth = "1" BlurRadius = "2" />



        <TextBlock x:Name = "day_n_txtbx" FontSize=  "38" Foreground = "$($Clockhash.DateColor)"

        Margin="5,42,0,0" HorizontalAlignment="Left">


                    <DropShadowEffect Color = "Black" ShadowDepth = "1" BlurRadius = "2" />



        <TextBlock x:Name = "month_txtbx" FontSize=  "20" Foreground = "$($Clockhash.DateColor)"

        Margin="54,48,0,0" HorizontalAlignment="Left">


                    <DropShadowEffect Color = "Black" ShadowDepth = "1" BlurRadius = "2" />



        <TextBlock x:Name = "day_txtbx" FontSize=  "15" Foreground = "$($Clockhash.DateColor)"

        Margin="54,68,0,0" HorizontalAlignment="Left">


                    <DropShadowEffect Color = "Black" ShadowDepth = "1" BlurRadius = "2" />



        <TextBlock x:Name = "year_txtbx" FontSize=  "38" Foreground = "$($Clockhash.DateColor)"

        Margin="0,42,0,0" HorizontalAlignment="Left">


                    <DropShadowEffect Color = "Black" ShadowDepth = "1" BlurRadius = "2" />






You may have noticed that I cast this as XML. This will be important later when I configure the initial window control.

I will now set up a runspace, which will have everything running in it (even the XAML code that I already showed), and a couple of synchronized collections that will handle the sharing of variables between my runspaces. Because I also want to provide the option of specifying various colors for the Time and the Date, I will have a couple of parameters to handle that.

Param (


    [string]$TimeColor = "White",


    [string]$DateColor = "White"


$Clockhash = [hashtable]::Synchronized(@{})

$Runspacehash = [hashtable]::Synchronized(@{})

$Runspacehash.host = $Host

$Clockhash.TimeColor = $TimeColor

$Clockhash.DateColor = $DateColor

$Runspacehash.runspace = [RunspaceFactory]::CreateRunspace()

$Runspacehash.runspace.ApartmentState = “STA”

$Runspacehash.runspace.ThreadOptions = “ReuseThread”


$Runspacehash.psCmd = {Add-Type -AssemblyName PresentationCore,






$Runspacehash.psCmd.Runspace = $Runspacehash.runspace

$Runspacehash.Handle = $Runspacehash.psCmd.AddScript({…})

There is a lot happening here, but what I am doing is creating a runspace by using the [RunspaceFactory] static method, which will be used to run this clock widget outside of the Windows PowerShell console. The console still needs to be running, otherwise it will close the widget. This means that I can still use the console without it being tied down to handle the GUI.

My hash tables are added into the runspace via the SetVariable method, and I add the required assemblies to allow Windows PowerShell to create the WPF GUI. The next thing that I will want to do is have a separate script block that will be referenced in a Timer event to run during each tick:

$Script:update = {

    $day,$Month,$Day_n,$Year,$Time,$AMPM = (Get-Date).DateTime -split "\s" -replace ","

    $Day_n = $Day_n.PadLeft(2,"0")

    $Time = $Time -replace '(.*):.*','$1'

    $Clockhash.time_txtbox.text = $Time

    $Clockhash.day_txtbx.Text = $day

    $Clockhash.ampm_txtbx.text = $AMPM

    $Clockhash.day_n_txtbx.text = $Day_n

    $Clockhash.month_txtbx.text = $Month

    $Clockhash.year_txtbx.text = $year  


Up next is connecting to the created XAML, creating the window control, and using that to make the connection to the other controls so I can begin writing event handlers for the clock:

$reader=(New-Object System.Xml.XmlNodeReader $xaml)

$Clockhash.Window=[Windows.Markup.XamlReader]::Load( $reader )

$Clockhash.time_txtbox = $Clockhash.window.FindName("time_txtbox")

$Clockhash.ampm_txtbx = $Clockhash.Window.FindName("ampm_txtbx")

$Clockhash.day_n_txtbx = $Clockhash.Window.FindName("day_n_txtbx")

$Clockhash.month_txtbx = $Clockhash.Window.FindName("month_txtbx")

$Clockhash.year_txtbx = $Clockhash.Window.FindName("year_txtbx")

$Clockhash.day_txtbx = $Clockhash.Window.FindName("day_txtbx")

Note  The names are case sensitive when you use the FindName() method. So take note of what you name the controls in the XAML code.

With that out of the way, we should begin looking at creating the event handlers for the clock. I want to first create a Timer object that will run in the UI thread and update the date and time with each tick. I want to do this as the window is being initialized so it starts updating right away:

#Timer Event


    #Create Timer object

    Write-Verbose "Creating timer object"

    $Script:timer = new-object System.Windows.Threading.DispatcherTimer

    #Fire off every 1 second

    $timer.Interval = [TimeSpan]"0:0:1.00"

    #Add event per tick



    [Windows.Input.InputEventHandler]{ $Clockhash.Window.UpdateLayout() 


    #Start timer

    Write-Verbose "Starting Timer"


    If (-NOT $timer.IsEnabled) {




You can see here that I invoke the $Update script block to update the date and time of the clock. This is a clock, so I want to have it update every second to ensure accuracy. I want to make sure that I clean up after myself when this widget is closed:





The next three event handlers satisfies my itch to make sure that everything lines up right away with the clock.:


    [int]$clockhash.length = [math]::Round(($Clockhash.day_txtbx.ActualWidth,$Clockhash.month_txtbx.ActualWidth |

        Sort -Descending)[0])

    [int]$Adjustment = $clockhash.length + 52 + 10 #Hard coded margin plus white space

    $YearMargin = $Clockhash.year_txtbx.Margin

    $Clockhash.year_txtbx.Margin = ("{0},{1},{2},{3}" -f ($Adjustment),




    [int]$clockhash.length = [math]::Round(($Clockhash.day_txtbx.ActualWidth,$Clockhash.month_txtbx.ActualWidth |

        Sort -Descending)[0])

    [int]$Adjustment = $clockhash.length + 52 + 10 #Hard coded margin plus white space

    $YearMargin = $Clockhash.year_txtbx.Margin

    $Clockhash.year_txtbx.Margin = ("{0},{1},{2},{3}" -f ($Adjustment),




    If ($Clockhash.time_txtbox.text.length -eq 4) {       

        $Clockhash.ampm_txtbx.Margin  = "133,0,86,0"

    } Else {

        $Clockhash.ampm_txtbx.Margin  = "172,0,48,0"



Had I thrown this into my $Update script block, the initial second or so would have the clock looking a little awkward by listing the year and having the A.M./P.M. text boxes crossing over the rest of the controls. Instead, this will make sure that the clock looks great at startup!

Because this is a widget, it will not have a button to close it or any visible window edges to grab to move around. This is where the following two events come into play:







The $This variable references the current object—in this case, the window control. By using the left button, I can move the widget around and I can close the widget by right-clicking it.

The final part is to run the script block to update the GUI and then show the widget. This is done by using ShowDialog() on the window control. That will actually happen when we use the BeginInvoke() method to kick off the runspace:


$Clockhash.Window.ShowDialog() | Out-Null


In the end, we get something that looks like this:

Image of widget

That black and white color just isn’t cutting it for me. I want something a little different for the date and time. I will create a clock widget that is more colorful:

.\ClockWidget.ps1 –TimeColor DarkRed –DateColor Gold

Image of widget

I can even use hex values for the colors:

.\ClockWidget.ps1 –TimeColor "#669999" –DateColor "#334C4C"

Image of widget

And that is all there is to building a clock widget by using Windows PowerShell and WPF.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow.

Boe Prox, Honorary Scripting Guy 

Comments (12)

  1. Boe Prox says:

    You can do something like this in your batch file:

    powershell.exe -noprofile -STA -WindowStyle Hidden -NonInteractive -NoLogo -Command "&{ }"

  2. Boe Prox says:

    My fault. I forgot to add a -NoExit in there to ensure that the powershell process remains after spawning the new thread for the clock. I tested with this addition and it worked for me.

    powershell.exe -noprofile -STA -WindowStyle Hidden -NonInteractive -NoLogo -NoExit -Command "&{.clockwidget.ps1}"

  3. Anonymous says:

    Great post. A colleague of mine who switched from Win7 to Win8, missed the widgets and really liked this.

  4. Boe Prox says:

    It would appear that everything inside of "&{}" was swallowed up. What should have been in the {}was ‘PathToScript’ ‘OptionalParameters’

  5. JV says:

    Now you are just showing off 😉

    Good stuff. This may turn out to be very useful.

  6. OldFart says:

    Anything involving building GUIs in PowerShell reminds me of the old saying, “When your only tool is a hammer, everything looks like a nail.”

  7. dom says:


    How can’i launch this script with BAT file ?

    Thank !

  8. dom says:


    Thanks for your reply.
    Unfortunately, this doesn’t work. the widget doesn’t show.

  9. dom says:

    it work!

    Have a good weekend

  10. GJA says:

    Hi Guys, I know this is an old post, but how do I let the user choose the color of the clock with some user interaction?


  11. no words to say thanks. You guys from 2 different world evolved a 3rd party never imagined before. Excellent!!

Skip to main content