Creating a Batch File
A batch file is a listing of one or more command-prompt commands within a text file. When the batch file is called or executed, the commands are executed. The best way to understand this is to do it. While there are sophisticated text editors you can use, Notepad will work.
The steps used to create a simple batch file, and then it builds on the simple batch to add extra capabilities. Ultimately, you'll end up with a batch file that can create a list of patches installed on the system and that can copy the file to a share on another computer.
Creating a Batch File
- Launch the command prompt
- Type Notepad ListPatches.bat and press Enter Notepad will launch, and because a
file named ListPatches.bat doesn't exist, you'll be prompted to create it Click Yes.
Note that the file will be created in the same directory in which the Command Prompt window was launched - Type in the following text in Notepad:
Echo Off
Echo Hello %username%. Today is %date%.
Press Ctrl+S to save the fi le, but don't close it. - Return to the command prompt, type ListPatches, and press Enter. Notice that since the batch file is considered one of the executable types, it is automatically located and executed. You'll see a greeting with today's date. This is okay but not very useful.
- Access Notepad, and type in the following text after your first two lines:
Wmic qfe > %computerName%patches.txt
This will create a list of updates currently installed on this system and store the updates in the file named computerNamepatches.txt, where the computer name will be different for each computer where it is executed. Press Ctrl+S to save the file. - Return to the command prompt, press the up arrow, and press Enter to execute the batch file again. Notice that it almost seems as though it's the same as before. A greeting appears, it pauses for a second or two, and then the command prompt returns.
- Provide some user feedback by adding the following lines to the batch file:
Echo A list of patches is stored in the %computername%patches.txt file.
Press Ctrl+S to save the file. - Access the command prompt, press the up arrow to retrieve the last command, and press Enter to view the difference. Notice that instead of %computername%, your actual computer name is used.
- You could also open the file for the user by adding this command to the batch file:
Notepad %computername%patches.txt
If you add this to the batch file to test it, make sure you remove it before moving on. - Last, if you wanted to copy it to a network share (such as a central computer that will
hold files from multiple computers), you could use the NET USE command. For this
set of commands, I'm assuming I have a share named Patches on a server named
Srv1 that I can access in the network and I have permissions to copy the file. I am
accessing it using the \\Srv1\Patches UNC path. You can use any server (or another
Windows 7 box) and any share that has appropriate permissions.
Net Use Z: /delete Net use z: \\SRV1\Patches Copy %computerName%Patches.txt Z: net use Z: /delete
- The first command ensures that the Z: drive isn't already mapped to something else. The next command maps the Z: drive to the UNC path using the \\serverName\ shareName format. The third line copies the file to the Z: drive using the Copy command, and the fourth line returns the environment to normal.
Now that the file is created, it can be configured to execute automatically based on a schedule. Windows 7 includes the built-in Task Scheduler that can be used to schedule tasks.
In this tutorial:
- Working with the Command Prompt
- Starting and Ending a Command Prompt Session
- Easy ways to invoke administrator Command Prompt sessions
- Starting Command Prompt at a Particular Folder
- Strings with Spaces Need Quotes
- Cmd.exe vs. Command.com
- Commands Are Not Case Sensitive
- Starting Command Prompt and Running a Command
- Cmd.exe and Other Command Prompts
- Using AutoRun to Execute Commands When Command Prompt Starts
- Using Cmds Command-Line Syntax
- Using Commands
- Type /? for help
- Starting Programs
- Open Windows Explorer at the current Command Prompt folder
- Using File-Name and Folder-Name Completion
- Use a different completion character
- Using Wildcards
- Editing the Command Line
- Using Command Symbols
- The Redirection Symbols
- The Pipe Symbol
- The Command Combination Symbols
- Pausing or Canceling Commands
- Simplifying Command Entry with Doskey Macros
- DOSKEY Saves Typing
- System Variables Identify the Environment
- Viewing Environment Variables
- Modifying Environment Variables
- Predefined Environment Variables
- Customizing Command Prompt Windows
- Setting the Window Size and Position
- Setting the Window Size and Position Visually
- Selecting a Font
- Setting Colors
- Setting Other Options
- Copy and paste in the command prompt window
- Navigating from the command prompt
- Printing a list of filenames
- Commands Use Paths
- Identifying Executables
- Modifying the Path to Executables
- Modifying the Path with the GUI
- Changing the Current Path with CD
- Changing the Current Path with Windows Explorer
- Capturing the Output
- A Sampling of Commands
- Dir
- Copy
- XCopy
- SET
- NET USE
- SystemInfo
- DriverQuery
- Echo
- Advanced Shell Commands
- Creating a Batch File
- Scheduling a Batch File
- Creating Scheduled Tasks with a Script
- Using Windows PowerShell and the PowerShell ISE
- Windows PowerShell ISE
- PowerShell Commands
- Verbs and Nouns
- Sending Output to a Text File
- PowerShell Syntax
- Variables Created with a $ Symbol
- Comparison Operators
- Parentheses, Brackets, and Braces
- Running PowerShell Scripts
- PowerShell Execution Policy
- Changing the Execution Policy
- Looping
- Collections
- Creating a PowerShell Script
- Documenting Scripts
- Using PowerShell Commands
- Getting Help on PowerShell
- Using WMI_Cmdlets
- Getting Details on an Object
- Querying Information on Specific Objects
- Terminate Applications with Win32_process
- Formatting Output with the -f Format Operator
- Filtering the Output with the Where-Object Command
- Using the IF statement
- Using the Switch Statement
- Script Reusability