Search code examples
powershellcaption

Changing the caption permanently


In my script, I start a third-party non-GUI application. I'm sort of trying to run this embedded in my script itself, so I will be able to change the icon and the windows caption. I have two restriction:

  1. I have to use & 'application.exe' to start the application. I tested Start-Process -NoNewWindow, but that breaks the functionality of application.exe.
  2. The application.exe needs to be running in my script. I can only change the icon when I compile my script with PS1 to Exe afterwards.

The challenge I'm now facing is related to the first restriction. I need to change the caption a-synchronously. The $host.ui.RawUI.WindowTitle = “New Title” is not working, because application.exe changes the caption right after execution. So I need to change it by using functions like SetWindowText(). This is working in VB.NET, but I'm looking for a way to start this function in parallel with the & 'application.exe'. When I use &, the application is executed and the script waits until it terminates. So I need to do the SetWindowText() in parallel.

Visual Basic/C has a BackgroundWorker functions for such cases. Is something like that also available in PowerShell? Thanks for any help in advance!

Kind regards, Eric


Solution

  • Everybody thank you very much for your help! The solution proved to be a lot easier that I thought. You don't have to keep on renaming the window. You just have to start the cmd window, wait a bit (in the background it's doing something with conhost.exe) and then rename it once. Here's the code I used:

    $titletext = "My New CMD Window Title"
    # Start a thread job to change the window title to $titletext
    $null = Start-ThreadJob { param( $rawUI, $windowTitle )
    Start-Sleep -s 2 #Wait until cmd.exe is started
    if ( $rawUI.WindowTitle -ne $windowTitle ) {
       $rawUI.WindowTitle = $windowTitle
       }
    } -ArgumentList $host.ui.RawUI, $titletext
    & 'c:\windows\system32\cmd.exe'
    

    Kind regards, Eric