Skip to content

Instantly share code, notes, and snippets.

@Skibisky
Last active June 12, 2018 04:01
Show Gist options
  • Save Skibisky/402c11589d35f011732e3316b6aa85d4 to your computer and use it in GitHub Desktop.
Save Skibisky/402c11589d35f011732e3316b6aa85d4 to your computer and use it in GitHub Desktop.
Alias for Windows Command Prompt
@echo off
if not exist %~dp0__aliases.bat (echo echo Aliases are Enabled > %~dp0__aliases.bat)
if [%~1] == [] GOTO PRINTOUT
if "%~1" == "_startup_" GOTO DEFINE
if "%~1" == "/remove" GOTO REMOVE
if [%~2] == [] GOTO SEARCH
:: Check to make sure the DOSKEYs are added on every CMD
REG QUERY "HKCU\Software\Microsoft\Command Processor" /v AutoRun >nul 2>&1
if %ERRORLEVEL% == 0 (
reg query "HKCU\Software\Microsoft\Command Processor" /V AutoRun > %TEMP%\alias_temp1.txt
findstr /ri "REG_SZ" %TEMP%\alias_temp1.txt > %TEMP%\alias_temp2.txt
::If the current AutoRun isn't me
for /f "tokens=3" %%a in (%TEMP%\alias_temp2.txt) do (
if not "%%a" == "%~dp0alias.bat" (
echo HKCU\Software\Microsoft\Command Processor\AutoRun to %~dp0alias.bat
)
)
) else (
echo Adding "%~dp0alias.bat _startup_" to HKCU\Software\Microsoft\Command Processor AutoRun
reg add "HKCU\Software\Microsoft\Command Processor" /V AutoRun /t REG_SZ /d "%~dp0alias.bat _startup_" /f
)
::Build and execute the alias, if valid, add to definitions
set addCom=DOSKEY %*
%addCom% > %TEMP%\alias_temp1.txt
findstr "Invalid macro" %TEMP%\alias_temp1.txt >nul
if %ERRORLEVEL%==0 (
echo Invalid alias, try %1=%2...
) else (
echo DOSKEY %* >>"%~dp0__aliases.bat"
)
GOTO DONE
:SEARCH
DOSKEY /macros | findstr %1
GOTO DONE
:REMOVE
DOSKEY %2=
findstr /v "DOSKEY\ %2" %~dp0__aliases.bat > %~dp0__temp.txt
move /y %~dp0__temp.txt %~dp0__aliases.bat >nul
GOTO DONE
:DEFINE
call %~dp0__aliases.bat
GOTO DONE
:PRINTOUT
DOSKEY /macros
:DONE
@Skibisky
Copy link
Author

Put this somewhere in PATH and try it out by calling alias unalias=alias /remove.
Remember you'll need to put $* on the end of macros that take arguments (eg. saythis=echo $*)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment