powershellcmdwinrs

Set the window title of a CMD/WINRS prompt


I'm creating some PowerShell code to open a command prompt on a remote machine. This works fine but I can't seem to find a way to set the title of that window, so you can see that you're connected to the remote client.

$ComputerName = 'HostName'

Start-Process 'winrs' -ArgumentList "/r:$ComputerName.domain.net cmd /noprofile /noecho"

I've tried by adding the well known TITLE $ComputerName at the end, but that doesn't change anything. If setting the title isn't possible, it would be nice to have a comment in the window to see the host name you are connected to.


Solution

  • The title of a PowerShell window can be changed via the $Host variable:

    $ComputerName = 'HostName'
    
    $Host.UI.RawUI.WindowTitle = $ComputerName
    
    Start-Process 'winrs' -ArgumentList "/r:$ComputerName.domain.net cmd /noprofile /noecho"

    Edit: If spawning a new window is not a hard requirement you could change the title of the PowerShell window as described above and run winrs inline (using the call operator &):

    $ComputerName = 'HostName'
    $Host.UI.RawUI.WindowTitle = $ComputerName
    & winrs /r:$ComputerName.domain.net cmd /noprofile /noecho

    Otherwise you could spawn a new PowerShell window and run the above in that window:

    $ComputerName = 'HostName'
    Start-Process 'powershell.exe' -ArgumentList "&{`$Host.UI.RawUI.WindowTitle = '$ComputerName'; & winrs /r:$ComputerName.domain.net cmd /noprofile /noecho}"

    Note that in this case you must escape the $ in $Host to prevent premature expansion of that variable (you want it expanded in the child process, not in the parent).