Historically, Windows system administration has been geared through a graphical interface. Though, there is still few options to get the most out of the DOS/CMD command line interface and improve your batch scripts with workaround for some missing commands.
👉 This post includes a collection of legacy posts for Windows Server 2003, Windows XP, and batch scripts. This information is provided as is, for anyone having fun hacking old systems, with no guarantee. Windows XP is an outdated system. Support by Microsoft ended on April 8, 2014. Use the information below at your own risk. For day-to-day use, upgrade to Windows 10 which also support Bash and Linux on Windows.
👉 Some tips below recommend the use of a windows resources kits. It is still available on the Microsoft website for download: Windows Server 2003 Resource Kit
How to do a pause in a batch script?
A useful command line missing on Windows XP is SLEEP or WAIT. There is two possible workarounds. First, is to use a batch script using the PING command. The second solution is to use the Windows Server 2003 Ressource Kit tools.
With PING
you can produce a fixed delay by testing the network loopback address. This can be really useful if you don’t want, or can’t, install additionals software.
:: Doing an approximative pause of 60 seconds
PING -n 61 127.0.0.1>nul
The second way is to install the Windows Server 2003 Resource Kit which provides a SLEEP.exe
command to create pause of a given time.
:: Doing a pause of 60 seconds
SLEEP 60
How to customize your DOS/CMD shell prompt?
In order to customize your DOS/CMD shell prompt on Windows, you need to use the PROMPT
command with the syntax PROMPT [text]
. The command prompt text can be made up of any normal characters (which seems to mean any alphanumeric ASCII characters) and support the following list of special codes.
DOS/CMD Prompt Code | Meaning |
---|---|
$A | & Ampersand |
$B | ` |
$C | ( Left parenthesis |
$D | Current Date |
$E | Escape code (ASCII code 27) |
$F | ) Right parenthesis |
$G | > Greater-than sign |
$H | Backspace (erases previous character) |
$L | < Less-than sign |
$N | Current drive |
$P | Current drive and path |
$Q | = Equal sign |
$S | Space |
$T | Current Time |
$V | Windows version number |
$_ | Carriage return and linefeed |
$$ | $ Dollar sign |
$+ | zero or more plus sign + characters depending upon the depth of the PUSHD directory stack. One character for each level pushed. Available only if Command Extensions are enabled. |
$M | Displays the remote name associated with the current drive letter or the empty string if current drive is not a network drive. Available only if Command Extensions are enabled. |
:: Display prompt help
PROMPT /?
:: Customize your prompt
PROMPT $T $D$_$P $F
:: Generated Prompt Output
17:11:55,31 sam. 21/10/2006
D:\Documents and Settings\John Doe>
How to find a string in a file by using QGREP?
Another missing command on Windows XP is GREP
, which allows to search plain-text files for a string or a regular expression. The Windows Server 2003 Resource Kit provides a GREP-Like command with the DOS command QGREP
.
:: display help
QGREP /?
usage: qgrep [-?BELOXlnzvxy][-e string][-f file][-i file][strings][files] -? - print this message
-B - match pattern if at beginning of line
-E - match pattern if at end of line
-L - treat search strings literally (fgrep)
-O - print seek offset before each matching line
-X - treat search strings as regular expressions (grep)
-l - print only file name if file contains match
-n - print line number before each matching line
-z - print matching lines in MSC error message format
-v - print only lines not containing a match
-x - print lines that match exactly (-BE)
-y - treat upper and lower case as equivalent
-e - treat next argument literally as a search string
-f - read search strings from file named by next argument (- = stdin)
-i - read file list from file named by next argument (- = stdin)
White space separates search strings unless the argument is prefixed with -e, e.g., 'qgrep "all out" x.y' means find either "all" or "out" in x.y, while 'qgrep -e "all out" x.y' means find "all out".
:: Example for UNIX-Like regexp search
QGREP -X "[a-Z]" MYFILE.TXT
How to view a text file content by using the LIST command?
Yet another missing command on Windows XP is LESS which allows to view the contents of a text file from a shell console. Again, with the Windows Server 2003 Resource Kit, you can have a LESS-Like command with the DOS command LIST
. While LIST is not 100% functionally-equivalent to most versions of the Linux/Unix less
command, list
does support text searching and bi-directional scrolling. This command is useful when examining log files such as web server logs, configuration files, and software “read me” files when you don’t want to open a Notepad window.
filename
REQUIRED
- The filename you wish to view-s:STRING
Performs a case insensitive search and highlights the first line matching your search text-g:LINE#
Thelist
display will jump to the specified line number, displaying it at the bottom of the screen. Note thatlist
starts its line numbering from line 0.
While running the
list
command, here are a few of the functions you may perform:Cursor keys
Use the cursor keys to scroll through the file. If the file is too wide to view onscreen at once, the left and right cursor keys will be active.Page Up, Page Down Keys Scrolls through the text file up or down one page at a time
Home, End Keys Jumps to the beginning or end of the file
W
Toggles text word-wrapQ or the ESC key Quit the
list
display/ Perform a case-sensitive search of the string you specify
\ Perform a case-insensitive search of the string you specify
n or F3
Searches for the next match to the search stringN Searches for the previous match to the search string
G Jump to the specified line number
F Open a new file without going back to the DOS prompt
? Displays a full list of commands
Note that by entering more than one filename after the
list
command you can view more than one file. Although only one file is displayed at a time, the following commands switch between files specified on the command line:CTRL PAGE-DOWN Displays the next file specified on the command line, or wraps to the first file.
CTRL PAGE-UP Displays the previous file specified on the command line, or wraps to the last file.
How to display the tail end of a text file?
Yet another missing command on Windows XP is TAIL which allows to view the tail end of a text file from a shell console. Again, with the Windows Server 2003 Resource Kit, you can get a DOS version of the tail
command.
:: display help
tail /?
:: display last ten lines of a file
tail FILENAME
:: display last thirty lines of a file
tail -30 FILENAME
:: keep accessing file, displaying new lines as necessary.
tail -f FILENAME
How to deactivate Windows XP firewall on startup?
The Windows XP firewall is automatically started when a new software is installed or during the windows update to SP2. In such cases you can lost your remote access like when using a VNC connection. While it is not recommended to disable your firewall on Windows, you may have have a need to disable it when running on a secured network to prevent the lost of control of the host after an update or a new install.
To keep your host available every time it reboot, add a batch file in the Start-up directory with the following line NETSH FIREWALL SET OPMODE DISABLE
.
How to solve files and directories permissions on Windows XP using CACLS?
When using network share on Windows XP you hit access restriction errors that prevent you to access certain files or folders. You can fix the permissions issues with the CACLS
command which can displays or modifies discretionary access control list (DACL) files.
cacls FileName [/t] [/e] [/c] [/g User**:permission] [/r** User […]] [/p User**:permission […]] [/d** User […]] **FileName : **Required. Displays DACLs of specified files.
**/t : **Changes DACLs of specified files in the current directory and all subdirectories.
**/e : **Edits a DACL instead of replacing it.
**/c : **Continues to change DACLs, ignoring errors.
/g User : permission : Grants access rights to the specified user. The following table lists valid values for permission.
**/r User : **Revokes access rights for the specified user.
**/p User : permission : **Replaces access rights for the specified user. The following table lists valid values for permission.
**/d User : **Denies access for the specified user.
**/? : **Displays help at the command prompt.Permission possible values :
n : None r : Read w : Write c : Change (write) f : Full control
CACLS
accept more than one user and you can use widlcards (? and *) to specify multiple files.
How to determine which Windows OS version is running with a batch script?
Many DOS commands in the 32-bit versions of Windows are similar but support different parameters, or use different registry key name. Thus, if you wish to write a batch file that can run on different types of machines, it may prove beneficial to determine the version of Windows on which the batch file is running. This way the batch file can execute commands appropriate to the operating system.
The simpliest way to know the Windows Version is to run the universal Windows DOS command ver
in a for
loop and then fallback with using find
to parse the returned output for a string match.
You can easily add the support of other versions of Windows as necessary or set an environment variable based on the version of Windows detected. You can any missing version based on this
full list of windows version which contains the complete ver
output.
@echo off
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if %VERSION% == "5.0" goto v2000
if %VERSION% == "5.1" goto xp
if %VERSION% == "5.2" goto xppro
if %VERSION% == "6.0" goto vista
if %VERSION% == "6.1" goto win7
if %VERSION% == "6.2" goto win8
if %VERSION% == "6.3" goto win8.1
if %VERSION% == "6.4" goto win10
if %VERSION% == "10.0" goto win10
ver | find "2003" > nul
if %ERRORLEVEL% == 0 goto v2003
ver | find "NT" > nul
if %ERRORLEVEL% == 0 goto nt
echo Unknow OS Version.
goto exit
:v2003
:: Run Windows 2003 specific commands here.
goto exit
:xp
:: Run Windows XP specific commands here.
goto exit
:v2000
:: Run Windows 2000 specific commands here.
goto exit
:nt
:: Run Windows NT specific commands here.
goto exit
:vista
:: Run Windows Vista specific commands here.
goto exit
:win7
:: Run Windows 7 specific commands here.
goto exit
:win8
:: Run Windows 8 specific commands here.
goto exit
:win10
:: Run Windows 10 specific commands here.
goto exit
:exit