Author Posts

August 30, 2017 at 8:46 pm

Hi i've got a ps1 that monitor a MDT server and on that ps1 with a xaml i'm using a button to refresh the button is doing

	$wshell = New-Object -ComObject Wscript.Shell
	$wshell.Popup("Rafraichissement des données effectué.",0,"Attention",0x0)

Instead of using that button I would like to "Auto" refresh every 1-2 minutes.... could you help me on that?


August 30, 2017 at 11:20 pm

If you want something to occur, non-stop, every two minutes, you could do something like:

  $wshell = New-Object -ComObject Wscript.Shell
  $wshell.Popup("Rafraichissement des données effectué.",0,"Attention",0x0)
  Start-Sleep -Seconds 120

August 30, 2017 at 11:50 pm

Thanks..But I realized that when the Do/While is running it keep the "focus" so no other execution is possible. So only @ every 120s, other action is able to run :/

August 31, 2017 at 12:22 am

By the fact that you're trying to auto run code that is currently manually activated by a button, you're running a gui application. Not sure if the below is relevant/will help (It's from a "Tip of the Day" e-mail from, but it's about having the powershell title bar updated via a background thread so that you're able to still interact with the powershell console while the background thread keeps updating the title bar. Maybe someone else has a better idea/more details.

Adding Live Clock to PowerShell Title Bar (Part 1)
All Versions
To continuously update the PowerShell title bar, and for example display the current date and time, you need a background thread that takes care of this. Without a background thread, your PowerShell would be busy all the time updating the title bar, thus unusable..
Here is a simple piece of code you can run to display a live clock in your title bar:
$code =
    # submit the host process RawUI interface and the execution context
        # compose the time and date display
        $time = Get-Date -Format 'HH:mm:ss dddd MMMM d'
        # compose the title bar text
        $title = "Current Time: $time"
        # output the information to the title bar of the host process
        $RawUI.WindowTitle = $title
        # wait a half second
        Start-Sleep -Milliseconds 500
    } while ($true)
$ps = [PowerShell]::Create()
$null = $ps.AddScript($code).AddArgument($host.UI.RawUI)
$handle = $ps.BeginInvoke()
The crucial thing here is to pass over the $host.UI.RawUI object from your foreground PowerShell to your background thread code. Only then can your background thread access the title bar owned by your foreground PowerShell.

September 1, 2017 at 2:02 pm

Thanks i've changed a bit the code and went with a timer with a stop button...

$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 60000 # milliseconds 1min
Function UpdateUi()
        $wshell = New-Object -ComObject Wscript.Shell
	$wshell.Popup("Start refresh.",0,"Attention",0x0)
Function StartTimer()
Function StopTimer()
	$wshell = New-Object -ComObject Wscript.Shell
	$wshell.Popup("Stop refresh.",0,"Attention",0x0)

Working great so far, thanks