Skip to content

Instantly share code, notes, and snippets.

@steve-jansen
Created March 8, 2013 17:22
Show Gist options
  • Save steve-jansen/5118149 to your computer and use it in GitHub Desktop.
Save steve-jansen/5118149 to your computer and use it in GitHub Desktop.
A Command Prompt auto-run script to injet DOSKEY macros into the shell from a text file, change the initial working directory to a SUBST'd drive letter, update the PATH to include SysInternal utilities, and log all Command Prompt launches to a history file
:: Name: Startup.Cmd.cmd
:: Purpose: Initializes a Windows command prompt shell
:: Author: [email protected]
:: Revision: April 2012
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
:: self-install this script, but, don't force an overwrite if an auto run script is already configured
IF /I "%~1"=="/install" (
ECHO %~n0: installing %~nx0 as the cmd.exe auto-run script
REG ADD "HKCU\Software\Microsoft\Command Processor" /v "AutoRun" /t REG_EXPAND_SZ /d "^"%~f0^""
)
::CHECK if arguments were passed to cmd.exe, if so exit this script
FOR /F "tokens=2" %%I IN ("%CMDCMDLINE%") DO (
IF NOT "%%I"=="" (
ECHO.[%DATE% %TIME%] - AutoRun Disabled - %CMDCMDLINE% >> "%USERPROFILE%\Documents\Scripts\%~n0.history.txt"
GOTO :EOF
) ELSE (
ECHO.[%DATE% %TIME%] - AutoRun Enabled - %CMDCMDLINE% >> "%USERPROFILE%\Documents\Scripts\%~n0.history.txt"
)
)
:: update the path as needed
PATH %PATH%;%ProgramFiles%\SysInternals;
:: Load the DOSKEY macros from ".txt" file in the same folder and same filename as this script
START "" /B DOSKEY /MACROFILE="%~dpn0.txt"
:: change the working directory
IF EXIST P:\ PUSHD P:\
@ECHO ON
@EXIT /B 0
sql=SQLCMD.exe -S lpc:(local)\SQLEXPRESS -E $*
sql.start=SC START MSSQL$SQLEXPRESS
sql.stop=SC STOP MSSQL$SQLEXPRESS
sql.restart=SC STOP MSSQL$SQLEXPRESS ^&^& SC START MSSQL$SQLEXPRESS
git="C:\Program Files\Git\bin\sh.exe" --login -i $*
vs=IF NOT DEFINED DevEnvDir ("%ProgramFiles%\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" %PROCESSOR_ARCHITECTURE%)
control="%SystemRoot%\system32\control.exe" /Name "Microsoft.$1"
appcmd="%ProgramFiles%\IIS Express\appcmd.exe" $*
wix=PATH | FINDSTR/L /C:"Windows Installer XML v3.5\bin" 1>nul 2>nul || PATH %PATH%;%ProgramFiles%\Windows Installer XML v3.5\bin;
ssdt=PATH | FINDSTR/L /C:"Microsoft SQL Server\110\DAC\bin" 1>nul 2>nul || PATH %PATH%;%ProgramFiles%\Microsoft SQL Server\110\DAC\bin
webdeploy=PATH | FINDSTR/L /C:"Microsoft Web Deploy V3" 1>nul 2>nul || PATH %PATH%;%ProgramFiles%\IIS\Microsoft Web Deploy V3
iis=START "" /B taskkill.exe /im:iisexpress.exe 2>NUL && PING localhost /n 2 >nul && START "" "%ProgramFiles%\IIS Express\iisexpress.exe.lnk" /systray:false /trace:warning /siteid:$*
where=@FOR %I in ($1) DO @(IF NOT "%~f$PATH:I"=="" @ECHO.%~f$PATH:I)&&@FOR %I IN (%PATHEXT%) DO @(FOR %J IN ($1%I) DO @(IF NOT "%~f$PATH:J"=="" @ECHO.%~f$PATH:J))
passwd=notepad "%USERPROFILE%\Documents\notes.txt:notes"
localdb="%PROGRAMFILES%\Microsoft SQL Server\110\Tools\Binn\sqllocaldb.exe" start v11.0&&"%PROGRAMFILES%\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE" -S (localdb)\v11.0 $*
@mikebentley15
Copy link

Thanks for sharing. Your script is pretty useful. I actually found a way to make a batch file the same as the doskey file if you're interested.

It's a hack, but a useful one. See the following:

;=@echo off

;=rem ::
;=rem :: This is both a batch script and a doskey macro file. It uses the hack
;=rem :: that in bash ;= is simply ignored whereas the doskey program thinks it
;=rem :: is the name of a macro. To avoid unintended consequences, we reset the
;=rem :: ; macro at the end of the file.
;=rem ::
;=rem :: I did not come up with this trick, I found it on someone's macro file
;=rem :: online. But I lost that, and remembered the trick. So I don't know
;=rem :: where it came from.
;=rem ::

;=rem :: These are bash commands that are executed. This calls doskey and passes
;=rem :: in itself as the macro file. This is very convenient to store the run
;=rem :: script with the data too.
;=doskey /macrofile=%0
;=goto END

;=rem :: Windows command aliases

cat=type $*
clear=cls $*
cp=copy $*
h=doskey /history
ls=dir $*
mv=move $*
rm=del $*

;=rem :: More aliases
;=rem :: ...

;=rem :: This is the end of the bash file. All of that middle stuff is
;=rem :: skipped by bash
;=rem :: Note: Need to reset the ; to empty at the very end
;=:END
;=@echo on

;=

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