33

I tend to have a lot of cmd prompts open at a time from running multiple batch files at once, so I figured the new Windows Terminal would be a neat way to tidy things up with tabs.

Problem is, I can't quite figure out how to change the default behavior of running batch files through a cmd instance to the desired running batch files through a cmd instance that is inside of a Windows Terminal instance.

I'm able to easily open Windows Terminal, start a cmd instance, cd to the correct dir, and run the batch file.. but that's a lot of effort. I want to just be able to run a batch file, and have it work through a cmd prompt that's inside Windows Terminal.

6
  • It is possible that this feature is not yet available, I urge you to seek out the current developers and ask if it is available or not and if it's not, you could log an enhancement issue for them to ensure that the usability of that new software is further increased! Commented May 17, 2019 at 17:59
  • Cite from github.com/microsoft/terminal The easiest way to communicate with the (windows terminal) team is via GitHub issues.
    – user6811411
    Commented May 17, 2019 at 19:20
  • not sure if I'm right, my very small knowledge of english, but does this link help? dostips.com/forum/viewtopic.php?f=3&t=9136 Commented May 17, 2019 at 19:56
  • 1
    @elzooilogico - that link is just two individuals discussing the terminal with no discussion on how to use it. Commented May 18, 2019 at 4:58
  • 1
    I haven't had a change to test it out yet, but I'd imagine that you either change the executable associated with the .bat extension or you use start. Commented May 18, 2019 at 7:37

7 Answers 7

50
+50

You can have this behaviour on double click by changing HKEY_CLASSES_ROOT\batfile\shell\open\command default value from:

"%1" %*

to:

"C:\Users\<user>\AppData\Local\Microsoft\WindowsApps\wt.exe" -p "Command Prompt" "%1" %*

or by using ftype command:

ftype batfile="C:\Users\<user>\AppData\Local\Microsoft\WindowsApps\wt.exe" -p "Command Prompt" "%1" %*

You have to change <user> with the current user name directory and of course, this wt.exe path (C:\Users\<user>\AppData\Local\Microsoft\WindowsApps\) is if you've installed Windows Terminal from Microsoft Store, if you haven't, then you have to find the path to wt.exe and use that instead. Windows enviroment variables %LOCALAPPDATA% and %USERPROFILE% did not work for me, so I'm using full path here.

You can create a registry entry to have an option to opem a BAT file using Windows Terminal and having it running with CMD:

  1. Open regedit and navigate to HKEY_CLASSES_ROOT\batfile\shell key.
  2. Create a key with the name wtopen and the default value Open with Windows Terminal:

RegEdit batfile shell commands

  1. Create a subkey HKEY_CLASSES_ROOT\batfile\shell\wtopen\command with the default value:
    "C:\Users\<user>\AppData\Local\Microsoft\WindowsApps\wt.exe" -p "Command Prompt" "%1" %*
    

And now you'll have a new entry "Open with Windows Terminal" when you right click on BAT files and when you click it it will open a new Windows Terminal with a Command Prompt panel running the selected batch file:

Windows explorer BAT file right click

Tested with Windows Terminal 0.11.1121.0

The default name for the Command Prompt (cmd) profile is "Command Prompt". You'll have to change it using the -p "<name>" parameter if you have other name than the default.

14
  • 1
    more general: "%LOCALAPPDATA%\Microsoft\WindowsApps\wt.exe" Not tried, but should be possible to change the default from command prompt with ftype batfile="%LOCALAPPDATA%\Microsoft\WindowsApps\wt.exe" %1 %*
    – Stephan
    Commented May 1, 2020 at 21:12
  • @Stephan I tried %LOCALAPPDATA% and %USERPROFILE%\AppData\... but these don't work but ftype is a quicker way to set it, yes. Commented May 1, 2020 at 21:18
  • 1
    This works but the question asked if there was a way to open a bat file in windows terminal by default just with double click instead of a right-click option Commented May 2, 2020 at 22:56
  • 1
    @NekoMusume yes, I cover that on the beggining of my answer; changing HKCR\batfile\shell\open\command didn't work for you? Commented May 2, 2020 at 23:31
  • 1
    Unfortunately it didn't for me, only the right-click worked Commented May 3, 2020 at 3:36
16

The other answers work, but I also wanted batch files to open as a new tab if Windows Terminal is already open and allow scripts without absolute paths:

Open the Registry Editor and navigate to this key:

Computer\HKEY_CLASSES_ROOT\batfile\shell\open\command

Then modify the (Default) value to the following:

wt.exe -w 0 new-tab -d . "%1" %*

where the -w 0 argument specifies to open in the first or existing window in a new-tab at the current working directory of the file: -d .

Tip: if you have a batch file set up to silently run in the background that you don't want popping up, just rename it from .bat to .cmd!

3
  • 1
    Is there a way to keep the tab/window open after the bat file finished?
    – pezcode
    Commented Nov 12, 2021 at 8:59
  • 1
    Just add a new line at the end of your batch file with the command pause. When it reaches that point it will stop and prompt you to press any key to exit, keeping it open until you do so.
    – tech189
    Commented Nov 13, 2021 at 11:01
  • ftype batfile="%LOCALAPPDATA%\Microsoft\WindowsApps\wt.exe" -w 0 new-tab -d . -p "Command Prompt" "%1" %* no need to open the registry editor.
    – ZR Han
    Commented May 30, 2022 at 11:16
8

I've been looking for this for at least a year, today I finally found a solution.. In the Microsoft Forum an Advisor Said:

.Bat extension is a Command Prompt executable format. It would not support in the New Windows Terminal as I have checked.

Fortunately this is wrong, I found a way to set the new Windows Terminal as Default:

Just create a .reg file with this code, (replacing "Marco" with your username) and run it:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\batfile\shell\open\command]
@="\"C:\\Users\\Marco\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe\" \"%1\""

You also need to edit the windows terminal settings.json and change the default:

"startingDirectory": "%USERPROFILE%"

to

"startingDirectory": "."

If you don't do this it won't work because it tries to run it inside the user folder and not in the current!

2
  • 3
    Works like a charm, although I had to put the starting directory part under the command prompt profile part of the settings.json for it to change its directory. Commented Apr 4, 2021 at 17:13
  • @Mathew Vandenberg this comment should be part of the answer Commented Jun 18, 2021 at 16:07
6

Today I found that the following command works (without the need to change the registry):

wt -p "Command Prompt" {pathtoyourbatchfile1} ; nt {pathtoyourbatchfile2}

The command above opens 2 tabs with the specified batch files. The "nt" part defines the additional tabs, so you can repeat it to initialize multiple tabs.

5
  • 1
    Thank you so much. Let me take you out for dinner Commented Aug 19, 2021 at 6:52
  • what is the -p flag i did wt.exe --help but it was not listed
    – Eboubaker
    Commented Mar 9, 2022 at 17:07
  • indeed that help page does not list everything. the -p flag specifies which program to run and the title for the "tab". See here for more information and examples: github.com/microsoft/terminal/blob/main/doc/specs/…
    – Allie
    Commented Mar 11, 2022 at 15:09
  • Where is the above used, are you putting this as the target of a shortcut? Commented Apr 18, 2022 at 20:50
  • you can place the command in a file named "domyaction.cmd" and then run it by using a shortcut
    – Allie
    Commented Apr 20, 2022 at 13:29
6

As stated by tech189,

The other answers work, but I also wanted batch files to open as a new tab if Windows Terminal is already open and allow scripts without absolute paths

and if "Command Prompt" is not the default profile in Windows Terminal, I want the .bat files to open with the profile "Command Prompt" in the Windows Terminal.

So, using the solutions of Christos Lytras and tech189, I was able to find a solution that works for me:

Open the Registry Editor and navigate to this key:

Computer\HKEY_CLASSES_ROOT\batfile\shell\open\command

Then modify the (Default) value to the following:

wt.exe -w 0 new-tab -d . -p "Command Prompt" "%1" %*
1

It will be supported by https://github.com/microsoft/terminal/issues/492

Windows will support change default terminal in future.

0

For the best way,if we need to add the System Environment in path, we need to change the REG_TYPE from REG_SZ to REG_EXPAND_SZ or we will only use the absolute path

The path %LOCALAPPDATA%\Microsoft\WindowsApps\wt.exe -w 0 nt -d . "%1" %* under the HKEY_CLASSES_ROOT\batfile\shell\open\command will be the below

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\batfile\shell\open\command]
@=hex(2):25,00,4c,00,4f,00,43,00,41,00,4c,00,41,00,50,00,50,00,44,00,41,\
  00,54,00,41,00,25,00,5c,00,4d,00,69,00,63,00,72,00,6f,00,73,00,6f,00,66,00,\
  74,00,5c,00,57,00,69,00,6e,00,64,00,6f,00,77,00,73,00,41,00,70,00,70,00,73,\
  00,5c,00,77,00,74,00,2e,00,65,00,78,00,65,00,20,00,2d,00,77,00,20,00,30,00,\
  20,00,6e,00,74,00,20,00,2d,00,64,00,20,00,2e,00,20,00,22,00,25,00,31,00,22,\
  00,20,00,25,00,2a,00,00,00

Not the answer you're looking for? Browse other questions tagged or ask your own question.