powershellterminalsplitpane

Windows Terminal : Split pane with different root path (Powershell)


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.


Solution

  • Therefore:

    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:

    $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)
    }