DESKTOP

Windows Vista : Automating Recurrent Tasks (part 1) - Working with the Command Prompt

7/27/2012 11:41:17 AM
As you can see, both WinRS and Telnet are character-based commands that do not rely on the graphical user interface to operate. The advantage of using character-based command interpreters is that they can be captured into batch or command files and can then be used to automate tasks. You can of course, automate tasks in the graphical interface through the Task Scheduler, but being able to trap commands into command files makes it easier to perform multiple tasks at once.

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.

Table 1. Windows Vista Commands
CommandNew or Updated in VistaDescriptionCorresponding GUI Tool
ASSOC Displays or modifies file extension associations.Windows Explorer
ATTRIB Displays or changes file attributes.Windows Explorer
 AUDITPOLModifies audit policies.Security Templates Security and Configuration Analysis
 BCDEDITSets 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
 CHANGESets 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
 CHGLOGONControls session logins. 
 CHGPORTControl COM port mappings for DOS application compatibility. 
 CHGUSRChange 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. 
 CHOICELets you select one item from a list of choices and returns the result.. 
 CLIPRedirects 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
 CMDKEYControls 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
 DISKRAIDUsed to access the Diskraid command window.Computer => Disk Management
 DISPDIAGDisplays 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
 EXPANDAllows 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. 
 FORFILESUsed 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
 ICACLSDisplay, modify, backup, or restore ACLs for files and directories.Windows Explorer
IF Performs conditional processing in batch programs. 
 ISCSICLIInitiates 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
 MKLINKCreates 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
 MUIUNATTENDControl 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
 NETCFGNetwork installer for Windows PE (WinPE).Control Panel => Network and Internet
 OCSETUPWindows 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. 
 PKMGRWindows package manager. 
 PNPUNATTENDUnattended online driver installation. 
 PNPUTILMicrosoft PnP utility 
POPD Restores the previous value of the current directory saved by PUSHD. 
PRINT Prints a text file. 
PROMPT Changes the Windows Command Prompt. 
PUSHD Saves the current directory then changes it. 
 QUERYUsed to query information about Terminal Services sessions. Can query users, processes, terminal servers or sessions.Terminal Services Manager
 QUSERDisplays 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
 ROBOCOPYAdvanced utility to copy files and directory trees.Windows Explorer
 RPCPINGPings 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. 
 SETXControls 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
 SXSTRACEWindows side-by-side (WinSxS) tracing tool. 
 TAKEOWNControls 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
 TIMEOUTControls 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. 
 TRACERPTUsed 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
 WAITFORUsed to send, or wait for, a signal on a system. 
 WBADMINControls backups and restores.Backup and Restore Center
 WCEUTILControls the Windows event collector.Event Viewer
 WEVTUTILControl Windows events.Event Viewer
 WHEREUsed to display the location of files matching a given search.Search
 WHOAMIGets 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
 WINRSLaunches the Windows remote shell. 
 WINSATLaunches 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:

  1. Choose Start Menu => Search => Notepad to open the Notepad.

  2. Type uptime \\PCname and repeat for each PC. Make sure each command is on its own line.

  3. 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.

  4. Name the file UptimeReport.cmd and save it under your Documents folder.

  5. 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.

  1. 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.

  2. 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.

  3. Close Notepad.

  4. 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.

  5. 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.

Other  
  •  Windows Vista : Administering Workstations - Performing Remote PC Administration
  •  Windows Server 2003 : Windows Terminal Services - Command-Line Utilities
  •  Windows Server 2003 : Terminal Services Administration (part 2) - Terminal Services Configuration
  •  Windows Server 2003 : Terminal Services Administration (part 1) - Terminal Services Manager
  •  Windows Server 2003 : Windows Terminal Services - Installing an Application, Configuring Terminal Services Licensing
  •  NAS Devices: The Storage Centers (Part 5) - Synology DS212+, Thecus N4800
  •  NAS Devices: The Storage Centers (Part 4) - Qnap TS-219P II Turbo NAS, QNap TS-412 Turbo NAS
  •  NAS Devices: The Storage Centers (Part 3) - D-Link ShareCenter Shadow DNS-325, NetGear ReadyNAS Duo v2
  •  NAS Devices: The Storage Centers (Part 2) - Iomega StorCenter ix2 Network Storage Cloud Edition, Western Digital My Book Live Duo 4TB
  •  NAS Devices: The Storage Centers (Part 1) - Buffalo LinkStation Pro 2TB, Freecom SilverStore 2-Drive NAS 2TB
  •  Windows Server 2008 R2 : Work with Remote Clients (part 2) - Install and Configure DirectAccess
  •  Windows Server 2008 R2 : Work with Remote Clients (part 1) - Install and Configure Windows Server 2008 R2 VPNs
  •  Windows Server 2008 R2 : Manage Remote Desktop Services (part 4) - Working with Virtual Desktop Infrastructure
  •  Windows Server 2008 R2 : Manage Remote Desktop Services (part 3) - Configure Remote Desktop Web Access
  •  Windows Server 2008 R2 : Manage Remote Desktop Services (part 2) - Configure Remote Desktop Gateway, Configure Remote Desktop Connection Broker
  •  Windows Server 2008 R2 : Manage Remote Desktop Services (part 1) - Administer Remote Desktop Session Host
  •  WD My Book Thunderbolt Duo 4TB
  •  Thunderbolt Storage (Part 3)
  •  Thunderbolt Storage (Part 2)
  •  Thunderbolt Storage (Part 1)
  •  
    Top 10
    Telephoto Zoom Lens : Sigma 50-150mm f/2.8 EX DC DS HSM
    Adobe Creative Suite 6 Software Reviews
    Bring Your Own Confusion (Part 2)
    Bring Your Own Confusion (Part 1)
    Discover The Windows Store
    Extremely Overclockable. Extremely Innovative
    G Data Total Protection 2013 - Innovative Fingerprinting Technique
    6 Pack: TB portable hard drives
    AMD Trinity: Intel-Beating Gaming Power
    Hot List: Photo printing – Snap Happy – Backup Storage – November 2012
    Most View
    Asus Rog Tytan CG8565 - Clash Of The Tytan
    Android Security Tools
    System Center Configuration Manager 2007 : Architecture Design Planning - Software Update Planning
    100-inch-projector-intergrated Sony HDR-PJ600VE Camera
    Programming the iPhone : Standard Control Types (part 1) - Buttons
    The Future Of Apple: Chip Off The Block (Part 6)
    HP ENVY 14 Spectre – The Spectral Beauty
    Ultrabook Supertest (Part 7) - Lenovo U310
    Ultrabooks To Get Touchscreens & Voice Recognition
    CorelDraw 10 : Basic Printing - To print multiple copies of a document & To print specific pages or objects
    All Abroard Travelng in a High-Tech World
    Programming the Mobile Web : Mobilizing WordPress and Other CMSs
    Configuring Windows 7 NIC Devices (part 1) - Configuring a Network Adapter & Troubleshooting a Network Adapter
    Leveraging and Optimizing Search in SharePoint 2010 : Uninstalling FAST Search Server 2010 for SharePoint
    Installing and Maintaining Devices in Vista: The Essentials
    SQL Server 2008 : Developing with LINQ to SQL (part 2) - Uncovering LINQ to SQL with Linqpad
    Exchange Server 2010 : Keep Exchange Healthy (part 1) - Ensure That Mail Flows Freely
    Building a WPF Application without XAML (part 1)
    SharePoint 2010 : Implementing Authentication Scenarios
    Oracle Coherence 3.5 : Planning Your Caches - Anatomy of a clustered cache