1

How to make Windows10 CMD to run a certain command each time I bootstrap it?

There is a certain command I want to run each time I open a new CMD window in Windows10 home.

How could this be done?

In Ubuntu for example, I would just type this command in /etc/bash.bashrc, but how can this be done in Windows10?

Thanks,

  • 2
    Possible duplicate of [Automatically executing commands when a command-prompt is opened](https://superuser.com/questions/302194/automatically-executing-commands-when-a-command-prompt-is-opened) – phuclv Jun 26 '18 at 14:51
  • [Can I run a script every time the cmd program is run?](https://superuser.com/q/804378/241386) – phuclv Jun 26 '18 at 14:53

2 Answers2

3

The shortcut to the command line literally runs cmd.exe and nothing else.

If you want to run a batchfile file when you open cmd, find the Command Prompt shortcut in the start menu, and right click / properties.

You'll see

%windir%\system32\cmd.exe

Change this to

%windir%\system32\cmd.exe /k c:\path\to\batch\file.bat

Replace the file location with your file.

When you run CMD, it will run the batch file. Normally the process exits once the batch file is complete, but the /k switch keeps it open for use.

Paul
  • 59,223
  • 18
  • 147
  • 168
  • @Benia The single command can be appended instead of the batch file, it will work the same way. You can use this approach to add cmd.exe to your menu as a proper short cut, https://superuser.com/questions/948088/how-to-add-exe-to-start-menu-in-windows-10 then pin the result to your taskbar. – Paul Jul 13 '17 at 02:24
  • Hmm this seems to work but I need this solution for the taskbar icon. I went to the original file location instead editing the shortcut but there I didn't have any field to insert `/k MyCommad`... –  Jul 13 '17 at 02:32
  • After changing the CMD shortcut as you said, I had to replace it with the taskbar shorcut that already existed in taskbar and referenced the original file. So, now it works fine. Thanks. –  Jul 13 '17 at 03:04
1

Based on Paul's answer that I accepted, this is what I did:

  1. Search for CMD in Windows search.
  2. Left click the icon and right click "Open file location".
  3. Left click the shortcut and go to properties.
  4. In properties, under target, enter:

    %windir%\system32\cmd.exe /k bash
    
  5. Open CMD and drag the CMD window itself to the taskbar.

Note: You might want to delete any non bash-oriented taskbar shortcuts you might have had. (You can always access CMD quickly by executing exit.)

phuclv
  • 26,555
  • 15
  • 113
  • 235