Automatically executing commands when a command-prompt is opened
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?
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.