How to start a console-based process and apply a custom title using Powershell How to start a console-based process and apply a custom title using Powershell powershell powershell

How to start a console-based process and apply a custom title using Powershell


There is a small quirk when changing the text of the process' main window: if you try to change the text straight after you have started the process, it may fail due to one of many possible reasons (e.g. the handle to the control which displays the text does not exist at the time of the function call). So the solution is to use the WaitForInputIdle() method before trying to change the text:

Add-Type -TypeDefinition @"using System;using System.Runtime.InteropServices;public static class Win32Api{    [DllImport("User32.dll", EntryPoint = "SetWindowText")]    public static extern int SetWindowText(IntPtr hWnd, string text);}"@$process = Start-Process -FilePath "notepad.exe" -PassThru$process.WaitForInputIdle()[Win32Api]::SetWindowText($process.MainWindowHandle, "My Custom Text")

Be aware that the application itself can still change the window text after you have made your own change.


I tried this with cmd.exe and it worked well.

Add-Type -Type @"using System;using System.Runtime.InteropServices;namespace WT {   public class Temp {      [DllImport("user32.dll")]      public static extern bool SetWindowText(IntPtr hWnd, string lpString);    }}"@$cmd = Start-Process cmd -PassThru[wt.temp]::SetWindowText($cmd.MainWindowHandle, 'some text')


$host.UI.RawUI.WindowTitle = "new title"

As already been said by George, anything/anyone can set it back (like custom prompt functions for example).