You can't do this directly at the command line, but you can do it using a doskey
macrofile. Macrofiles don't even need to use confusing magic like $g
; they're not part of the shell, so shell special characters can be used normally, and get included in the definition of the macro instead of being interpreted by the shell before the macro gets defined.
Create a file wherever you like (e.g. %USERPROFILE%\mymacros.txt
), and put the following line in it:
logged_build=build >build.log 2>&1
Then load the macros by running:
doskey /MACROFILE=%USERPROFILE%\mymacros.txt
You can put many macros in the file to load them all at once too; this makes it easy to customize your command prompt in general too; you can either modify the existing Command Prompt
shortcut or create a new shortcut based on cmd.exe
to make the Target
:
%windir%\system32\cmd.exe /K doskey /MACROFILE=%USERPROFILE%\mymacros.txt
and clicking the shortcut will create a command prompt with all of the macros pre-loaded. The /K
option to cmd.exe
runs the subsequent command in the shell before giving the user an interactive prompt. Saves a lot of hassle if your prompts define all your macros automatically without having to set them up each time.
Alternatively, to avoid needing to modify individual shortcuts, you can set a registry key that will load the macros no matter what, even if cmd.exe
is invoked directly, without going through a modified shortcut. Just run:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "doskey /MACROFILE=%USERPROFILE%\mymacros.txt"
You can change HKCU
to HKLM
to make it apply globally for all users, not just yourself, though in that case you'd want to put the macro file in a common location, not your user profile. Annoyingly, you can't use a REG_EXPAND_SZ
for cases like this (which would allow you to use variables like %USERPROFILE%
to set a global HKLM
setting to files relative to each user's profile directory, or handle the case of a profile being relocated), but it works well enough.