Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
programming4us
Windows 7

Using the Windows Command Prompt (part 1)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
5/19/2011 5:48:45 PM
Many older administrators cut their teeth on the old DOS prompt that preceded Windows. They moved from the DOS prompt to the Windows graphical user interface, only to realize that many things can be accomplished only from the command prompt. While the command prompt may look out of fashion to some, it is indeed an integral tool to accomplish many basic and advanced administrative tasks.

PowerShell (a souped-up command prompt) was introduced in Windows Vista and Windows Server 2008, and it has been significantly enhanced with version 2.0 in Windows 7 and Windows Server 2008 R2. Windows Server 2008 and Windows Server 2008 R2 include Server Core editions that are Windows operating systems without the GUI—everything is done from the command prompt.

In short, the command prompt is not going away. It's stronger than ever. One of the biggest reasons is that anything that can be executed at the command prompt can be scripted, and anything that can be scripted can be scheduled or programmed to respond to specific events.

In this section, you'll learn some basics related to the command prompt and some basic commands, and then you'll create some basic scripts in the form of batch files. Remember though, this small section won't tell you everything about the command prompt.

1. Launching the Command Prompt

This is a participative sport. You can't just read about it—you really need to get your hands on the keyboard and execute these commands to see how they work. Start by launching the command prompt. In Windows 7, you can launch it using one of these two methods:

  • Click Start => All Programs => Accessories => Command Prompt.

  • Click Start, type CMD in the Search text box, and press Enter.

NOTE

By default, the command prompt will launch without administrative permissions. However, some commands need to be executed with administrative permissions. For these, right-click the Command Prompt link or the cmd.exe executable that shows when you use the Search text box, and select Run As Administrator.

It starts with white text on a black screen, though you can modify it if you desire. After launching the command prompt, right-click the title bar, select Properties, and then select the Colors tab. You'll see a dialog box similar to Figure 1.

Figure 1. Modifying the display of the command prompt

The screen background is black by default, but you can modify it to a white background by clicking the white block. Select Screen Text, and click the black block.

In addition to modifying the colors, you can modify the font from the Font tab. You can select one of only three fonts, but you can choose one of many sizes. I often modify the fonts when doing demonstrations in the classroom so students can easily see the commands—even from the cheap seats.

2. Command Prompt Basics

First, let's cover some basics when using the command prompt. These basics apply if you're using basic commands, using advanced commands, or using the commands in scripts. In addition, many of these same basics apply to PowerShell commands.

2.1. Help Is Always Available

You can find help for just about any command by typing in the command with either /? or /Help. In other words, to get help on the Shutdown command, you can enter either Shutdown /? or Shutdown /Help at the command prompt. Also, many commands have help available by typing in Help and then the command. For the Shutdown command, you can enter Help Shutdown.

Windows 7 help includes several help topics files and links to online resources. Click Start, select Help And Support, type in Command Reference Overview, and click Search Help (or press Enter). The Command Reference Overview help article includes a short intro and links for many online articles, including help topics for about 200 individual commands that can be executed at the command prompt.

2.2. Spelling Counts

At some point in the future, computers will do what we want them to do, not what we ask them to do. For now, they interpret our commands quite literally. As an example, if I wanted to start the Print Spooler service, I would enter this command:

Sc start spooler

However, if I accidentally entered the following command instead (Sx instead of Sc), the command prompt would give a syntax error:

Sx start spooler

The error indicates the command is not recognized, because it is trying to execute a command called Sx, which doesn't exist. When things don't work, it's worth your time to read the error message. It will often point you in the right direction.

2.3. Commands Are Modified with Switches

A switch is identified with either a / symbol or a - symbol. The / symbol was used often in older DOS commands, and the - symbol was common in UNIX and UNIX derivatives. However, at this point, most commands will accept either switch.

Each command has a specific set of switches that are accepted and can be identified when the command is entered with the /? switch.

As a simple example, you can use the DIR command to view the contents of the current directory (by typing DIR and pressing Enter). You can modify the command to view all the files in the current directory that start with IP (using DIR IP*). It can be modified to look for any file that starts with IP* in any subdirectories with the following command that adds the /S switch.

DIR IP* /S

2.4. Commands Are Not Case Sensitive

Most command-prompt commands are not case sensitive. In other words, a command is interpreted the same if it's entered as all uppercase, all lowercase, or a mixture of both uppercase and lowercase. As an example, the SET command allows you to view many different environment variables for your system. You would see the same result if you entered it as SET, SeT, set, or even sET.

It's extremely rare, but occasionally you'll run across a parameter used within a command-prompt command that needs to be a specific case. When this true, it will be stressed.

2.5. Commands Can Use Wildcards

A wildcard is a character that can take the place of other characters. They are often used for search and copy operations. The command prompt includes two wildcard characters: the asterisk (*) and the question mark (?).

The * symbol will look for any instance of zero or more characters in the place of the * symbol. As an example, if you want to know if you have any TXT files (with the .txt extension) in the current directory, you can use the following command:

Dir *.txt

Similarly, if you want a listing of all files that start with App and end with .exe, you can use this command:

Dir App*.exe

Note that it will include a file named App.exe, if it exists, in addition to any files that that have letters after App.

The ? wildcard will take the place of a single character. It isn't used as often, but you can use it if you're looking for something more specific. For example, if you were looking for any files that had an extension of .ex and any third character, you could use this command:

Dir *.ex?

It would not include any files that ended with .ex without a third character in the extension. In other words, the ? wildcard specifies that a single character must exist for a match to occur. This is different from the * symbol, which will match for zero or more characters.

2.6. Strings with Spaces Need Quotes

Many commands will accept parameters, and when the parameters include spaces, the parameter usually needs to be enclosed in quotes.

As an example, the Net Shell command (netsh) can be used to configure a lot of networking settings, including the properties for the network interface card (NIC). The default name for the wired network interface card is Local Area Connection. If you want to set the DNS IP address to 10.10.0.10 using netsh, you could use the following command:

netsh interface ipv4 set DNSServer "Local Area Connection" static 10.10.0.10


In this example, the name of the NIC is expected after the DNSServer parameter. Since "Local Area Connection" is enclosed in quotes, it's interpreted as the name of the NIC.

However, the following command (without the quotes) would be interpreted quite differently:

netsh interface ipv4 set DNSServer Local Area Connection static 10.10.0.10

In this example, Local would be interpreted as the name of the NIC and the netsh command would then try to interpret Area as a separate command. Since netshArea command, the entire command would fail. doesn't have an

You can occasionally get away without using the quotes. For example, if you're at the root of C (C:\) and want to change to the Program Files folder (which has a space), the following command will work:

CD Program Files

This is only because the CD command has been programmed to accept it, but that wasn't always the case. You could also enter this command as

CD "Program File"

2.7. DOSKEY Saves Typing

DOSKEY is a utility that is constantly running in the background of the command prompt and can be very valuable—if you know how to use it. Every time you enter a command in a command prompt session, it is recorded by DOSKEY and it can be recalled.

As an example, imagine you're testing connectivity with a server using the following ping command:

Ping DC1.Training.MCITPSuccess.com

You could execute the command and then realize the NIC wasn't configured correctly, or the host cache needed to be cleared with IPConfig /FlushDNS, or something else needed to be done. After resolving the issue, you want to execute the command again. Instead of typing it in from scratch, simply use the up arrow to recall it, press Enter, and you've executed it again (without typing it again).

The up arrow can be used to retrieve any previous command that you've entered in this session (up to the limit of the buffer, which is rather large). This can be valuable when you're entering very long commands or even short commands if you use the hunt-and-peck method of typing.

Typos are also common at the command line. However, you don't have to retype the entire command. You can use the up arrow instead to recall the command and then use the left and right arrows to position the cursor where you want to modify the text. Make your corrections, press Enter, and the corrected command executes.

You can also use the F7 key to display a pop-up window that shows a history window. It includes all of the commands you've entered in the current session. You can then use the up or down arrow to select the desired command.

DOSKEY is a command-prompt utility itself, and it includes some commands you can use. For example, if you want to view all the commands that have been entered in the current session, enter DOSKEY/History.

By default, the system includes a buffer size of 50 commands. If you need more, you can modify the buffer size. For example, the following command changes the buffer to 99:

DOSKEY /Listsize=99

3. System Variables Identify the Environment

Many system variables are available within Windows 7, and you'll see these often when using the command prompt and Windows PowerShell. They are useful in identifying specific information about the environment, without actually knowing the current environment. As a simple example, every computer has a computer name, but the computer names are different. However, the system variable %computername% holds the value of the local computer's computer name.

System variables are easy to identify. They always start and end with a percent symbol (%). An easy way to view the value of any variable is by using the echo command in the following format:

Echo %variableName%

Table 1 shows many of the commonly used system variables and their value.

Table 1. Some commonly used system variables
VariableValue
%windir% %systemroot%Both %windir% and %systemroot% identify the folder where Windows was installed, typically C:\Windows.
%systemdrive%The folder where the system boot files are located, typically C:\.
%computername%The name of the local computer.
%username%The name of the user logged on to this session.
%date%Holds the value of the current date in the format ddd mm/dd/yyyy. The first three letters are an abbreviation of the day such as Mon, Tue, Wed, and so on. The remaining format is all numbers with mm for the month, dd for the day, and yyyy for the year.
%time%Holds the value of the current time in a 24-hour format as hh.mm.ss.ms.
%errorlevel%Indicates whether the previous command resulted in an error. If it didn't have an error, the value is 0.
%ProgramFiles%Points to the location of the Program Files folder, which is normally C:\Program Files.
%Public%The location of the Public folder, typically C:\Users\Public.
Other -----------------
- Automating the Deployment of Windows 7 : Using the Microsoft Deployment Toolkit 2010 (part 2) - Creating a Task Sequence
- Automating the Deployment of Windows 7 : Using the Microsoft Deployment Toolkit 2010 (part 1) - Installing MDT 2010 & Creating a MDT 2010 Deployment Share
- Deploying Images with Windows Deployment Services (part 3) - Capturing Images with WDS
- Deploying Images with Windows Deployment Services (part 2) - Adding and Configuring WDS
- Deploying Images with Windows Deployment Services (part 1) - WDS Requirements & Deploying Images with WDS
- Automating the Deployment of Windows 7 : Imaging with the Windows Automated Installation Kit (part 3) - Preinstallation Environment & System Image Manager
- Automating the Deployment of Windows 7 : Imaging with the Windows Automated Installation Kit (part 2) - Understanding the Deployment Image Servicing and Management Tool
- Automating the Deployment of Windows 7 : Imaging with the Windows Automated Installation Kit (part 1) - ImageX
- Automating the Deployment of Windows 7 : Choosing a Deployment Strategy
- Automating the Deployment of Windows 7 : Understanding and Designing Images
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server