Archive

Posts Tagged ‘batch script time zone’

Script to Check or Set Time Zone

February 3, 2012 Leave a comment

This script can be used to audit and/or set the Time Zone on Windows 2003 Server, Windows 2008, Windows 2008 R2 and Windows 7.  I will add Windows XP and Vista at a later date. The steps are as follows:

  1. Check operating system version and set the %OSVER% variable.
  2. If %OSVER% is not set, warn then exit the script.
  3. Review the current system time zone setting.
  4. Query to change the time zone; allow user to exit the script.
  5. If user chooses to change the time zone, offer US time zones to select from.
  6. If user selects a time zone, run code based on %OSVER%.
  7. If user selects a different time zone, open date/time Control Panel to manually set.
  8. Review the time zone settings again to validate configuration change.

Windows 2008 requires the use of tzone.exe. You can download tzone.exe here.

You can copy and paste the code below or you can download the script here.


@echo off
TITLE Check/Set Time Zone Configuration
COLOR 17

:OSCHK
FOR /F “tokens=4-5 delims=, ” %%g IN (‘wmic os get caption ^|find /I “Windows”‘) DO (IF /I “%%g”==”2003″ (set OSVER=%%g) ELSE (IF /I “%%g”==”2008″ (IF “%%g%%h”==”2008R2″ (set OSVER=%%g%%h) ELSE set OSVER=%%g)))
FOR /F “tokens=3 delims= ” %%g IN (‘wmic os get caption ^|find /I “Windows”‘) DO (IF /I “%%g”==”7″ set OSVER=%%g)

IF /I “%OSVER%”==”” (goto OSWARN) ELSE (goto TZRVW)

:OSWARN
echo.
echo.
echo *************************************
echo WARNING
echo *************************************
echo.
echo This script is designed to run on the following
echo Windows operating systems only:
echo Servers – 2003, 2008, 2008 R2
echo Clients – Windows 7
echo.
goto END

:TZRVW
cls
echo.
echo The time zone on this machine is:
FOR /F “skip=1 tokens=*” %%g IN (‘wmic timezone get description ^|find ” “‘) Do (@echo %%g)
goto TZQRY

:TZQRY
echo.
SET TZQRY=
SET /P TZQRY=Do you need to change the Time Zone? (y/n/q to quit): %=%
echo.
IF /I “%TZQRY%”==”y” (goto TZSEL) ELSE (IF /I “%TZQRY%”==”n” (goto END) ELSE (IF /I “%TZQRY%”==”q” (goto END)))
IF /I NOT “%TZQRY%”==”y” (IF /I NOT “%TZQRY%”==”n” (IF /I NOT “%TZQRY%”==”q” goto TZQRY0))

:TZQRY0
echo.
echo You made an invalid entry – please try again.
echo.
pause
goto TZRVW

:TZSEL
cls
echo.
echo Which Time Zone should this machine have?
echo 1. Pacific Time
echo 2. Mountain Time
echo 3. Arizona
echo 4. Central Time
echo 5. Eastern Time
echo 6. Other

echo.
SET SYSTZ=
SET /P SYSTZ= %=%
IF /I “%SYSTZ%”==”1″ (goto TZPAC) ELSE (IF /I “%SYSTZ%”==”2″ (goto TZMTN) ELSE (IF /I “%SYSTZ%”==”3″ (goto TZARZ) ELSE (IF /I “%SYSTZ%”==”4″ (goto TZCEN) ELSE (IF /I “%SYSTZ%”==”5″ (goto TZEAS) ELSE (IF /I “%SYSTZ%”==”6″ (goto TZOTH))))))
IF /I NOT “%SYSTZ%”==”1″ (IF /I NOT “%SYSTZ%”==”2″ (IF /I NOT “%SYSTZ%”==”3″ (IF /I NOT “%SYSTZ%”==”4″ (IF /I NOT “%SYSTZ%”==”5″ (IF /I NOT “%SYSTZ%”==”6″ (goto TZSEL0))))))

:TZSEL0
echo.
echo You have made an invalid entry – please try again.
echo.
pause
goto TZSEL

:TZPAC
IF /I “%OSVER%”==”2003″ (control timedate.cpl,,/Z Pacific Standard Time) ELSE (IF /I “%OSVER%”==”2008″ (tzone -Zone Pacific Time) ELSE (IF /I “%OSVER%”==”2008R2″ (tzutil /s “Pacific Standard Time”) ELSE (IF /I “%OSVER%”==”7″ (tzutil /s “Pacific Standard Time”))))
goto TZRVW

:TZMTN
IF /I “%OSVER%”==”2003″ (control timedate.cpl,,/Z Mountain Standard Time) ELSE (IF /I “%OSVER%”==”2008″ (tzone -Zone Mountain Time) ELSE (IF /I “%OSVER%”==”2008R2″ (tzutil /s “Mountain Standard Time”) ELSE (IF /I “%OSVER%”==”7″ (tzutil /s “Mountain Standard Time”))))
goto TZRVW

:TZARZ
IF /I “%OSVER%”==”2003″ (control timedate.cpl,,/Z US Mountain Standard Time) ELSE (IF /I “%OSVER%”==”2008″ (tzone -Zone Arizona) ELSE (IF /I “%OSVER%”==”2008R2″ (tzutil /s “US Mountain Standard Time”) ELSE (IF /I “%OSVER%”==”7″ (tzutil /s “US Mountain Standard Time”))))
goto TZRVW

:TZCEN
IF /I “%OSVER%”==”2003″ (control timedate.cpl,,/Z Central Standard Time) ELSE (IF /I “%OSVER%”==”2008″ (tzone -Zone “Central Time (US & Canada)”) ELSE (IF /I “%OSVER%”==”2008R2″ (tzutil /s “Central Standard Time”) ELSE (IF /I “%OSVER%”==”7″ (tzutil /s “Central Standard Time”))))
goto TZRVW

:TZEAS
IF /I “%OSVER%”==”2003″ (control timedate.cpl,,/Z Eastern Standard Time) ELSE (IF /I “%OSVER%”==”2008″ (tzone -Zone Eastern Time) ELSE (IF /I “%OSVER%”==”2008R2″ (tzutil /s “Eastern Standard Time”) ELSE (IF /I “%OSVER%”==”7″ (tzutil /s “Eastern Standard Time”))))
goto TZRVW

:TZOTH
echo.
echo Control Panel will now open so you
echo can set the time zone manually.
start control timedate.cpl
echo.
pause
goto TZRVW

:END
echo Exiting script…
echo.
ping localhost -n 5 >NUL
EXIT

 

Notes:

Since each line of a batch file causes the whole file to be processed again until it reaches the next line, I put as much logic in a single line as possible. That means several nested IF statements where necessary.

The script will open the date/time Control Panel to modify the time zone on Windows 2003 Server.

Windows 2008 Server requires the use of tzone.exe to modify the time zone.

Windows 2008 R2 Server uses native tzutil.exe to modify the time zone.

 

These scripts have been tested and are fully functional on Windows Server 2008, Windows Server 2008 R2 and Windows 2003 Server. They are provided with no guarantee and I assume no responsibility for the use or misuse of these scripts or any issues resulting from their use.