Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

PowerShell Remoting Profiles with Windows Terminal

Posted on February 10, 2020February 13, 2020

I have jumped in the deep end and fully committed to Windows Terminal as my default PowerShell environment. I love having one interface with tabs for different terminal profiles. Windows Terminal makes it easy for me to have tabs open to PowerShell 7, Windows PowerShell, an Ubuntu instance or even a PowerShell session with no profile. The last piece I needed was an easy way to launch a Windows Terminal profile and connect to a remote computer. Want to see how I did it?

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

First I created a new profile in my Windows Terminal settings. I'm going to assume you know how to do that or can figure it out. If you copy and paste an existing profile, don't forget to set a new GUID.  For my first remote profile, I wanted to create a PowerShell remoting session to my test domain controller DOM1. Here's my Windows Terminal profile for that part.

{
        "acrylicOpacity": 0.5,
        "background": "#0a52e2",
        "closeOnExit": true,
        "colorScheme": "Jeff",
        "commandline": "powershell.exe -nologo -noprofile -noexit -file c:\\scripts\\wtpsremote.ps1 dom1",
        "cursorColor": "#FFFFFF",
        "cursorShape": "underscore",
        "fontFace": "Cascadia Code",
        "fontSize": 14,
        "guid": "{b89c512a-cd2b-4fb4-a6d2-3366cee85970}",
        "historySize": 9001,
        "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
        "name": "DOM1",
        "padding": "0, 0, 0, 0",
        "snapOnInput": true,
        "startingDirectory": "%USERPROFILE%",
        "tabTitle": "PSRemote: DOM1.Company.Pri",
        "useAcrylic": false
   },

The magic bits are centered on the commandline setting. In my profile I'm telling Windows Terminal to start Windows PowerShell with no logo, no profile and not to exit. I'm then providing a file and an argument for that script. I initially fussed with -Command but eventually found it easier to simply call a script file. The wtpsremote.ps1 file, creates the remoting session.

#requires -version 5.1

#wtpsremote.ps1

[cmdletbinding()]
Param(
    [Parameter(Position = 0, Mandatory, HelpMessage = "Specify remote computer name")]
    [ValidateNotNullOrEmpty()]
    [string]$Computername,
    [pscredential]$Credential,
    [Parameter( HelpMessage = "A profile script to run in the remote session")]
    [ValidateScript( {Test-Path $_})]
    [string]$RemoteProfile = "C:\scripts\RemoteProfile.ps1",
    [Parameter(HelpMessage = "Specify a remote endpoint other than the default Microsoft.powershell")]
    [string]$ConfigurationName,
    [int32]$Port,
    [switch]$UseSSL
)

#remove this this parameter if specified since it can't be used to create a new PSSession
[void]($PSBoundParameters.Remove("RemoteProfile"))

#Create a new remoting session splatting the parameters passed to this script
$remote = New-PSSession @PSBoundParameters

#run the remote profile script
if ($RemoteProfile) {
    Invoke-Command -Session $remote -FilePath $RemoteProfile -HideComputerName |
        Select-Object -property * -ExcludeProperty runspaceID |
        Format-Table -AutoSize
}

#open the pssession
Enter-PSSession -session $remote

$msg = @"
 Reminder:                                                                        
 Remove the `$remote pssession after you exit and before you close the profile tab.
                                                                                  
 PS C:\> `$remote | Remove-PSSession                                               
 PS C:\> exit                                                                     
"@

#you may need to adjust colors based on your profile settings
Write-host $msg -ForegroundColor Magenta -BackgroundColor Gray

The script takes a number of parameters related to creating a new PSSession, which it does and then connects to it. And the script goes a bit further.

Normally, when you launch a PowerShell remoting session, no profile scripts are run. But this script lets you specify local file that can be run remotely, in essence as a profile script. This is the default I use.

#requires -version 5.1

#this is a remote profile script

#create a new prompt function for the remote session
#how long has this session been running?
$initiated = Get-Date

$remotePrompt = {
    #display the session runtime without the milliseconds
    $ts = ((Get-Date) - $initiated).ToString().split(".")[0]
    Write-Host ">$ts< " -ForegroundColor yellow -nonewline
    "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

Set-Item -Path function:\prompt -Value $remotePrompt -Force

Set-Location C:\
Clear-Host

#display some information about the remote computer
Get-CimInstance -ClassName Win32_OperatingSystem |
Select-Object @{Name="Computer";Expression={$_.CSName}},
@{Name="OS";Expression = {$_.Caption}},
@{Name="PSVersion";Expression = {$PSVersionTable.PSVersion}},
@{Name="User";Expression = {Whoami.exe}}

The profile script defines a prompt for the remote session that will show you how long you have been connected. It also displays a summary about the remote computer. You can add whatever else you might need such as defining variables, aliases or importing modules.

Going back to wtpsremote.ps`, the last step is to display a reminder.  If you simply close the profile tab, the remoting session will remain in a disconnected state until the remote computer cleans it up or is rebooted. To be a good network citizen, when you are finished type 'exit' to leave the remote session. This will dump you back in the terminal profile running Windows PowerShell. You should remove the pssession and then you can exit or close the profile tab.

Here's what I get when I open this Windows Terminal profile.

A Windows Terminal PowerShell Remote Tab

As I use the terminal, the prompt updates to show me how long I have been connected. When finished, I can exit.

A remote session prompt

It is at this point that I would remove the pssession and exit the tab.

My connection script also lets you specify alternate credentials and even an alternate endpoint. Here's a profile for SRV1 that connects with specific credentials and connects to the PowerShell 7 endpoint.

{
    "acrylicOpacity": 0.5,
    "background": "#808285",
    "closeOnExit": true,
    "colorScheme": "Jeff",
    "commandline": "powershell.exe -nologo -noprofile -noexit -file c:\\scripts\\wtpsremote.ps1 -computername srv1 -credential company\\administrator -configurationname PowerShell.7.0.0-rc.2",
    "cursorColor": "#FFFFFF",
    "cursorShape": "underscore",
    "fontFace": "Cascadia Code",
    "fontSize": 14,
    "guid": "{173fd06c-883a-404b-97bd-9f955a0e85f0}",
    "historySize": 9001,
    "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
    "name": "SRV1",
    "padding": "0, 0, 0, 0",
    "snapOnInput": true,
    "startingDirectory": "%USERPROFILE%",
    "tabTitle": "PSRemote: SRV1.Company.Pri",
    "useAcrylic": false
},

A Windows Terminal PowerShell Remote Session to an alternate endpoint

I am finding these remote tabs very useful and hope you will too. Enjoy!

Update

I updated my techniques and code for creating remote profiles in Windows Terminal to now support cross-platform. Read about it here.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “PowerShell Remoting Profiles with Windows Terminal”

  1. Pingback: Cross Platform PowerShell Profiles with Windows Terminal • The Lonely Administrator
  2. Pingback: ICYMI: PowerShell Week of 14-February-2020 | PowerShell.org

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d