65

EDIT

If you want to perform any task at computer startup or based on an event this is very helpful

http://answers.microsoft.com/en-us/windows/forum/windows_7-performance/how-to-schedule-computer-to-shut-down-at-a-certain/800ed207-f630-480d-8c92-dff2313c193b


Back to the question

I have two questions:

  1. I want some specific commands to be executed when I start command prompt.

    e.g. cls to clear my command prompt.

  2. I want to execute some commands in a batch file and wait for the user to enter new commands (if any).

    e.g. A batch file which will take the user to some specified folder and then wait for the user to rename/delete a file from the command prompt.

How can I do it?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Rajkiran
  • 15,845
  • 24
  • 74
  • 114

7 Answers7

113

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thank you, it worked for me. Now I need to undo this setting, do you know how I can do it? – alexandre1985 Oct 28 '16 at 15:40
  • [Delete it](https://technet.microsoft.com/en-us/library/cc742145.aspx)? – Ansgar Wiechers Oct 29 '16 at 00:10
  • 17
    Oh, a word of advice. Don't launch things that launch additional command prompts, that results in an infinite loop. – John Leidegren Aug 15 '17 at 07:27
  • How can I specified more than one script? For example to run `init.cmd` and `aliases.cmd` at startup – builder-7000 Apr 17 '19 at 20:24
  • @Sergio I don't think you can, and I wouldn't recommend it anyway. See John Leidegren's comment above. – Ansgar Wiechers Apr 18 '19 at 08:48
  • In Linux I would add a line in `.bashrc` with the code `source .bash_aliases`. I tried something similar in Windows: I added a line in `init.cmd` with `cmd aliases.cmd`. But this created an infinite loop when I started cmd. In conclusion, I could not find a way to execute two files at startup, so I had to put all the commands in `init.cmd`. – builder-7000 Apr 18 '19 at 16:31
  • 2
    @Sergio Windows is not Linux, and CMD is not Bash. Sorry to disappoint. You may want to look into PowerShell instead. – Ansgar Wiechers Apr 18 '19 at 16:56
  • 1
    This method will create problems if you use automated command-line tools (such as deployment tools). Every single process that loads a new shell will load your init.cmd file leading to unexpected results (and definite slowdowns). – pbarney Sep 09 '21 at 17:37
53
  1. Make a shortcut
  2. Go to the properties
  3. 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

Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
LewisTehMinerz
  • 656
  • 7
  • 15
17

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).

zovits
  • 906
  • 16
  • 27
Rajkiran
  • 15,845
  • 24
  • 74
  • 114
3

Expanding a bit, here is an alternative for Windows 10 where multiple aliases can be defined and applied to the Command Prompt upon execution.

  1. Create a file called init.cmd containing aliases on your %USERPROFILE% folder:

init.cmd

@echo off
doskey c=cls
doskey d=cd %USERPROFILE%\Desktop
doskey e=explorer $*
doskey g=git status
doskey l=dir /a $*
  1. Register it to be applied whenever the Command Prompt is executed:

In the Command Prompt, run:

reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f

Done

Now the contents of init.cmd will run for executions of cmd.exe namely from:

  • Taskbar shortcut
  • WIN+R cmd
  • By typing cmd in the File Explorer address bar
  • By running cmd.exe directly from C:\Windows\System32

After registering these settings just remember to close/open:

  • The Command Prompt so the settings are applied
  • The File Explorer, if you use to launch the cmd via File Explorer address bar

To unregister it, run:

reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun
rbento
  • 9,919
  • 3
  • 61
  • 61
  • 2
    For curious/newbies: the flags to `reg add` mean: `/t REG_EXPAND_SZ`: [type of value: string, expanding env variables](https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-value-types), `/f`: force overwrite. – philb Jul 29 '21 at 16:11
2

First, you need to press Windows Key + R. In the box that appears, type "regedit" (without the quotes). The Windows Registry Editor should open. Now, locate to HKEY_CURRENT_USER/Software/Microsoft/Command Processor. Once you have clicked on Command Processor on the left side, click Edit on the top bar. Then go to New > String Value in the Edit menu. Rename the String Value that appears to Autorun. Right click on Autorun and select Modify. Under the "Value Data" area, type in the commands you want to run. You can run multiple by typing && between them.

Chaos
  • 21
  • 2
0

I have a command to run a python program. I do not want to run this command manually after login, I want this command should run automatically after I logged in to my ubuntu. I am using Ubuntu 16.04.

Here is the command.

sh demo_darknet_yolov3.sh , this shell is placed in this directory littro@littro-System-Product-Name:~/MobileNet-YOLO-master/MobileNet-YOLO-master

Asad Javed
  • 31
  • 1
  • 6
0

Depending on your script, you may want to use the cmd.exe /k <input script> method instead of the registry entry Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\autorun

I have found with the latter, that other programs that launch cmd are affected by the registry entry. For example, I cannot get Visual Studio Native Tools prompt to work properly because my script gets in the way. In my case, the script is a menu with 5 options including to launch various programs manually (I like to minimize my auto-run programs) and set various environment variables (ie., printers, proxy settings, default versions of programs, etc).

If you are doing something static I think either method works fine.

I would have commented on the question or an applicable answer, but I do not have the reputation to comment.

brawny84
  • 61
  • 1
  • 4