Posted in:

I recently worked on a project where we needed to simultaneously launch lots of PowerShell windows. This obviously resulted in a lot of clutter of windows, and I wondered whether it would be possible to launch them all as tabs in a single instance of Windows Terminal instead, to reduce clutter and to allow them all to be closed together.

The good news is that Windows Terminal can be launched quite easily with wt.exe and has some command line arguments that enable us to get all the windows as tabs in a single Windows Terminal instance.

The key commandline arguments are -w to allow us to specify a window name. You can make up your own name here, but be careful not to start another with the same name too quickly or it won't detect that the first one exists. I found that sleeping 1 second after launching the first instance of wt.exe was enough. There may well be a more elegant way but some of the alternative techniques I tried didn't work.

You then supply nt to request a new tab, followed by additional flags.

The -p argument lets you pick the profile and -d lets you pick the starting directory. There are also flags to set the tab title and color although I found that the title got ignored.

For the command to run inside the new tab I used PowerShell -c followed by the name of the actual PowerShell script I wanted to run. This seemed to work out a lot better than just trying to execute a PowerShell script directly.

So the overall command line looks like this:

wt.exe -w myWindow -d C:\myworkingfolder PowerShell -c .\run.ps1
# n.b. pause briefly to allow a windows terminal with this name to 
# start up before trying to create more tabs in that window

Here's some C# code that you can use to launch the window.

var folder = "C:\\myworkingfolder";
var processInfo = new ProcessStartInfo("wt.exe") {
    WorkingDirectory = folder,
    Arguments = $" -w myWindow nt -d {folder} PowerShell -c .\\run.ps1" 
};
Process.Start(processInfo);