powershelltitlebarcustom-titlebar

Change title of another window through PowerShell


I have a VB application, which starts several instances of a third party non-GUI application. To keep track of these multiple instances, I update their title, using the SetWindowText() function. This application however has the nasty habit of continuously updating the title, so each SetWindowText works only temporary. As soon as you click anywhere in the screen, the tile is changed back. I found a way to update the title through PowerShell, using the following code:

$titletext = "My Title"
# Start a thread job to change the window title to $titletext
$null = Start-ThreadJob { param( $rawUI, $windowTitle )
Start-Sleep -s 2
if ( $rawUI.WindowTitle -ne $windowTitle ) {
    $rawUI.WindowTitle = $windowTitle
}
}-ArgumentList $host.ui.RawUI, $titletext
& 'c:\Program Files\Application\Application.exe' '-id=userid -pass=password'

This works perfectly and the title change is permanent, so exactly what I want. The only problem is that everything is being logged in the Windows PowerShell log, including the parameters -id= and -pass=. A solution would be if I can start application.exe through my VB application and do the rename through a PowerShell script, but I don't know if that is possible through a ThreadJob. Is it possible to start a ThreadJob and rename another window, maybe through it's handle?


Solution


  • For example, assuming that your VB.NET application has created environment variable MYPWD containing the password, before launching the PowerShell script:

    $titletext = "My Title"
    
    # Start a thread job to change the window title to $titletext
    $null = Start-ThreadJob { param( $rawUI, $windowTitle )
      Start-Sleep -s 2
      if ( $rawUI.WindowTitle -ne $windowTitle ) {
        $rawUI.WindowTitle = $windowTitle
      }
    } -ArgumentList $host.ui.RawUI, $titletext
    
    # Note: 
    # * Assumes that your VB.NET application has set env. var. "MYPWD".
    # * The arguments must be passed *individually*, not inside a single string.
    & 'c:\Program Files\Application\Application.exe' -id=userid "-pass=$env:MYPWD"