Rob van der Woude's Scripting Pages

Batch HowTos

On this page you'll find a shorthand list of common tasks in batch files, and the command(s), variables and/or registrey keys and values that can be used to perform those tasks.
The commands, variables and/or registry keys and values provided are not complete solutions. You may need to experiment with them before actually using them.

Notes: Many, if not most, of the commands listed here require Windows 2000 and later, some require Windows XP or later, some require Active Directory.
Read more detailed information by following the links provided.
  Depending on the Windows version, some commands (e.g. SETX and NETSVC) may require installation of a Resource Kit.

 

Task Available commands, registry keys/values or variables
Add/remove computer to/from domain NETDOM (1)
Counters FOR /L or SET /A counter += 1
Delays SLEEP, TIMEOUT, CHOICE, PING 127.0.0.1 or PING ::1
Current date DATE /T, %Date%, DEBUG or WMIC Path Win32_LocalTime Get Day,Month,Year
Current directory (no trailing backslash except for root directories) CD or %CD%
Current directory with trailing backslash %__CD__%
Current drive %CD:~0,2% or FOR /F %%A IN ("%CD%") DO SET CurDrv=%%~dA
Current time TIME /T, %Time%, DEBUG or WMIC Path Win32_LocalTime Get Hour,Minute,Second
Directory of current batch file (trailing backslash) %~dp0
Drive of current batch file %~d0
Edit a file in its registered program REG
Environment, permanent changes SETX (2)
File size FOR %%A IN (filespec) DO SET FileSize=%%~zA
File timestamp FOR %%A IN (filespec) DO SET TimeStamp=%%~tA
Firewall configuration NETSH
Harddisk & volume management DISKPART, FDISK, FORMAT, CONVERT, MOUNTVOL, VOL, CHKDSK, CHKNTFS, DEFRAG
Hardware inventory WMIC
IP address (local) PING %ComputerName%, NSLOOKUP %ComputerName% or IPCONFIG /ALL
IP address (remote) PING, NSLOOKUP or WMIC NICCONFIG Get IPAddress
IP address (WAN) to file WGET -O output_filename -q http://automation.whatismyip.com/n09230945.asp (3)
IP address (WAN) to file WANIP > output_filename 2>NUL (3)
IP address (WAN) to screen WGET -q -O- http://automation.whatismyip.com/n09230945.asp (3)
IP address (WAN) to screen WANIP (3)
Map network drive NET Use * \\uncpath or PUSHD \\uncpath
Logoff Multiple commands available
Open a file, folder or web page in its registered program START
Permissions CACLS, XCACLS (2), MMC & SECEDIT, SUBINACL (3) or SETACL (3)
Popup dialogs NET Send %ComputerName%
Print text NOTEPAD /P
Print a file with its registered program ASSOC, FTYPE & REGEDIT or REG
Printer management RUNDLL32 PRINTUI.DLL,PrintUIEntry
Process management Multiple commands available
Prompt for user input SET /P & other commands
Random interger %RANDOM%
Read text files FOR /F or occasionaly < redirection
Reboot Multiple commands available
Registry REGEDIT or REG
Service management NET Stop, NET Start, SC or NETSVC (2)
Shutdown Multiple commands available
Test elevated privileges in Windows XP/Vista/7 (tip: Ron Guggisberg) OPENFILES >NUL || ECHO Administrative privileges required
Test if running in Microsoft Virtual Machine HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters
Test if running in Virtual Machine (tip: Emilio Hodge) DMIDecode -s system-product-name (3)
Test if running in Virtual PC WMIC Path Win32_BaseBoard Get Manufacturer,Product
Unicode to ASCII conversion TYPE
User/group management NET, DSADD, DSGET, DSMOD, DSMOVE, DSQUERY & DSRM (1) or CSVDE & LDIFDE (1)
Write text files Redirected ECHO

 

Notes: (1) Requires Windows XP with Windows Server 2003 Administration Tools Pack, or Windows Server 2003 or later.
  (2) Resource Kit tool
  (3) Third party tool

page last modified: 2023-10-30; loaded in 0.0071 seconds