Windows Vista : Automating Recurrent Tasks (part 1) - Working with the Command Prompt
In Vista, you can automate operations by using three methods. Of course, there are more, but they often require third-party tools to execute. Two of the methods are available by default and the third must be downloaded and installed separately. They include:
The Command Prompt: A built-in character-based command interpreter
Microsoft PowerShell: A new downloadable command interpreter based on the .NET Framework
The Task Scheduler: A powerful automation engine.
Ideally, you will create command files either in the Command Prompt or in PowerShell, and then you can use the Task Scheduler to schedule these tasks to run automatically whenever you need them.
1. Working with the Command Prompt
The Windows Vista Command Prompt has been greatly enhanced since Windows XP. It now boasts more than 120 commands with 40 new or updated commands. This gives the command line quite a bit of power. For example, if you want to see the network configuration of a system in one quick way, all you do is open a Command Prompt and type ipconfig/all. This will automatically list all of the network connections with each one of the settings for each connection.
Commands usually require switches to modify their operation and operators to indicate the object on which you want to run the command. The easiest way to discover information about each command is to simply open a Command Prompt and type:
commandname /?
Doing this automatically displays help information about the command you list. In some cases, commands include sub-items. For information on using these subitems, type the command, the subitem and then the help request (/?).
Table 1 lists the available commands in Windows Vista, updated or new commands, and where possible, the corresponding graphical interface tool. Use this table to identify which commands you need to work with.
| Command | New or Updated in Vista | Description | Corresponding GUI Tool |
|---|---|---|---|
| ASSOC | Displays or modifies file extension associations. | Windows Explorer | |
| ATTRIB | Displays or changes file attributes. | Windows Explorer | |
| AUDITPOL | Modifies audit policies. | Security Templates Security and Configuration Analysis | |
| BCDEDIT | Sets properties in boot database to control boot loading. | Computer System Properties | |
| BITSADMIN | Administers the Background Intelligent Transfer Service (BITS), the service which is used to download updates to PCs. | ||
| BREAK | Sets or clears extended CTRL+C checking. | ||
| CACLS | Displays or modifies access control lists (ACLs) of files. | Windows Explorer | |
| CALL | Calls one batch program from another. | ||
| CD | Displays the name of or changes the current directory. | Windows Explorer | |
| CHANGE | Sets special terminal server modes for logons, COM port mappings, and software installations. | Add or Remove Programs for Terminal Server | |
| CHCP | Displays or sets the active code page number. | ||
| CHDIR | Displays the name of or changes the current directory. | Windows Explorer | |
| CHGLOGON | Controls session logins. | ||
| CHGPORT | Control COM port mappings for DOS application compatibility. | ||
| CHGUSR | Change application installation mode. | Add or Remove Programs for Terminal Server | |
| CHKDSK | Checks a disk and displays a status report. | Computer => Disk Management | |
| CHKNTFS | Displays or modifies the checking of disk at boot time. | ||
| CHOICE | Lets you select one item from a list of choices and returns the result.. | ||
| CLIP | Redirects output from the command line to the clipboard. | Clipboard | |
| CLS | Clears the screen. | Show Desktop Icon | |
| CMD | Starts a new instance of the Windows command interpreter. | Command Prompt | |
| CMDKEY | Controls stored user names and passwords. | ||
| COLOR | Sets the default console foreground and background colors. | ||
| COMP | Compares the contents of two files or sets of files. | Windows Explorer | |
| COMPACT | Displays or alters the compression of files on NTFS partitions. | Computer => Disk Management | |
| CONVERT | Converts FAT volumes to NTFS. You cannot convert the current drive. | Computer => Disk Management | |
| COPY | Copies one or more files to another location. | Windows Explorer | |
| DATE | Displays or sets the date. | Control Panel => Date and Time | |
| DEL | Deletes one or more files. | Windows Explorer | |
| DIR | Displays a list of files and subdirectories in a directory. | Windows Explorer | |
| DISKCOMP | Compares the contents of two floppy disks. | Windows Explorer | |
| DISKCOPY | Copies the contents of one floppy disk to another. | Windows Explorer | |
| DISKPART | Displays or configures Disk Partition properties. | Computer => Disk Management | |
| DISKRAID | Used to access the Diskraid command window. | Computer => Disk Management | |
| DISPDIAG | Displays diagnostics. | System Configuration | |
| DOSKEY | Edits command lines, recalls Windows commands, and creates macros. | ||
| DRIVERQUERY | Displays current device driver status and properties. | Device Manager | |
| ECHO | Displays messages, or turns command echoing on or off. | ||
| ENDLOCAL | Ends localization of environment changes in a batch file. | ||
| ERASE | Deletes one or more files. | Windows Explorer | |
| EXPAND | Allows the extraction of Microsoft Update files (.MSU) and launches the Microsoft Update Standalone Installer. | Windows Explorer | |
| EXIT | Quits the CMD.EXE program (command interpreter). | ||
| FC | Compares two files or sets of files, and displays the differences between them. | ||
| FIND | Searches for strings in files. | Search | |
| FINDSTR | Searches for a text string in a file or files. | Search | |
| FOR | Runs a specified command for each file in a set of files. | ||
| FORFILES | Used to select a file or files to execute a command on it; used mostly in batch jobs | ||
| FORMAT | Formats a disk for use with Windows. | Computer => Disk Management | |
| FSUTIL | Displays or configures the file system properties. | Windows Explorer | |
| FTYPE | Displays or modifies file types used in file extension associations. | Windows Explorer | |
| GOTO | Directs the Windows command interpreter to a labeled line in a batch program. | ||
| GPRESULT | Displays Group Policy information for machine or user. | Group Policy Management Console | |
| GPUPDATE | Updates Group Policy Settings for machine or user. | Group Policy Management Console | |
| GRAFTABL | Enables Windows to display an extended character set in graphics mode. | ||
| HELP | Provides Help information for Windows commands. | Help and Support | |
| ICACLS | Display, modify, backup, or restore ACLs for files and directories. | Windows Explorer | |
| IF | Performs conditional processing in batch programs. | ||
| ISCSICLI | Initiates iSCSI. | iSCSI Initiator | |
| LABEL | Creates, changes, or deletes the volume label of a disk. | Computer => Disk Management | |
| MD | Creates a directory. | Windows Explorer | |
| MKDIR | Creates a directory. | Windows Explorer | |
| MKLINK | Creates Symbolic Links and Hard Links. | ||
| MODE | Configures a system device. | Device Manager | |
| MORE | Displays output one screen at a time. | ||
| MOVE | Moves one or more files from one directory to another directory. | Windows Explorer | |
| MUIUNATTEND | Control Multiple User Interface (MUI) unattend actions. | ||
| NET | Controls service status, adds users or groups, changes network configuration, shares folders, and more. | Control Panel => Network and Internet | |
| NETCFG | Network installer for Windows PE (WinPE). | Control Panel => Network and Internet | |
| OCSETUP | Windows optional component setup. | Control Panel => Programs => Turn Windows features on or off | |
| PATH | Displays or sets a search path for executable files. | ||
| PAUSE | Suspends processing of a batch file and displays a message. | ||
| PKMGR | Windows package manager. | ||
| PNPUNATTEND | Unattended online driver installation. | ||
| PNPUTIL | Microsoft PnP utility | ||
| POPD | Restores the previous value of the current directory saved by PUSHD. | ||
| Prints a text file. | |||
| PROMPT | Changes the Windows Command Prompt. | ||
| PUSHD | Saves the current directory then changes it. | ||
| QUERY | Used to query information about Terminal Services sessions. Can query users, processes, terminal servers or sessions. | Terminal Services Manager | |
| QUSER | Displays information about users logged on to the system through Terminal Services. | Terminal Services Manager | |
| RD | Removes a directory. | Windows Explorer | |
| RECOVER | Recovers readable information from a bad or defective disk. | Computer => Disk Management | |
| REM | Records comments (remarks) in batch files or CONFIG.SYS. | ||
| REN | Renames a file or files. | Windows Explorer | |
| RENAME | Renames a file or files. | Windows Explorer | |
| REPLACE | Replaces files. | Windows Explorer | |
| RMDIR | Removes a directory. | Windows Explorer | |
| ROBOCOPY | Advanced utility to copy files and directory trees. | Windows Explorer | |
| RPCPING | Pings a server using remote procedure call (RPC). | ||
| RUNAS | Changes the credentials used to execute a command. Note: This command cannot generate an elevated status like the Run As Administrator command can. In Vista, it can only be used to lower credentials, not elevate them. | Run As Administrator | |
| SC | Displays or configures services (background processes). | Services | |
| SCHTASKS | Schedules commands and programs to run on a computer. | Task Scheduler | |
| SECEDIT | To work with security templates and update security configurations. | Security Templates Security and Configuration Analysis | |
| SET | Displays, sets, or removes Windows environment variables. | ||
| SETLOCAL | Begins localization of environment changes in a batch file. | ||
| SETX | Controls environment variables in the user or system environment. | ||
| SHIFT | Shifts the position of replaceable parameters in batch files. | ||
| SHUTDOWN | Allows proper local or remote shutdown of machine. | Start Menu => Shutdown | |
| SORT | Sorts input. | ||
| START | Starts a separate window to run a specified program or command. | Windows Explorer | |
| SUBST | Associates a path with a drive letter. | Computer => Disk Management | |
| SYSTEMINFO | Displays machine specific properties and configuration. | System Information | |
| SXSTRACE | Windows side-by-side (WinSxS) tracing tool. | ||
| TAKEOWN | Controls file ownership. | Windows Explorer | |
| TASKKILL | Kill or stop a running process or application. | Task Manager | |
| TIME | Displays or sets the system time. | Control Panel => Date and Time | |
| TIMEOUT | Controls wait times in batch files. | ||
| TITLE | Sets the window title for a CMD. EXE session. | ||
| TRACERT | Determines the path taken to a destination by sending Internet Control Message Protocol (ICMP) Echo Request messages to the destination. | ||
| TRACERPT | Used to generate trace reports. | Event Viewer | |
| TREE | Graphically displays the directory structure of a drive or path. | Windows Explorer | |
| TYPE | Displays the contents of a text file. | Notepad | |
| VER | Displays the Windows version. | System Information | |
| VERIFY | Tells Windows whether to verify that your files are written correctly to a disk. | ||
| VOL | Displays a disk volume label and serial number. | Computer => Disk Management | |
| XCOPY | Copies files and directory trees. | Windows Explorer | |
| WAITFOR | Used to send, or wait for, a signal on a system. | ||
| WBADMIN | Controls backups and restores. | Backup and Restore Center | |
| WCEUTIL | Controls the Windows event collector. | Event Viewer | |
| WEVTUTIL | Control Windows events. | Event Viewer | |
| WHERE | Used to display the location of files matching a given search. | Search | |
| WHOAMI | Gets user name and group information along with security identifiers (SID), privileges, logon identifier (logon ID) for the current user (access token) on the local system. | System Information | |
| WINRS | Launches the Windows remote shell. | ||
| WINSAT | Launches the Windows system assessment tool. | ||
| WMIC | Displays WMI information inside interactive command shell. |
There are several ways to work with the Command Prompt. Usually, you need to access an elevated Command Prompt to perform a given operation. Remember that to do this, you need to right-click on the shortcut in your Quick Launch area and select Run As Administrator. This way, everything you run from the Command Prompt will be elevated.
Creating command files
In addition, you'll want to run multiple commands in a series. For example, you can run uptime reports on a series of PCs. To do this, you use the uptime command. This tool is designed specifically to report on system uptime.
Using this tool and a little ingenuity, you can produce your uptime reports automatically. Basically, you need to run the following command:
uptime \\PCname
To run a report on multiple computers, you would repeat the line one after another. To simplify the process, put these commands into a command file:
Choose Start Menu => Search => Notepad to open the Notepad.
Type uptime \\PCname and repeat for each PC. Make sure each command is on its own line.
When you're done, choose File => Save. In the Save dialog box, use the drop-down list under Save as type to choose All Files. You can change the extension of the text file; otherwise all files are created with the TXT extension.
Name the file UptimeReport.cmd and save it under your Documents folder.
To generate a report, open an elevated Command Prompt, change folders to your Documents folder, and type:
UptimeReport
This will produce a report of uptime on each PC in the list.
Now, you'll find that this report isn't so useful because it produces output on the screen and you need to review the screen to see the results of each PC. This is why you should use the pipe (>) command. Using the pipe command will automatically output the results of your command into a text file so that you can review them through Notepad. This makes it a lot easier to verify report outputs. You can even schedule the task in Task Scheduler and review the resulting output at your leisure. Use the following structure to pipe output into a text file:
UptimeReport >UptimeReportDate.txt
You include the date into the title of the file so that you can differentiate output reports.
Creating an elevated Command Prompt Here hack
One of the most annoying things with the Command Prompt is the location it leaves you in when you launch it. If you need to get to another location in your folder structure, you often have to use the cd command several times to get to the appropriate location. This takes time, time you don't have when dealing with issues. Ideally, you should be able to navigate to the proper location through Windows Explorer and then, when you're there, generate a Command Prompt, even an elevated Command Prompt, in that location. For this, you need a registry hack.
Use Notepad to type the following text into a text file:
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\cmd] @="Comm&and Prompt here" "NoWorkingDirectory"="" "Extended"=- [HKEY_CLASSES_ROOT\Directory\shell\cmd\command] @="cmd.exe /k \"pushd %L && title Command Prompt\"" [HKEY_CLASSES_ROOT\Directory\shell\runas] @="Ele&vated Comm&and Prompt here" "NoWorkingDirectory"="" [HKEY_CLASSES_ROOT\Directory\shell\runas\command] @="cmd.exe /k \"pushd %L && title Command Prompt\"" [HKEY_CLASSES_ROOT\Drive\shell\cmd] @="Comm&and Prompt here" "NoWorkingDirectory"="" "Extended"=- [HKEY_CLASSES_ROOT\Drive\shell\cmd\command] @="cmd.exe /k \"pushd %L && title Command Prompt\"" [HKEY_CLASSES_ROOT\Drive\shell\runas] @="Ele&vated Comm&and Prompt here" "NoWorkingDirectory"="" [HKEY_CLASSES_ROOT\Drive\shell\runas\command] @="cmd.exe /k \"pushd %L && title Command Prompt\""
NOTE
Make sure you type in the text displayed here exactly as it appears. Double-check your text after you typed it. Making Registry changes is a tricky proposition at best so make sure you do it right.
Save the text file as CommandPromptHere.reg. Make sure you use the All Files option in the Save dialog box, otherwise your file will also have a .TXT extension. You use the .REG extension because it is the extension used to update Registry settings through text files.
Close Notepad.
Now, double-click the CommandPromptHere.reg file through Windows Explorer. Accept the UAC prompt and then accept the Registry Editor warning. Click OK when the Registry has been updated.
Now, you can go to any folder in Windows Explorer, and right-click it to select either Command Prompt Here or Elevated Command Prompt Here from the context menu, as seen in Figure 1.
Figure 1. Using a Registry Hack to create Command Prompt Here options
Now you can save time by being able to get the Command Prompt exactly where you need it.