The Ultimate Batch File Template

While batch files are certainly en route to going the way of the dinosaur, they're still my preferred way of scripting automated procedures and routines.

After a few years of various projects, I've finally put together what I believe to be the ultimate template for a versatile batch file, allowing for either self-elevation or running as the current user.

Here's what I've got so far:

@echo off

title Project Title Here
color 9F
mode con: cols=120 lines=44
powershell -command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=120;$B.height1000;$W.buffersize=$B;}"

if /i "%1"=="" goto start
if /i "%1"=="elevated" goto elevated
goto check

:start
cls
echo ==============================
echo.
echo     Welcome to the Project
echo.
echo ==============================
echo.
echo How would you like to run this process?
echo 1. Current User
echo 2. Elevated Session
echo Q. Quit
set choice=
set /p choice=Choice:

if "%choice%"=="1" goto continue
if "%choice%"=="2" goto elevate
if "%choice%"=="q" goto end
if "%choice%"=="Q" goto end
goto start

:elevate
powershell.exe Start-Process -FilePath "$env:comspec" -ArgumentList '/c ""%0"" elevated' -Verb runAs
goto end

:elevated
echo ==============================
echo.
echo     Welcome to the Project
echo.
echo ==============================

:continue
echo.

rem Here we put the main routine of the code. If we'd like to run another
rem routine against this batch file, the :check anchor will take all arguments
rem as long as the first isn't "elevated"

echo Main routine goes here
pause

goto end

:check
rem Here's where individual runnable routines can be stored, to be run as follows:
rem call %0 <arg1> <arg2>

:end

The options at the start of the script will set the title of the Command Prompt window, as well as setting the colour of the foreground and background. The mode line in the header will set the size of the window, however in doing so removes the history from the window, meaning that the user can't scroll back up to see what's happened.

This is remedied by the PowerShell command at the end of the header by changing the buffer size, in this case to 1000 lines, to ensure the user can scroll back up. Commenting out the PowerShell line may also be desirable in some cases.

When the script is first run, you're presented with a menu to either run as the current user, or request elevation. Windows doesn't really allow running as other users, only administrative ones, so selecting elevation will require entering an admin account.

Whether run as the current user or elevated, the :continue anchor will be reached, where the same code will be run in either instance, although you can certainly check for whether the script is running in elevated mode using the same check done on line 9 above.

When the routine is done, the script will automatically jump to the :end anchor, or you can create your own anchors with other menus for branching choices.

One important thing to remember if you choose to make your own menus: run each if statement on its own line in as simple a form as possible - long and complicated if statements, if not met, can immediately fall through the rest of the script, even if more routines are underneath; I don't know why, but it taught me to keep my menu choice if statements simple!

I hope this is useful - for more information on Command Prompt usage and batch files, SS64 is a great resource worth checking out.