### So you can pass the clock file ... param($clockxaml="clock.xaml") ### Import the WPF assemblies Add-Type -Assembly PresentationFramework Add-Type -Assembly PresentationCore Write-Host "Initializing Performance Counters, please have patience" -fore Cyan $script:cpu = new-object System.Diagnostics.PerformanceCounter "Processor", "% Processor Time", "_Total" $script:ram = new-object System.Diagnostics.PerformanceCounter "Memory", "Available KBytes" ## get initial values, because the counters don't work until the second call $null = $script:cpu.NextValue() $null = $script:ram.NextValue() $script:maxram = (gwmi Win32_OperatingSystem).TotalVisibleMemorySize Write-Host "Loading XAML window..." -fore Cyan ## I've removed the xaml to a separate document because it's getting too big for my example :) $clock = [Windows.Markup.XamlReader]::Load( (New-Object System.Xml.XmlNodeReader ( [Xml](Get-Content $clockxaml) ) ) ) ## Create a script block which will update the UI $counter = 0; $updateBlock = { # Update the clock $clock.Resources["Time"] = [DateTime]::Now.ToString("hh:MM.ss") # We only want to update the counters at most once a second # Otherwise their values are invalid and ... # The CPU counter fluctuates from 0 to the real number if( $counter++ -eq 4 ) { $counter = 0 # Update the CPU counter $cu = $cpu.NextValue() $clock.Resources.CpuP = ($cu / 100) $clock.Resources.Cpu = "{0:0.0}%" -f $cu #$clock.FindResource("CpuStory").Begin($clock) # Update the RAM counter $rm = $ram.NextValue() $clock.Resources.RamP = ($rm / $maxram) $clock.Resources.Ram = "{0:0.00}Mb" -f ($rm/1MB) #$clock.FindResource("RamStory").Begin() } } ## Hook up some event handlers $clock.Add_SourceInitialized( { ## Before the window's even displayed ... ## We'll create a timer $timer = new-object System.Windows.Threading.DispatcherTimer ## Which will fire 4 times every second $timer.Interval = [TimeSpan]"0:0:0.25" ## And will invoke the $updateBlock $timer.Add_Tick( $updateBlock ) ## Now start the timer running $timer.Start() if( $timer.IsEnabled ) { Write-Host "Clock is running. Don't forget: RIGHT-CLICK to close it." } else { $clock.Close() Write-Error "Timer didn't start" } } ) $clock.Add_MouseLeftButtonDown( { $_.Handled = $true $clock.DragMove() # WPF Magic! } ) $clock.Add_MouseRightButtonDown( { $_.Handled = $true $timer.Stop() # we'd like to stop that timer now, thanks. $clock.Close() # and close the windows } ) ## Lets go ahead and invoke that update block &$updateBlock ## And then show the window $clock.ShowDialog()