Submitted By: Michael J. Ginter

Backs up system state on a computer. This script can be easily scheduled to run nightly typing the following command at the command line: at TIME /every:m,t,w,th,f,s,su Script File Location. For example: at 18:45 /every:m,t,w,th,f,s,su C:\Scripts\SysState\SysState.bat. The backup file will be over written each time it runs. For non-domain controllers the file size will range from 250MB (Windows 2000) to 600 MB (Windows Server 2003).

Visual Basic
@echo off
REM ======================================================================
REM NAME: SystemState.bat
REM AUTHOR: Michael Ginter 
REM DATE : 9/5/2003
REM UPDATED: 6/6/2006
REM UPDATED: 11/17/2006 - added logging feature
REM COMMENT: Script works on windows XP, 2000, and 2003
REM No additional files required (ie. job files, etc.)
REM Schedule the job to run using AT schedule
REM Example: (will run daily at 6am)
rem at 06:00 /every:su,m,t,w,th,f,s <path>/SysState.bat
REM ======================================================================

REM date /t returns Day_of_Week, Month, Day and year.
REM After this mess, we have min,hh,day,dd,mm and yyyy envariables.

for /f "tokens=1,2,3,4* delims=.:/- " %%i in ('date /t') do (for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (set day=%%i&set %%x=%%j&set %%y=%%k&set yyyy=%%l))
for /f "tokens=1,2* delims=: " %%i in ('time /t') do (set hh=%%i&set min=%%j)

REM default Location of the backlog files
Set LogLoc="%USERPROFILE%\Local Settings\Application Data\Microsoft\Windows NT\NTbackup\Data"

Set BackupLoc=%SYSTEMROOT%\system32\ntbackup.exe

REM Backup Files/Folders/Systemstate List (files or folders could be specified here) 
Set BList=SystemState

REM Backup file Path
Set BDIR=D:\System_State

REM Backup LOG file Path

REM Remove old log files before begining backup
If exist %LOGLOC%\*.log del %LogLoc%\*.log /q >NUL
If exist %LDIR%\*.log del %LDIR%\*.log /q >NUL

REM Backup file Path and location
Set BFile=%BDIR%\%COMPUTERNAME%_SS_Backup.bkf

REM Backup Comments
Set BComment="SystemState %COMPUTERNAME% %yyyy%%mm%%dd%-%hh%:%min%"

REM Backup Job Name
Set BJob="SystemState %COMPUTERNAME% %yyyy%%mm%%dd%-%hh%:%min%"

REM Backup mode (copy, normal, differential, incremental, or daily)
Set BMode=normal

REM Log report type (n[none],s[summary],f[full])
Set LogType=F

REM Create Backup File Path if it does not exist
If Not Exist %BDIR% md %BDIR%

REM Create Backup LOG File Path if it does not exist
If Not Exist %LDIR% md %LDIR%

REM Full command executing the backup. 
REM /R: = restrict access to backup file
REM /RS: backup in remote storage
REM /V: Verify after backup completes
REM /HC: hardware compression
REM example... ntbackup backup systemstate /f <file> /m normal /j <jobname> /d <comment> /r:no /rs:no /v:yes /hc:off /L:F
%BackupLoc% backup %BList% /F %BFile% /m %BMode% /j %BJob% /D %BComment% /R:no /RS:no /V:yes /HC:off /L:%LogType%

If exist %LOGLOC%\*.log for /f %%N IN ('DIR %LOGLOC%\*.log /b') do for /f "tokens=1 delims=." %%I in ('@echo.%%N') do @ren %LOGLOC%\%%N %%I_%COMPUTERNAME%_%yyyy%%mm%%dd%-%hh%%min%.log >NUL
If exist %LOGLOC%\*.log copy %LOGLOC%\*.log %LDIR% >NUL

REM Remove old log files before begining backup
If exist %LOGLOC%\*.log del %LOGLOC%\*.log /q >NUL

REM Any errors during backups will be reported in the event viewer in the Application Log.

REM Clear Variables to clean up data, when script completes
Set BList=
Set BFile=
Set BComment=
Set BJob=
Set BMode=
Set min=
Set hh=
Set day=
Set dd=
Set mm=
set yyyy=