Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am looking for a way to execute some console commands automatically when a command-prompt is opened in Windows, specifically to set some command aliases.

For example, when a command-prompt is opened, I want a command such as the following to be automatically run so that I can use ls instead of dir.

doskey ls=dir

How can this be done?

share|improve this question

migrated from stackoverflow.com Jun 25 '11 at 19:49

This question came from our site for professional and enthusiast programmers.

    
possible duplicate of Automatically run a script when I log on to Windows –  slhck Jun 25 '11 at 20:20
1  
Windows XP does not have DOS. –  grawity Jun 25 '11 at 21:47
    
@KContreau, interessant. So it would be the reason to my batches does not save doskey subsitutions after a "start /separate" command, but only with "cmd /k" or "cmd /c"... –  kokbira Jun 26 '11 at 0:04
    
do we have to change question title or not after "Clarification" edition? –  kokbira Jun 26 '11 at 1:02
1  
@KCotreau: Technically, it's part of Win32 Console subsystem, not specific to cmd.exe. –  grawity Jun 26 '11 at 22:14

6 Answers 6

up vote 6 down vote accepted

If you want it to load on bootup, rather than when you log in, if you have XP Pro (XP home does not include gpedit.msc), do this:

Start>Run>gpedit.msc

and hit enter. Then run gpupdate /force from a command line.

Go to

Computer Configuration>Windows Settings>Scripts>Startup

and add the location to the script.

You can also do this for a user logon under

User Configuration>Windows Settings>Scripts>Startup

If you have XP Home, you can copy the scripts to either:

C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup

for loading them on bootup, or:

C:\Windows\System32\GroupPolicy\User\Scripts\Logon

for loading them for a user on logon. The GroupPolicy folder is hidden.

share|improve this answer
    
I'd prefer this method to putting it in the startup folder. Makes it more permenant. –  Joe Taylor Jun 25 '11 at 20:16
    
@Joe Taylor Thanks. To be honest, I only use the Startup folder for things like starting Outlook. This is a more standard way of doing it from a system-administration point of view. –  KCotreau Jun 25 '11 at 20:18
    
@KContreau, your solution is very useful, but not for that case. I tested but it does not functioned for doskey substitution command... –  kokbira Jun 26 '11 at 0:13
    
@kokbira The original question was simply "How to run a script on startup in XP?", which I think I answered accurately. He edited and added the "for example, and doskey ls=dir" part after. –  KCotreau Jun 26 '11 at 0:51
1  
@KContreau, you are right, he problem is that the title of this question is not correct. In "Clarification" he said that he wants "to run some DOS commands when I start a DOS prompt, mainly to set some aliases". To do some commands when Windows starts, your solution is ok, but to run aliases it is not. –  kokbira Jun 26 '11 at 0:57

Gravity's answer worked great for me.

I created a shell-aliases.cmd script in my Documents folder (Someplace I can find it later).

Then created the 'AutoRun' Expandable String Value in the registry key:

'HKEY_CURRENT_USER\Software\Microsoft\Command Processor'

Set the value to the full path to the aliases file (Mine was in 'C:\Users\Lee\Documents\shell-aliases.cmd').

Once I reloaded the command prompt my doskey aliases worked. Plus my aliases file is easily accessible and ready to edit as I think of more to add.

share|improve this answer

Different command interpreters handle command aliases in different ways.

Microsoft's CMD relies upon the Win32 console subsystem to provide its command aliasing mechanism. The doskey command actually interacts with the process' console, and it is the console itself that holds aliases. Thus aliases are per-console and transient.

JP Software's TCC/LE handles aliases itself. The alias command allows one to view and manipulate aliases, which are either "local" or "global". Aliases are still transient, but are either private to one command interpreter process or shared amongst all command interpreter processes (across all consoles).

Shells such as the Korn shell in the SFUA utility toolkit — whose tools run in the Subsystem for UNIX-based Applications that comes right there in the box with Windows 7 Ultimate edition and Windows Server 2008 R2 (For Windows XP, one can download and install Services for UNIX version 3.5.) — and the Bourne Again shell from cygwin similarly handle aliases themselves. Aliases are transient, and per command interpreter process.

Auto-loading a default initial set of aliases varies by command interpreter, too.

In all cases, one places commands to set the aliases in a script of some sort.

grawity has already given one of the two registry keys that cause Microsoft's CMD to run a command as it starts up. Unfortunately, this is per command interpreter process, even though the aliases themselves are stored per console. Start a secondary command interpreter from the initial one, and any manual changes to the default aliases will be reset. An alternative, that at least performs initialization only once per console, is to add the /K option and the name of a command script that calls doskey to the command tail in the shortcut that starts the command interpreter.

JP Software's TCC/LE automatically runs the TCSTART script upon its startup. Again, this is per command interpreter process. However, TCC's "local" aliases are also per command interpreter process, so this is the right place to initialize aliases if one is employing local aliases. For global aliases, simply create a shortcut in your Startup folder that loads aliases from a file (with alias /r) and then runs the shralias command so that they persist after that shortcut has finished running.

For the Bourne Again and Korn shells, simply do as one would with these shells on any other platform. Use the alias command in the $HOME/.profile and $HOME/.bashrc scripts for the Bourne Again shell; and use the alias command in the $HOME/.profile, ENV, and $HOME/.kshrc scripts for the Korn shell.

share|improve this answer

Command aliases are specific to the console window you configure them on, therefore a startup script will not work. You can do it this way:

  1. Create a batch file (.cmd) with your doskey commands, put it somewhere in Application Data.

  2. Configure it to be started automatically whenever you open cmd. The setting is in Registry:

    • key: HKCU\SOFTWARE\Microsoft\Command Processor
    • value: AutoRun
    • type: REG_EXPAND_SZ
    • data: "%AppData%\aliases.cmd"
share|improve this answer

The problem here is if you do it in startup, these substitutions would be avaliable only in the CMD prompt opened. If it closes, all these substitutions will be lost and must be done again for every command prompt openned.

So, some alternative ways (you can combine them):

I. Create a batch and a shortcut:

  1. Create a batch with these macros and save it to some known place (e.g. c:\apps\mycmd.bat).
  2. Create a shortcut to it in desktop (or where you want) putting "cmd /k" before its path (e.g. "cmd /k c:\apps\mycmd.bat").
  3. So all your commands will be run every time you open that shortcut.

II. Create a batch and a entry in PATH enviroment variable:

  1. Create a batch with these macros and save it to some known place (e.g. c:\apps\mycmd.bat).
  2. Go to Control panel > System > Advanced configs > Advanced > Enviroment variables.
  3. In User or System entries, search for a variable named PATH. Double click on it and, at the end, insert ";" and the path to your BAT (e.g. "c:\apps\" => "previous_string;c:\apps\"), then Ok and Ok.
  4. So when you use a cmd (or in a BAT file), just put the name of a BAT in created path (e.g. "c:\apps") to run your commands (e.g. type "myapps" to run those substitution commands).
share|improve this answer
    
I tried "start /separate" but it saves only enviroment variable (not the substitutions of doskey command)... The "set" command is also valid to the opened prompt only, but we can use "setx" to save it to system or user enviroment variables "forever". I don't know if there is an way to save doskey substititions "forever" to the system or for every Windows startup. I do prefer using my ways... –  kokbira Jun 25 '11 at 23:57

There are several locations that will do, but the start menu > start-up folder probably is the easiest.

Run 'msconfig' and see what programs start up, and why. You can just add your program to one of those lists (for example, in the registry).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.