Dynamic PowerShell and SSH remoting tabs for Windows Terminal

Scott HANSELMAN and Thomas MAURER have already written about Windows Terminal tabs opening a remote session to a specific computer.

However, because I am a SysAdmin and have to connect to dozen of different computers every day, I needed to bring this a little further and make it more “dynamic”: every time I open a remoting tab, it should ask for the computer name and the username if necessary. The result looks now like this:

Caveat

Configurations below are based on the fact that:

  • For SSH connections, your remote subsystems are named powershell for Windows PowerShell and pwsh for PowerShell core in your sshd_config file. If this is not the case, you have to update those JSON configurations below with your own subsystem names.
  • For PowerShell 7 connections, you have configured your endpoint with the file provided with the installation package (see here for more details). If not, you have to update the JSON configurations below with the name of your own endpoints.

Details and explanations

What will follow are pieces of configuration you can add to your existing settings. Nothing complicated! Click on the dropdown list of your Windows Terminal console, click Settings and copy/paste the examples below into the Profiles / List section.

{
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "profiles": {
        "list": [
			// THE CODE GOES HERE
			// THE CODE GOES HERE
        ]
    }
}

Note:
If you are new to JSON notation, and you want to add several of the configurations below to your own setting file, just don’t forget to add a comma , between each piece of configuration contained inside curly braces { }.

You can also find the whole code for all cases in a single file here.

In addition, and since it has been removed from the basic configuration, I have also added to this file the blue color scheme when you open a Windows PowerShell remoting tab.
If you want to have all tabs in black again, just remove the "colorScheme": "Campbell Powershell" lines.

SSH to PowerShell Core

{
	"guid": "{4af2f036-8118-4af3-a28b-c2044894a74c}",
	"name": "SSH to PowerShell Core",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory,HelpMessage='@:|@|:|')]$HostName)$Host.UI.RawUI.WindowTitle=$HostName;Enter-PSSession -HostName $HostName -Subsystem 'pwsh'}",
	"icon": "ms-appx:///ProfileIcons/{574e775e-4f2a-5b96-ac1e-a2962a402336}.png",
	"hidden": false
}

Note:
This configuration is assuming that you have installed PowerShell 7 on your client computer.
If not, and if you want to use the OpenSSH client, the command line would be a little different:

{
	"guid": "{4af2f036-8118-4af3-a28b-c2044894a74c}",
	"name": "SSH to PowerShell Core",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory,HelpMessage='@:|@|:|')]$HostName)$Host.UI.RawUI.WindowTitle=$HostName;ssh $HostName -s pwsh}",
	"icon": "ms-appx:///ProfileIcons/{574e775e-4f2a-5b96-ac1e-a2962a402336}.png",
	"hidden": false
}

SSH to PowerShell Core with a key file

{
	"guid": "{e5ee7363-c4d0-4413-a3e9-764d51004769}",
	"name": "SSH to PowerShell Core with KeyFile",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory,HelpMessage='@:|@|:|')]$HostName,[Parameter(Mandatory,HelpMessage='c:\\\\userKey_rsa')]$KeyFilePath)$Host.UI.RawUI.WindowTitle=$HostName;Enter-PSSession -HostName $HostName -KeyFilePath $KeyFilePath -Subsystem 'pwsh'}",
	"icon": "ms-appx:///ProfileIcons/{574e775e-4f2a-5b96-ac1e-a2962a402336}.png",
	"hidden": false
}

Note:
This configuration is assuming that you have installed PowerShell 7 on your client computer.
If not, and if you want to use the OpenSSH client, the command line would be a little different:

{
	"guid": "{e5ee7363-c4d0-4413-a3e9-764d51004769}",
	"name": "SSH to PowerShell Core with KeyFile",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory,HelpMessage='@:|@|:|')]$HostName,[Parameter(Mandatory,HelpMessage='c:\\\\userKey_rsa')]$KeyFilePath)$Host.UI.RawUI.WindowTitle=$HostName;ssh $HostName -i $KeyFilePath -s 'pwsh'}",
	"icon": "ms-appx:///ProfileIcons/{574e775e-4f2a-5b96-ac1e-a2962a402336}.png",
	"hidden": false
}

PSRemoting to PowerShell Core

            {
                "guid": "{5fb07b72-2cc2-457a-9761-f1863f51a202}",
                "name": "PSRemoting to PowerShell Core",
                "commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory)]$ComputerName)$Host.UI.RawUI.WindowTitle=$ComputerName;Enter-PSSession -ComputerName $ComputerName -ConfigurationName PowerShell.7}",
                "icon": "ms-appx:///ProfileIcons/{574e775e-4f2a-5b96-ac1e-a2962a402336}.png",
                "hidden": false
            }

Note:
This configuration is assuming that you have installed PowerShell 7 on your client computer. If not, you can just replace pwsh.exe with powershell.exe.

PSRemoting to PowerShell Core with Credential

{
	"guid": "{56dbd21a-06e1-4ec0-84bc-3c7146370c06}",
	"name": "PSRemoting to PowerShell Core with Credential",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory)]$ComputerName)$Host.UI.RawUI.WindowTitle=$ComputerName;Enter-PSSession -ComputerName $ComputerName -ConfigurationName PowerShell.7 -Credential (Get-Credential)}",
	"icon": "ms-appx:///ProfileIcons/{574e775e-4f2a-5b96-ac1e-a2962a402336}.png",
	"hidden": false
}

Note:
This configuration is assuming that you have installed PowerShell 7 on your client computer. If not, you can just replace pwsh.exe with powershell.exe.

SSH to Windows PowerShell

{
	"guid": "{df187850-acfa-4859-98b3-fc9ef8a18fac}",
	"name": "SSH to Windows PowerShell",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory,HelpMessage='@:|@|:|')]$HostName)$Host.UI.RawUI.WindowTitle=$HostName;Enter-PSSession -HostName $HostName -Subsystem 'powershell'}",
	"icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
	"hidden": false
}

Note:
This configuration is assuming that you have installed PowerShell 7 on your client computer.
If not, and if you want to use the OpenSSH client, the command line would be a little different:

{
	"guid": "{df187850-acfa-4859-98b3-fc9ef8a18fac}",
	"name": "SSH to Windows PowerShell",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory,HelpMessage='@:|@|:|')]$HostName)$Host.UI.RawUI.WindowTitle=$HostName;ssh $HostName -s 'powershell'}",
	"icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
	"hidden": false
}

SSH to Windows PowerShell with KeyFile

{
	"guid": "{a18a4df0-e051-40d1-afe1-d593fa62278f}",
	"name": "SSH to Windows PowerShell with KeyFile",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory,HelpMessage='@:|@|:|')]$HostName,[Parameter(Mandatory,HelpMessage='c:\\\\userKey_rsa')]$KeyFilePath)$Host.UI.RawUI.WindowTitle=$HostName;Enter-PSSession -HostName $HostName -KeyFilePath $KeyFilePath -Subsystem 'powershell'}",
	"icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
	"hidden": false
}

Note:
This configuration is assuming that you have installed PowerShell 7 on your client computer.
If not, and if you want to use the OpenSSH client, the command line would be a little different:

{
	"guid": "{a18a4df0-e051-40d1-afe1-d593fa62278f}",
	"name": "SSH to Windows PowerShell with KeyFile",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory,HelpMessage='@:|@|:|')]$HostName,[Parameter(Mandatory,HelpMessage='c:\\\\userKey_rsa')]$KeyFilePath)$Host.UI.RawUI.WindowTitle=$HostName;ssh $HostName -i $KeyFilePath -s 'powershell'}",
	"icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
	"hidden": false
}

PSRemoting to Windows PowerShell

{
	"guid": "{a8489413-9231-488c-a288-b58d15580476}",
	"name": "PSRemoting to Windows PowerShell",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory)]$ComputerName)$Host.UI.RawUI.WindowTitle=$ComputerName;Enter-PSSession -ComputerName $ComputerName}",
	"icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
	"hidden": false
}

Note:
This configuration is assuming that you have installed PowerShell 7 on your client computer. If not, you can just replace pwsh.exe with powershell.exe.

PSRemoting to Windows PowerShell with Credential

{
	"guid": "{8162f340-84d4-44a2-a47c-3081f47a0ef7}",
	"name": "PSRemoting to Windows PowerShell with Credential",
	"commandline": "pwsh.exe -NoProfile -NoExit -Command &{param([Parameter(Mandatory)]$ComputerName)$Host.UI.RawUI.WindowTitle=$ComputerName;Enter-PSSession -ComputerName $ComputerName -Credential (Get-Credential)}",
	"icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
	"hidden": false
}

Note:
This configuration is assuming that you have installed PowerShell 7 on your client computer. If not, you can just replace pwsh.exe with powershell.exe.

11 thoughts on “Dynamic PowerShell and SSH remoting tabs for Windows Terminal

  1. Great article,
    do you know how to lead the custom profile on the remote session? I tried this but is not working
    Register-PSSessionConfiguration -Name Shell -StartupScript $Profile

    Like

    1. Oh I see…

      PowerShell 5.1 sessions are already present if you configured PSRemoting.
      And if you want to create new custom sessions, this is a little beyond the scope of this post.

      Furthermore, your statement “it does not work” is too broad…
      What does not work? Is the the Session Configuration not created at all, or does is not work like expected? What did you expect?
      Did you restart the WinRM service after creating the configuration?
      Maybe you have already an existing session named Shell…
      Is it the profile which is not applied as expected? You must provide a file accessible to the system, because $profile is basically designed for your account. When you type Get-Item -Path $Profile does the file exist?
      You see, there are many possibilities behind a broad question :)

      Like

    1. SSH is not built into PowerShell 7. Instead, PowerShell leverages the SSH client you have on your local computer and the SSH service on the remote computer.
      On top of this connexion, it calls any subsystem you have configured in your SSH configuration on the remote computer, including Windows PowerShell (5.1 and earlier) or PowerShell Core (6.2 or 7).

      Like

Leave a Reply to Retro Cosmos (@spackie101) Cancel reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s