Windows – Is it possible to have scripts run in Windows Terminal

command linepowershellwindows-terminal

I have little power shell and cmd scripts that I run now and then. When I click on a bat script, it opens a classic CMD window. When I run a PS1 script, it opens the classic PowerShell window.

Is it possible to have these open in the new Windows Terminal by default when I run them? No, I don't want to open the terminal, navigate to the script, and run it. I just want to click on the .bat and have it run in the CMD window that is within the Windows Terminal app.

Can this be done?

I tried finding the terminal exe so that I could set ps1 files to open with it but I get an error when I try to do that. See screenshot.

enter image description here

Thanks!

Best Answer

I am putting this here since it is too short for a comment, and that you are not asking a PowerShell code question/issue, but asking a 'How do I configure my environment/chosen tool, to do X or Y.

  1. Windows Terminal is not a shell/command processor.
  2. There is no concept of run in WT.
  3. There is only run with a command processor, PowerShell or cmd.exe. cmd.exe executes .bat, .cmd, .vbs. Powershell executes .ps.
  4. WT, on launch, starts your default shell/command processor.
  • PowerShell.exe
  • Pwsh.exe
  • cmd.exe
  • Bash.exe
  • Python.exe

... whatever processors you have configured in your WT settings.

Even if you reghack this, it will still only run the default shell/command processor you have configured in the JSON settings. So, why start a shell host, just to run a shell/command processor.

What NotTheDr01ds is showing, is already the default when right-clicking a .ps1 file and trying to change that to WT.exe will not work as you'd think.

You can first prove this to yourself by using WinKey + R and typing in the below. One will not work the other will.

wt d:\scripts\hello.ps1
# Results - error and locks WT
<#
[error 0x800700c1 when launching `d:\scripts\hello.ps1']
#>
wt powershell -noprofile -noexit d:\scripts\hello.ps1
# Results - runs the script as expected
<#
Hello World

Tuesday, 23 March, 2021 20:15:28
#>

So, any reg hack would require you to specify the command processor and the filename and any arguments needed for a successful run. As noted in my comments. You can avoid reghacks and just create a shortcut and add this shortcut to your SendTo menu. In Windows Explorer, just type...

shell:SendTo

Or in PowerShell just to this:

explorer shell:SendTo

... and paste your shortcut there (I have lots there), then you have to make the settings changes.

# Example:
"C:\Program Files\WindowsApps\Microsoft.WindowsTerminal_1.6.10571.0_x64__8wekyb3d8bbwe\wt.exe" powershell -noprofile -noexit

I am letting you know in advance, that this ran into some issues.

Sometimes it worked, sometimes it did not.

Now, of course, if you wanted to do this for any of the command processors, then you need to create multiple shortcuts, alter them as needed, and paste them to the SendTo folder, then right-click your file, and select the proper SendTo shortcut.