How do I run a Windows Terminal command from a shortcut? - windows-terminal

The same way you can echo "hello world" on cmd by creating a shortcut using "C:\Windows\System32\cmd.exe" /k echo hello world, how can I do the same with Windows Terminal? Swapping the cmd file location with the wt file location doesn't work. It says "/k was not expected."

You'll want to prepend wt.exe to that command, not replace cmd.exe. So the full commandline should look like:
wt cmd.exe /k echo hello world
That tells wt.exe to run the commandline cmd.exe /k echo hello world.

Related

Strange behavior when running start.bat file in Windows 2019 Core

I have a start.bat which does nothing but
#echo off
Echo "Hello World"
Then I open a cmd window and type in
start "" /D D:\Test start.bat
it opens a new cmd window with "Hello World", but does not close the window automatically.
Now I created another bat file named start2.bat which has the same content as the start.bat above. Then I type in
start "" /D D:\Test start2.bat
it opens a new cmd window with "Hello World", and does close the window automatically.
Why is it happening ???
I try to use some trace tool to analyse, from the tool, it seems like my system turn start "" /D D:\Test start.bat to cmd.exe /K start.bat
TraceTool
Tried in another computer, now, no matter which file name I used, it turns into cmd/exe /K start.bat
start3.bat
Update:
Turns out adding /C does not work for me, attach process tree below, the system still changes /C to /K
Process tree view with /C
Update:
Provide full process view. My start.bat is exactly the same as above
And now I open a cmd.exe window, and typed in
"c:\windows\system32\cmd.exe" /C START "test" /D "C:\test" start.bat or just cmd /C START "test" /D "C:\test" start.bat
And
"c:\windows\system32\cmd.exe" /C START "test" /D "C:\test" start2.bat or just cmd /C START "test" /D "C:\test" start2.bat
From the process tree view, it can be seen that the first command turns into
C:\windows\system32\cmd.exe /K start.bat
While the second command turns into
C:\windows\system32\cmd.exe /c "c:\test\start2.bat" "
cmd.exe
Full Process Tree View
From the usage information of the start command (excerpt from the description of the command/program argument):
[...]
command/program
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.
If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.
[...]
Hence when starting an internal command or a batch file, cmd.exe together with its /K option is used, so start "" "script.bat" is equivalent to:
start "" cmd.exe /K "script.bat"
If you want the new console window to become closed, use this instead:
start "" cmd.exe /C "script.bat"

Windows Registry Run Key

According to http://msdn.microsoft.com/en-au/library/aa376977(v=vs.85).aspx:
"Run and RunOnce registry keys cause programs to run each time that a user logs on. The data value for a key is a command line."
Should I then be able to add a key with:
Name: MyName
Data: START /MIN "Title" "cmd.exe" /c "#echo off && "C:\TestApplication.exe" -Arg1 "Arg2"
with the goal being to start my console application "C:\TestApplication.exe" minimized with arguments "-Arg1 "Arg2"" when Windows starts?
I ask because I cannot seem to get it working.
The documentation is misleading, though not strictly incorrect. The command line is passed directly to CreateProcess() rather than being passed to cmd.exe so commands internal to cmd.exe, such as start, are not valid. This means you need to add cmd /c at the start of the command line.
You're also missing a quote mark at the end, and you don't need to put quotes around cmd.exe. This works:
cmd /c START /MIN "Title" cmd /c "#echo off && "C:\TestApp.exe" -Arg1 "Arg2""
However, since the target application is an executable, not a batch file, you don't need the #echo command either:
cmd /c START /MIN "Title" cmd /c ""C:\TestApp.exe" -Arg1 "Arg2""
(Note that the command line passed to /c is never echoed.)

cmd.exe /k switch

I am trying to switch to a directory using cmd and then execute a batch file
e.g.
cmd /k cd "C:\myfolder"
startbatch.bat
I have also tried (without success)
cmd cd /k cd "C:\myfolder" | startbatch.bat
Although the first line (cmd /k) seems to run ok, but the second command is never run. I am using Vista as the OS
Correct syntax is:
cmd /k "cd /d c:\myfolder && startbatch.bat"
ssg already posted correct answer. I would only add /d switch to cd command (eg. cd /d drive:\directory). This ensures the command works in case current directory is on different drive than the directory you want to cd to.
cmd cd /k "cd C:\myfolder; startbatch.bat"
or, why don't you run cmd /k c:\myfolder\startbatch.bat, and do cd c:\myfolder in the .bat file?
I can't see an answer addressing this, so if anyone needs to access a directory that has space in its name, you can add additional quotes, for example
cmd.exe /K """C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"" & powershell.exe"
From PowerShell you need to escape the quotes using the backquote `
cmd.exe /K "`"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat`" & powershell.exe"
Notice the escaped quotes
`"
inside the path string:
"`"C:\my path\`""
This will execute the proper command in cmd, i.e. the path surrounded with quotes which should work.
The example command above will initialise the MSVC developer command prompt and go back to PowerShell, inheriting the environment and giving access to the MSVC tools.
You can use & or && as commands separator in Windows.
Example:
cmd cd /K "cd C:\myfolder && startbatch.bat"
I give this as an answer because I saw this question in a comment and cannot comment yet.
cmd /k "cd c:\myfolder & startbatch.bat"
works, and if you have spaces:
cmd /k "cd "c:\myfolder" & startbatch.bat"
As I understand it, the command is passed to cmd as "cd "c:\myfolder" & startbatch.bat", which is then broken down into cd "c:\myfolder" & startbatch.bat at which point the remaining " " takes care of the path as string.
You can also use &&, | and || depending on what you want to achieve.

"start %comspec% /c script.cmd" vs "start cmd /C script.cmd"

What is the difference between following commands:
start %comspec% /c script.cmd
start cmd /C script.cmd
I need that cmd window for script.cmd should close automatically when script.cmd is finished.
%comspec% just points to cmd.exe, so both commands will do the same thing. Other than that /C is correct, this will close the command prompt after execution
The difference is that "%comspec%" should expand to the default command interpreter, whereas "cmd" searches for a cmd executable and invokes it. Most of the time they are the same.
But in case there is a cmd executable (cmd.exe, cmd.bat, etc) in the current directory, than that cmd executable will be invoked.
I would go with using %comspec%
For a not admin user on WindowsXP-SP2.
%comspec% starts in %WINDIR%\System32.
'cmd' starts in user's home e.g. C:\Document And Settings\USER1.

Pre-Populate a Windows Command Prompt Windows with Text

does anyone know if there is a way (programatically or not) to open up a windows command prompt window and pre-populate it with text?
Run CMD.EXE /K ECHO The Text
Create a batchfile (.bat) that uses the type command, then open that:
echo "Hello World"
echo "Insert junk here"
cmd.exe

Resources