How to run a command on command prompt startup in Windows
If you want a defined set of commands to run every time you start a command prompt, the best way to achieve that would be to specify an init script in the AutoRun registry value. Create it like this (an expandable string value allows you to use environment variables like %USERPROFILE%
):
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun ^
/t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f
Then create a file init.cmd
in your profile folder:
@echo off
command_A
command_B
...
cls
To remove these changes, delete the registry key:
reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun
- Make a shortcut
- Go to the properties
- The bit where it says:
C:\Users\<Your username>\Desktop\cmd.exe
, you put:-cmd /K <your command here>
e.g.
C:\Users\Lewis\Desktop\cmd.exe -cmd /K color 1f
This is the way to launch 1 command without having to mess about with the registry.
Run multiple commands
You can also use &
(and) operator to execute multiple commands.
Eg.
C:\Users\Lewis\Desktop\cmd.exe -cmd /K color 1f & H: & <your command>
Credits: user6589073
I found my answer: I should use the /K
switch, using which I can enter a new command on the opened command prompt.
E.g. cmd /K cls
will open a command prompt for me and clear it. (Answer for question 1)
and
cmd /K MyBatchFile.bat
will start a command prompt, execute the batch file and stay on the command prompt and will not exit. (Answer for question 2).