I'm currently using Windows Terminal with PowerShell, and I wanted to find a solution for remembering the different terminals created between sessions, as the internal Visual Studio command-line tool doesn't provide this functionality (If it does, I'm all ears :))
To achieve this, I attempted to build a script in Windows Terminal that would create a new tab and split it into three sections, each with a different root. Here's the script I tried alongside the error log :
$frontendPath = "C:\Path_Example\frontend"
$backendPath = "C:\Path_Example\backend"
$rootPath = "C:\Path_Example"
$command = "wt ; split-pane -p 'pwsh -NoExit -Command `""cd '$frontendPath'; pwsh`""" +
" ; split-pane -H -p 'pwsh -NoExit -Command `""cd '$backendPath'; pwsh`""" +
" ; split-pane -H -p 'pwsh -NoExit -Command `""cd '$rootPath'; pwsh`"""
Start-Process -FilePath wt.exe -ArgumentList "-d", ".", "-e", $command
At C:\Users\speak\split.ps1:5 char:58
+ $command = "wt ; split-pane -p 'pwsh -NoExit -Command `""cd '$fronten ...
+ ~~
Unexpected token 'cd' in expression or statement.
At C:\Users\speak\split.ps1:6 char:59
+ " ; split-pane -H -p 'pwsh -NoExit -Command `""cd '$backen ...
+ ~~
Unexpected token 'cd' in expression or statement.
At C:\Users\speak\split.ps1:7 char:59
+ " ; split-pane -H -p 'pwsh -NoExit -Command `""cd '$rootPa ...
+ ~~
Unexpected token 'cd' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
As you can see, I'm not very experienced with scripting, so I sought help from ChatGPT. Unfortunately, despite trying different prompts, I haven't been able to resolve the issue. If anyone could provide some guidance or help me troubleshoot, I would greatly appreciate it.
Thanks a lot.
wt.exe
, despite being the CLI of Windows Terminal, is a GUI-subsystem application and therefore launches asynchronously by default; therefore, you can invoke it directly - no need for Start-Process
pwsh.exe
, the PowerShell (Core) 7+ CLI, has a -WorkingDirectory
(-wd
) parameter.
More generally, as Mehrdad Qasemkhani points out, wt.exe
itself has a --startingDirectory
(-d
) parameter that allows specifying a startup working directory, irrespective of what shell is being launched.
Therefore:
Using -d
(--startingDirectory
) in combination with an explicit shell executable (pwsh
):
wt.exe -d $frontEndPath pwsh `; split-pane -H -d $backendPath pwsh `; split-pane -H -d $rootPath pwsh
If your default profile is pwsh
, -d
alone is enough:
wt.exe -d $frontEndPath `; split-pane -H -d $backendPath `; split-pane -H -d $rootPath
Alternatively, using pwsh
's -wd
(-WorkingDirectory
) parameter:
wt.exe pwsh -wd $frontEndPath `; split-pane -H pwsh -wd $backendPath `; split-pane -H pwsh -wd $rootPath
This opens a new Windows Terminal window with a single tab split horizontally into 3 panes, each running a PowerShell (Core) instance with the specified directory as the current location (working directory).
Note the need to escape ;
as `;
, so that PowerShell doesn't interpret it as its statement separator.
If you want to open the new tab in an existing window, more work is needed:
-w 0
targets the most recently active window
however, that window isn't automatically activated (if there is a way to do that via options, do let us know).
$frontendPath = 'C:\'
$backendPath = 'C:\Windows'
$rootPath = 'C:\Users'
# Get the PID (process ID) of what is presumed to be the most
# recently active window, if any.
$existingPid = (Get-Process -ErrorAction Ignore WindowsTerminal | Select-Object -Last 1).Id
# -w 0 targets the most recently active window, if any.
wt.exe -w 0 -d $frontEndPath pwsh `; split-pane -H -d $backendPath pwsh `; split-pane -H -d $rootPath pwsh `; ft 0
# If a preexisting window was targeted, it must now be activated explicitly.
if ($existingPid) {
(New-Object -ComObject WScript.Shell).AppActivate($existingPid)
}