Automatically executing commands when a command-prompt is opened
Solution 1
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.
Solution 2
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:
Create a batch file (
.cmd
) with your doskey commands, put it somewhere in Application Data.-
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"
- key:
Solution 3
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.
Solution 4
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.
Solution 5
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:
- Create a batch with these macros and save it to some known place (e.g. c:\apps\mycmd.bat).
- 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").
- So all your commands will be run every time you open that shortcut.
II. Create a batch and a entry in PATH enviroment variable:
- Create a batch with these macros and save it to some known place (e.g. c:\apps\mycmd.bat).
- Go to Control panel > System > Advanced configs > Advanced > Enviroment variables.
- 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.
- 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).
Related videos on Youtube
Daniel Williams
I love coding! The way I get to do what I love is by specializing in integration data between Project Management and ERP systems. For example, I integrate Primavera with JD Edwards. Or PeopleSoft, or Timberline, or, heck, even an Excel spreadsheet. I have a number of frameworks I've developed to help me do this. Most of my work is done on C#, using Windows Forms, ASP.NET, and Silverlight. I also play with developing WP7 apps just for fun.
Updated on September 18, 2022Comments
-
Daniel Williams almost 2 years
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 ofdir
.doskey ls=dir
How can this be done?
-
slhck about 13 yearspossible duplicate of Automatically run a script when I log on to Windows
-
user1686 about 13 yearsWindows XP does not have DOS.
-
kokbira about 13 years@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 about 13 yearsdo we have to change question title or not after "Clarification" edition?
-
user1686 about 13 years@KCotreau: Technically, it's part of Win32 Console subsystem, not specific to
cmd.exe
. -
Synetech about 13 years@KCotreau, > Also, doskey in Windows did not do aliases Huh? It works for me. I open a command-prompt, typed
doskey bbdd=dir c:\/p
, then usedbbdd
and it executeddir c:\/p
as expected. Its aliasing functionality is intact. -
hicklypups about 13 years@Synetech inc. Thank you. My memory and research failed me. Wait until you get to be my age, you will understand. :) In my defense, again, that was not part of the original question.
-
Synetech about 13 yearsNo problem; I miss DOS—though in your (re, um, attack?
:-D
), you said that after Daniel added the doskey command to the question. Anyway, I think this is what Daniel is asking about; too badAUTOEXEC.NT
won’t work forCMD.EXE
. -
Joe Internet about 13 yearsYou might be interested in UnixUtils... unxutils.sourceforge.net
-
-
Joe Taylor about 13 yearsI'd prefer this method to putting it in the startup folder. Makes it more permenant.
-
hicklypups about 13 years@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.
-
kokbira about 13 yearsI 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...
-
hicklypups about 13 years@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.
-
kokbira about 13 years@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 about 13 years@KContreau I understood that your answer were right before question edition...
-
hicklypups about 13 years@kokbira I can't really answer the second question...it really should have been asked separately.
-
James about 8 yearsThis was a great solution for a Win2K Virtual OS where I wanted the command prompt to always load my compiler paths. Only difference was the use of REG_SZ instead of REG_EXPAND_SZ
-
nburr almost 8 yearsThis is a nice solution. By chance, is there any way to suppress the initial output?
-
user1686 almost 8 years@Nickolas: Just redirect the commands to NUL as always?
-
User5910 over 6 yearsThis works beautifully HOWEVER it may interfere with the Windows resource compiler (rc.exe) causing the errors like "The system cannot find the path specified." See Visual Studio Shell Isolated project doesn't compile.
-
User5910 over 6 yearsI fixed this by setting checking for the existence of the file first:
if exist "%AppData%\aliases.cmd" "%AppData%\aliases.cmd"
-
Georgii Oleinikov over 5 yearsCould't find a workaround and it seems that using an AutoRun is not the best solution. blogs.msdn.microsoft.com/oldnewthing/20170221-00/?p=95515 I'm just having a shortcut to cmd.exe /k <whatever you want to be executed before cmd start>
-
elig almost 4 yearsIt is worth noting that if you already have something under that key (like Clink for example) you can append commands/scripts via
&&