Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Programmatically Opening Windows Terminal in a Specific Folder


:P
On this page:
Edit this Post

This post is mostly obsolete, as Windows Terminal now supports a -d parameter for specifying a startup folder: wt.exe -d "c:\temp". Leaving this post in place for archival purposes only.

A related and more recent post that talks about launching from other applications can be found here:
Opening an Admin Windows Terminal from Visual Studio


I've been using Windows Terminal for a while now since it was announced and made available in the Windows Store a few months back.

There are lots of reasons to like this new Terminal and I don't want to get into all of them here. For me, the main reason is that it ships with a new ConHost.exe, that is considerably faster than the ConHost shipped with Windows which results in much faster terminal painting and scrolling.

Keep in mind that Windows Terminal is a Terminal UI, plus the Console Host, and it's not a replacement for the Powershell, CMD or Bash shells or any other shell. The Terminal acts as a host to those shells that are loaded inside of it. Any shell application uses ConHost and the Terminal UI provides the hosting UI for these shell instances.

A few reasons to check it out:

  • Multiple tabs of Shell Instances
  • Very fast text rendering
  • Smooth text scaling
  • Better terminal spec compliance
  • Open source on GitHub and under active development
  • Many improvements expected...

If you haven't yet, check it out...

No Automation Yet

The terminal works great, but it's still in the early stages of development. The core terminal functionality is rock solid, but the Windows UI shell and command interface are not very feature rich around the edges yet. It's all very utilitarian which is to be expected - after all the focus first and foremost is on building a better Console Host and it's an early preview.

To make it easy to load, Microsoft provides a globally mapped executable - wt.exe - that you can launch without providing a path from anywhere in your system: From Windows-R, from another console or from another application using CreateProcess() related APIs.

You can launch Windows Terminal from anywhere with:

wt.exe

or by using the installed shortcut.

One problem currently is that you can't easily automate the terminal application when it launches. There are no command line options supported yet (although there's discussion around this and it will come eventually), which means you can't start up the shell in a specific folder, execute a startup command or even pick a profile to start with.

Recently I had several people asking about Windows Terminal in Markdown Monster:

How can I launch Windows Terminal as my Terminal Viewer in Markdown Monster

The short answer is:

  • You can customize the startup default Terminal Shell Profile
  • Set "startingDirectory" : "%__CD__%"
    which starts the Shell out of the active OS folder
  • Side effect: Windows shortcut launching launches from System folder

For more information read on.

Markdown Monster and Terminals

Markdown Monster has configuration options that let you customize the Terminal executable and Command Arguments so you can customize which terminal gets launched. The default is Powershell but it's easy to add a commandline to switch the Cmd.exe or WSL or another version of Bash. In the program the terminal launching is provided via context menu options from with from various folder related operations:

MM does this from number of places:

  • From the current document tab or document
  • From the Folder Browser Folder
  • From a file or folder in the Folder Browser
  • From the Git Commit Dialog

I get a lot of use out of that feature and I suspect others use it quite a bit as well especially given several of the Window Terminal requests.

Unfortunately if I want to use wt.exe as my shell option, I can't pass the command parameters the way I currently do with the other shells, which is by using custom launch commands in the shell to change to a specific folder.

For example for the default Powershell terminal the default is:

powershell.exe     -NoExit -Command  "& cd '{0}'"

Since Windows Terminal is really a shell host, rather than an actual shell, you can't pass parameters directly to the shell. wt.exe currently doesn't have any command line parameters (AFAIK) so there's no way to set the working folder or push a command to the launched shell.

I also can't specify which configured terminal to start up via an option - basically all you can do is wt.exe without arguments at the moment and hope for the best.

Automating anyway

To launch Windows Terminal programmatically I can use code like the following:

var pi = new ProcessStartInfo{
	FileName = "wt.exe",
	WorkingDirectory = "c:\\temp", 	    
	UseShellExecute = false		
};
Process.Start(pi);

and that works, except it fails to load out of the WorkingDirectory.

The problem with this approach is that you get only the default configuration, and the folder - even though set via the WorkingDirectory in the start info - is completely ignored by the wt.exe startup due to a default profile setting. Hrmph!

Windows Terminal Profiles

The bad news is that you can't pass a working folder to wt.exe via a startup command yet.

What you can do however is to customize the startup Profile and change it so the profile starts the shell in the currently active folder. You can make a change to a configuration key in the default Windows Terminal Shell profile.

This works, but it means it's up to the user to customize their default profile, which isn't terribly user friendly, but it's a workaround that works for now.

You can access the Windows Terminal profile JSON file by going to Settings in the Terminal itself using the down button:

If you edit that file you'll find:

Here you can specify multiple profiles for each type of shell window, and you can add additional profiles of your own or customize the existing ones.

Each profile has a guid key that uniquely identifies it and the startup profile is referenced by a defaultProfile key that points at one of these profile guids.

Forcing the Startup Path

So in Markdown Monster I would love to use Windows Terminal and after searching around a little bit unsuccessfully to find command line options I posted a message on Twitter asking if anybody had gotten this launching WT in a specific folder to work.

@ChristofJans ended up helping me out:

Here are the relevant keys:

"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
...
"profiles" : 
[
    {
        "commandline" : "powershell.exe",
        "guid" : "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
        "name" : "Windows PowerShell",
        "startingDirectory" : "%USERPROFILE%",
    },
    { ... }
]    

The gist of is that by default all profiles are configured with a hard coded startingDirectory setting to the user profile

"startingDirectory" : "%USERPROFILE%","

You can change this folder to use the active working directory with the following change:

"startingDirectory" : "%__CD__%"

And voila WT now opens in the specified path if you set the path prior or provide a WorkingDirectory to the CreateProcess APIs.

Side Effects

Unfortunately, there's a side effect: Now when you start wt.exe from your default shortcut it'll start in your SYSTEM folder:

That's not ideal, but I can live with that. It's easy to cd ~.

I suspect there's a way to fix the startup path the Windows shortcut somehow by setting the shortcut starting directory, but - it's a bloody Windows Store app and that shit is buried somewhere and not worth the effort to have it blown away on the next update anyway.

The ideal solution here would be for WT.exe to provide a way to select a profile to invoke, then setup a custom profile that's not the default and add the %__CD__% pathing there, which would provide the features needed for applications, while leaving the default profile intact.

Overall profiles are great because they do make it easy to create new shell configurations quickly simply by copying profile entries and modifying a couple of obvious settings.

Using Windows Terminal as an External Tool in Visual Studio

So with what you know now you can also set Windows Terminal as an external tool in other applications like Visual Studio's External tools for example. I like to have a quick way to open a terminal in the currently selected item's folder. You can do this with this in Visual Studio Tools → External Tools...:

Notice that this is a bit convoluted because Visual Studio fails to directly load wt or wt.exe. Not sure why given that it seems to work using Process.Start() in .NET even without shell execution, but using wt.exe directly gives an Invalid Executable error. So this instead launches a CMD shell and the START command which does use shell commands, to launch Windows Terminal via the WT command. Ugly because it briefly flashes the CMD window, but otherwise it works.

Remember that you need the default directory of the default profile set to "startingDirectory" : "%__CD__%" in order to launch from the base folder rather than some other hardcoded path.

Summary

The good news is that with the startingDirectory value set in the default profile, you can get Windows Terminal to launch from a folder of your choice. This makes it possible to use Windows Terminal from Visual Studio and also from Markdown Monster:

and it works just dandy now!

The terminal has been a joy to use, which is why I'm mucking around with all this in the first place. I'm following up on the request I got earlier because - heck - I want to use the Windows Terminal in MM too 😃. And now I can...

this post created and published with Markdown Monster
Posted in Windows  .NET  

The Voices of Reason


 

Beej
January 27, 2020

# re: Programmatically Opening Windows Terminal in a Specific Folder


Colin Dekker
February 14, 2020

# re: Programmatically Opening Windows Terminal in a Specific Folder

Edit Windows Terminal profile.json and add the following to the desired profile(s):

"startingDirectory": "."

Download the official .ico file from

https://raw.githubusercontent.com/microsoft/terminal/master/res/terminal.ico

Then, save the reg commands below to a .reg file, replacing [[USERNAME]] and save it. (The path to wt.exe is hardcoded but could easily be changed to an expandable string, too lazy myself 😛 )

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt]
@="Windows Terminal Here"
"Icon"="D:\\tools\\terminal.ico"

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt\command]
@="C:\\Users\\[[USERNAME]]\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe"

Rick Strahl
February 14, 2020

# re: Programmatically Opening Windows Terminal in a Specific Folder

Looks like with the latest terminal updates we now get a good command line interface.

Open a folder:

wt.exe -d c:\temp

Open with a specific profile:

wt.exe -d c:\temp -p "cmd"

pavi2410
March 27, 2020

# re: Programmatically Opening Windows Terminal in a Specific Folder


mohammed
May 27, 2020

# re: Programmatically Opening Windows Terminal in a Specific Folder

  • i opened new instance of terminal and it starts at C:my_user_name
  • i ran cd to my desire project to work with
  • new tab open in C:my_user_name again!!!! how can i configure it to open new tab of the current working directory ?

Ana Helena
September 20, 2021

# re: Programmatically Opening Windows Terminal in a Specific Folder

Hi Rick. Thanks for the post!

What if I wanted to open a new tab using a path? Can you help me with this? For instance, I have a folder D:/dev and I'd like to open this open using a profile to open in a new tab.

Thanks in advance. Ana


klaudyu
February 08, 2024

# re: Programmatically Opening Windows Terminal in a Specific Folder

although I didn't see it documented (wt -h), this works: wt -d <folder> or wt --startingDirectory <folder>


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024