0

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.

DarkLite1
  • 13,637
  • 40
  • 117
  • 214
  • Do you want to change the title of the command prompt on the local computer or the remote computer? – aphoria Jul 29 '15 at 12:20
  • I want to open a a command prompt window that is connected to a remote machine, with the name of the remote machine in the window title. The code above does exactly that, except for the name of the machine in the title. – DarkLite1 Jul 29 '15 at 12:22

2 Answers2

2

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).

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • I would like to mark it with solved, but for one reason or another, it's still not setting the title. It always says `C:\Windows\System32\winrs.exe`. – DarkLite1 Jul 29 '15 at 13:40
  • Now I can see what your command did, it changed indeed the title of the PowerShell window but not he title of the opened CMD window. And that is actually the one I'm after. – DarkLite1 Jul 29 '15 at 13:45
  • Thank you Ansgar. I tried using your last suggestion with spawning but it threw the error: `ComputerName: The term 'ComputerName' is not recognized as the name of cmdlet...` – DarkLite1 Jul 30 '15 at 07:19
  • @DarkLite1 My mistake. You need quotes around `$ComputerName` when setting the window title, otherwise PowerShell would try to execute the expanded variable as a command. – Ansgar Wiechers Jul 30 '15 at 08:17
  • Awesome! Works perfectly now, thank you again. I'm just not skilled in the quotes, double quotes, .. section. – DarkLite1 Jul 30 '15 at 09:28
0

This isn't really a powershell question, since it's more around "How do I manipulate a window started with WinRS?".

That being said, it doesn't seem you can change the title of a window owned by WinRS at all - since even running "Title " manually in the window it creates does nothing. But you could get it to post a comment easily enough by changing your start up command to :

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

Hope that helps.

GodEater
  • 3,445
  • 2
  • 27
  • 30