S.Siva
S.Siva

Reputation: 2091

Create a batch file with multiple options

Hi I want to create a batch which performs more than one operations. Like it should display

1.Restart
2.Shutdown
3.Close all Windows
4.Log off
5.Switch User

Then "Enter your choice:(User Choice) Then it should perform the operation

Can you help in this?

Upvotes: 27

Views: 132510

Answers (5)

slyfox1186
slyfox1186

Reputation: 350

This script has all of the common shutdown command line options. Pick your choice and enjoy.

@echo off
setlocal

title Shutdown Script
color 0A

set seconds=1

:start
cls
echo.
echo Select a number:
echo.
echo [1] Restart (Default Setting)
echo.
echo [2] Restart Reregister Applications
echo.
echo [3] Restart UEFI/BIOS
echo.
echo [4] Restart Advanced Boot Options
echo.
echo [5] Shutdown (Default Setting)
echo.
echo [6] Shutdown Reregister Applications
echo.
echo [7] Sign Out User
echo.
echo [8] Exit
echo.

choice /c 12345678 /m "Enter your choice:"
if errorlevel 8 goto :end
if errorlevel 7 (
cls
echo.
echo Sign out
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /l
goto error
)

if errorlevel 6 (
cls
echo.
echo Shutdown PC and Re-register any applications on next boot
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /sg /t %seconds%
goto error
)

if errorlevel 5 (
cls
echo.
echo Shutdown PC ^(Default Setting^)
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /s /t %seconds%
goto error
)

if errorlevel 4 (
cls
echo.
echo Restart PC and load the advanced boot options menu
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /r /o /t %seconds%
goto error
)

if errorlevel 3 (
cls
echo.
echo Restart PC to UEFI/BIOS menu
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /r /fw /t %seconds%
goto error
)

if errorlevel 2 (
cls
echo.
echo Restart PC and Re-register any applications
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /g /t %seconds%
goto error
)

if errorlevel 1 (
cls
echo.
echo Restart PC ^(Default Setting^)
choice /c yne /m "Are you sure you want to continue Y or N or [E]xit?"
if errorlevel 3 goto end
if errorlevel 2 goto startover
if errorlevel 1 shutdown /r /t %seconds%
goto error
)

:startover
cls
echo.
echo Restarting script
timeout 2 >nul
goto start

:error
cls
echo.
echo You might be here because of a bad input selection
timeout 2 >nul
echo.
echo Perhaps try another input
endlocal
exit /b

:end
cls
echo.
echo Exiting
timeout 2 >nul
endlocal
exit /b

Upvotes: 0

SmithMart
SmithMart

Reputation: 2801

Now someone just put is this your homework... I almost don't want to paste this! lol

but... I will anyway :p

echo off
:begin
echo Select a task:
echo =============
echo -
echo 1) Option 1
echo 2) Option 2
echo 3) Option 3
echo 4) Option 4
echo -
set /p op=Type option:
if "%op%"=="1" goto op1
if "%op%"=="2" goto op2
if "%op%"=="3" goto op3
if "%op%"=="4" goto op4

echo Please Pick an option:
goto begin


:op1
echo you picked option 1
goto begin

:op2
echo you picked option 2
goto begin

:op3
echo you picked option 3
goto begin

:op4
echo you picked option 4
goto begin

:exit
@exit

You can swap goto begin to goto exit in each of the sections if you want it to run the command then close the batch file, which is what I would recommend, seeing as you want to shutdown.

Hope this helps.

Martyn

Upvotes: 18

Anonymous
Anonymous

Reputation: 61

This way is even better...

@echo off 
title A better way
color 0a
echo.
echo 1.restart
echo 2.shutdown
echo 3.close all windows
echo 4.log off
echo 5.switch user
echo.

set /p a=
IF %a%==1 (put in restart code)
IF %a%==2 (put in shutdown code)
IF %a%==3 (put in close all Windows code)
IF %a%==4 (put in log out code)
IF %a%==5 (put in switch user code)

Upvotes: 6

Steven Schroeder
Steven Schroeder

Reputation: 6194

This should get you started. The CHOICE command is available in most versions of Windows but may require that you get the Windows NT 4 resource kit. The CHOICE command is available in Windows 7.

@ECHO OFF
CLS
ECHO 1.Restart
ECHO 2.Shutdown
ECHO 3.Close all Windows
ECHO 4.Log off
ECHO 5.Switch User
ECHO.

CHOICE /C 12345 /M "Enter your choice:"

:: Note - list ERRORLEVELS in decreasing order
IF ERRORLEVEL 5 GOTO SwitchUser
IF ERRORLEVEL 4 GOTO Logoff
IF ERRORLEVEL 3 GOTO CloseAllWindows
IF ERRORLEVEL 2 GOTO Shutdown
IF ERRORLEVEL 1 GOTO Restart

:Restart
ECHO Restart (put your restart code here)
GOTO End

:Shutdown
ECHO Shutdown (put your shutdown code here)
GOTO End

:CloseAllWindows
ECHO Close All Windows (put your close all windows code here)
GOTO End

:Logoff
ECHO Logoff (put your log off code here)
GOTO End

:SwitchUser
ECHO Switch User (put your switch user code here)
GOTO End

:End

Upvotes: 47

Ajeet  Sinha
Ajeet Sinha

Reputation: 2345

There a a lot of tutorials available on internet for batch programming like http://www.infionline.net/~wtnewton/batch/batguide.html .you can go through it

The commands 1.Restart 2.Shutdown 3.Close all Windows 4.Log off 5.Switch User are available from execution from command like but require admin privileges. you can try this for available options.

shutdown help

on command line.

Upvotes: 0

Related Questions