32

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.

Mashiro-chan
  • 79
  • 1
  • 4
  • 8
  • 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! – Zach Pedigo May 17 '19 at 17:59
  • Cite from https://github.com/microsoft/terminal `The easiest way to communicate with the (windows terminal) team is via GitHub issues.` –  May 17 '19 at 19:20
  • not sure if I'm right, my very small knowledge of english, but does this link help? https://www.dostips.com/forum/viewtopic.php?f=3&t=9136 – elzooilogico May 17 '19 at 19:56
  • 1
    @elzooilogico - that link is just two individuals discussing the terminal with no discussion on how to use it. – SomethingDark May 18 '19 at 04: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`. – SomethingDark May 18 '19 at 07:37
  • conemu and its fork cmder support this, not only .bat, but .cmd. – userA789 Apr 05 '21 at 18:47

7 Answers7

48

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.

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
  • 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 May 01 '20 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. – Christos Lytras May 01 '20 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 – Nico Nekoru May 02 '20 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? – Christos Lytras May 02 '20 at 23:31
  • 1
    Unfortunately it didn't for me, only the right-click worked – Nico Nekoru May 03 '20 at 03:36
  • @NekoMusume the `ftype` command works for Windows 10 for double click (*default behavior*), did you chnage `` to your actual user account folder name? – Christos Lytras May 03 '20 at 09:14
  • Its working now, I had issues with controlled folder access. Thank you! – Nico Nekoru May 04 '20 at 19:01
  • After following steps, my .bat files are opening with new terminal on double click, but terminal opens empty(not with lines in .bat). – Prasad Ostwal May 21 '20 at 09:02
  • @PrasadOstwal this is to **run** bat files using *Command Prompt* (*CMD*) using Windows Terminal, not to *display* the bat file contents. Create a bat file that contains `DIR & PAUSE` and then run it. – Christos Lytras May 21 '20 at 09:18
  • @ChristosLytras I meant to say run only! They are not running. thanks for reply. – Prasad Ostwal May 22 '20 at 11:42
  • @PrasadOstwal you are doing something wrong then, because I've tested this on Windows 10 and it runs every batch file using CMD through Windows Terminal. – Christos Lytras May 22 '20 at 11:45
  • nice, but how do you do that for shortcuts ? – jsaddwater Feb 20 '21 at 18:56
  • If you are getting file not found exceptions when double clicking the bat file, then you might have to follow these instructions too: https://stackoverflow.com/a/67878616/4573065 – ST-DDT Aug 18 '21 at 08:10
14

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!

tech189
  • 303
  • 2
  • 10
  • 1
    Is there a way to keep the tab/window open after the bat file finished? – pezcode Nov 12 '21 at 08: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 Nov 13 '21 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 May 30 '22 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!

Ares9323
  • 846
  • 1
  • 12
  • 20
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.

Allie
  • 1,081
  • 1
  • 13
  • 17
  • 1
    Thank you so much. Let me take you out for dinner – Aakash Verma Aug 19 '21 at 06:52
  • what is the `-p` flag i did `wt.exe --help` but it was not listed – Eboubaker Mar 09 '22 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: https://github.com/microsoft/terminal/blob/main/doc/specs/%23607%20-%20Commandline%20Arguments%20for%20the%20Windows%20Terminal.md – Allie Mar 11 '22 at 15:09
  • Where is the above used, are you putting this as the target of a shortcut? – Hannes Ziegler Apr 18 '22 at 20:50
  • you can place the command in a file named "domyaction.cmd" and then run it by using a shortcut – Allie Apr 20 '22 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" %*
Dharman
  • 30,962
  • 25
  • 85
  • 135
Schbourf
  • 61
  • 1
  • 1
1

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

Windows will support change default terminal in future.

Yue Yin
  • 61
  • 1
  • 2
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
MongWu
  • 1