Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get batch files to run through the new Windows Terminal? [closed]

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.

like image 865
Mashiro-chan Avatar asked May 17 '19 17:05

Mashiro-chan


People also ask

How do I run a .bat file in Windows Terminal?

Command Prompt Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to run a Windows 10 batch file and press Enter: C:\PATH\TO\FOLDER\BATCH-NAME. bat In the command, make sure to specify the path and name of the script.

How do I run a batch file without closing a window?

If you're creating a batch file and want the MS-DOS window to remain open, add PAUSE to the end of your batch file. This prompts the user to Press any key. Until the user presses any key, the window remains open instead of closing automatically.

How do I get a batch file to run automatically when I start Windows?

To run a batch file at start up: start >> all programs >> right-click startup >> open >> right click batch file >> create shortcut >> drag shortcut to startup folder. Show activity on this post. Show activity on this post. Show activity on this post.

How do I keep the console open after executing a batch file?

Edit your bat file by right clicking on it and select “Edit” from the list. Your file will open in notepad. Now add “PAUSE” word at the end of your bat file. This will keep the Command Prompt window open until you do not press any key.


6 Answers

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.

like image 83
Christos Lytras Avatar answered Sep 30 '22 02:09

Christos Lytras


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!

like image 20
tech189 Avatar answered Sep 29 '22 02:09

tech189


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!

like image 20
Ares9323 Avatar answered Sep 28 '22 02:09

Ares9323


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" %*
like image 36
Schbourf Avatar answered Oct 02 '22 02:10

Schbourf


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.

like image 39
Allie Avatar answered Sep 29 '22 02:09

Allie


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

Windows will support change default terminal in future.

like image 21
Yue Yin Avatar answered Sep 29 '22 02:09

Yue Yin