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 forStart-Processpwsh.exe, the PowerShell (Core) 7+ CLI, has a-WorkingDirectory(-wd) parameter.More generally, as Mehrdad Qasemkhani points out,
wt.exeitself 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):If your default profile is
pwsh,-dalone is enough:Alternatively, using
pwsh's-wd(-WorkingDirectory) parameter: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 0targets the most recently active windowhowever, that window isn't automatically activated (if there is a way to do that via options, do let us know).