Windows Command Line: Beginners Guide

From ETCwiki
Jump to navigationJump to search

Windows Command Line

A simple guide for any skill level.

Windows-command-line.jpg

Windows Command Line, known as a Command Line Interface, can be a very powerful tool for system administrators, power users, and technical support personnel. While most users will never see Windows Command Line, this guide is intended for Intermediate to Advanced users.

Most of Windows Command Line functions can be accessed through Windows GUI, other functions and programs can not. Knowing and understanding Windows Command Line will allow you to complete tasks faster, access features and programs not available in the Windows GUI, and look like you know what you're doing.

Through this guide, we will walk through each section of commands that Windows XP Command line offers you the capability of. At the end, we discuss how scripting, and batch files can automate tasks, and save you time.

Terms: CLI - Command Line Interface (non-GUI) WCL - Windows Command Line GUI - Graphical User Interface Script - Chain of commands to be automatically run in order


Navigating Windows Command Line

Windows-command-line-run.jpg To open Windows Command Line, click Start > Run and enter the word "cmd". This is a shortcut to opening a historic file called command.com, which is now called cmd.exe (the main program running WCL). The files for WCL can be found in "%systemroot%\system32". Whenever you see a value starting and ending with %, you will know this is a variable. The %systemroot% variable will have a value of whatever your main windows folder is. For Windows XP, your system root is C:\Windows\, unless specified otherwise. For Windows 2000, your system root is C:\Winnt.

Terms: Variable - A value that can change based on user input Syntax - The way a command is typed (word order)


Navigating the Command Line Interface: Commands and Examples

Each command in Windows Command Line has a different function. Each command also has a help document associated with it (for the most part). To access help for any command, try these two methods:

Windows Command Line Help (command) /? - This /? at the end of the command will bring up a help document describing different "modifiers" you can add to command using the / symbol.

windows xp cli

If you dont get a help document from the first command, check your syntax, and try the other help command.

HELP - Help document for WCL Usage: help OR help <command> Example: help Example: help copy Explanation: Help lists commands, help <command> shows command help document


Windows Command Line Commands

Listed below are some commonly used Windows Command Line Commands. They show the command name, its description, usage, and an explanation of what the example command would do.

CD - Change current working directory Usage: cd <directory you want to open> OR cd\ Example: cd\ Explanation: Changes your working directory to root (c:\) Example: cd windows Explanation: Takes you to the windows directory inside your current directory

COPY - copies files or folders to other locations that already exist Usage: copy <file or folder from> <copy file or folder to> Example: copy employees.txt c:\work\employees\

CLS - Clears the screen of all commands Usage: cls Example: cls

DEL - Deletes a file from current directory Usage: del <folder or file name> Example: del programs Explanation: Deletes the folder or file named programs

DIR - Used to list all files in current directory Usage: dir (modifiers can be listed with /?) Example: dir Explanation: This will list all files and folders at current location

EXIT - Exits CMD Usage: exit

FIND - Finds a command given certain search parameters Usage: Find <search term> Example: Find "*.txt" Explanation: Finds all text files in directory Notes: * can be used as a 'wildcard' to mean any length or any combination of letters.

MD - Make directory at current location Usage: md <directory name you want to make> Example: md programs Explanation: This will make a new folder at the current location

MOVE - Moves a file from one location to another (cut) Usage: Move <full path of file you want to move> <already existing directory to move TO> Example: Move c:\games\mygame.zip c:\newgames\ Explanation: This will move the file mygame.zip from one directory, to another existing directory

PAUSE - Pauses scripts or batch files and waits for user input example: pause Explanation: When in a script, this displays "Press any key to continue" and waits for user input

RD - Remove Directory Usage - rd <folder name> Example: rd windows Explanation: This removes the directory Windows (bad idea) Notes: to remove a directory and its contents, "rd /s windows"

REN - Rename a folder or file Usage: ren <file or folder> <new name you want> Example: ren windows window Explanation: this changes your windows directory name to window

TIME - Check or modify system time Usage: time

VER - Check your version of Windows Usage: ver


Windows Command Line also interfaces with files on your computer to record data to. Piping and chaining commands is another option in most CLI situations. Chaining commands is simply feeding the output of one command (dir for example outputs a list of files and folders) to another command that you want to run only for the output of the first file.

Piping Commands

The chaining of commands can be used by typing | (shift + \). This is called the pipe, or piping commands. To pipe one command to another, type one full command, and WCL automatically outputs the data that you normally see to the next command.

For example, "dir c:\windows\ | Find "*.txt" The above example would list all files in c:\windows\ then put all of the files and folders in c:\windows\ get sent to the find command. The find command then looks for any files that end in .txt. You will now see a list of all txt files in c:\windows\


Output to a text file

Another useful function for script makers is the ability to output text from the command line (input and output) to a text document. Using the output to text file, or append options both come in handy at different times.

> - Create a new text file and have WCL output info to it Usage: <command to output> > <location of text file to output to> Example: dir > c:\myfiles.txt Explanation: This command would list all files and folders in current directory, and outputs to file c:\myfiles.txt

>> - Append information from a command to a text document. Usage: <command> >> <file to add to> Example: dir > c:\myfiles.txt Explanation: This would list files and folders in current directory and ADD the list to myfiles.txt while keeping the previous data in tact.


Chaining Commands

When there are multiple commands you want to run at one time, Chaining commands in Windows Command Line comes in handy. To chain a command, we have two tools.

& - Complete one command then another directly after Usage: command & command2 Example: dir & ver Explanation: This will list all files in current directory and then the version of Windows Command Line


&& - Complete one command, then another if the first was successful Usage: <command1> && <command2> Example: copy c:\importantdata.txt c:\backup\ && ren backupdata.txt Explanation: This will copy the file importantdata.txt to c:\backup\, THEN if successful, it will rename the file to backupdata.txt


Processes and Performance

Windows has its GUI version of system performance monitor that shows up every time you press CTRL-ALT-DELETE at the same time. There is also a command line version that can be configured to show much more information for power users.

Another thing to take into account is that when you open a CMD window, it will use a LOT less system resources than a GUI. This can be VERY helpful in ending processes that are lagging your computer a lot.

To access the process and performance monitoring tools, use the following commands:

Tasklist in Windows Command Line (DOS)

TASKLIST - Lists all running processes with detailed information Usage: Tasklist Note: the /svc addition to tasklist allows you to see what processes are using what services at the moment Note2: /M addition shows each process, and associated DLL files


TASKKILL - Stops tasks with given parameters Usage: Taskkill <which task> Example: Taskkill /pid 140 Explanation: This will end the task with PID of 140 (the process identification number). You can find the PID for each task using tasklist

Controlling Windows Services in Command Line

Services are extra functions that Windows has the ability to start and stop depending on whether you allow the services to be started. A lot of windows services contribute to slow computing, security risks, and instability in the operating system. While some services are essential, others are never needed. Windows XP Services

To list, modify, start and stop services you will need a new commands to help you.

SC query - Shows list of configured services Usage: SC query Explanation: This will list info and data on services


SC qc - Queries a specific service Usage: SC qc <service name> Example: sc qc messenger Explanation: This will show configuration data for the messenger service

SC start - Starts a specific service Usage: SC start <service name> Example: SC start messenger Explanation: This starts the messenger service

SC stop - Stops a specific service Usage: SC stop <service name> Example: SC stop messenger Explanation: This stops the messenger service

SC config - Configures service properties Usage: SC config <service name> start= <start type> Example: SC config messenger start = disabled Explanation: This will tell the messenger service NOT to start with the computer. Notes: Settings available for start= are Disabled, Auto, and Demand Disabled never starts, Auto starts every startup, and demand starts as the service is needed


Windows Hard Disk Utils

Windows comes with extensive command line utilities to control, check and maintain your hard disk drives. Included in the utilities are Drive Partitioners, Defragmenters, Check Disk Utilities, and utilities to set a drive to be checked for errors on next startup.

Try some of these Windows Command Line Commands, as they can save your computer in case of disasters like virus's or improper shutdowns.

CHKDSK - Checks a hard disk for errors Usage: chkdsk <parameters> <drive to check> Example: chkdsk /f C: Explanation: This command will scan drive C for errors, and repair them if needed. Notes: /f parameter auto-repairs errors when found

DEFRAG - Defragments a hard drive to optimize performance Usage: Defrag <parameter> <drive letter> Example: Defrag c:\ Explanation: Desfragments the C: drive Notes: defrag -a <drive letter> will analyze only, and not defragment



User Control

Embedded in Windows XP is the ability to add and remove users. This concept is called a "Multi-User Operating System". This type of system allows for more than one user account to be made, each account with specific permissions to access data or run programs. User Accounts can be changed from control panel, or Windows Command Line.

NET USER - View a list of local user accounts Usage: Net User <name of user, or blank for all> Example: Net User Chris Explanation: Displays user account info for user Chris Notes: Access to net user can be used across a single domain also Parameters: net user <name> /delete Explanation: This deletes a user account

Windows-command-net-user.jpg



NET USER - Adds users to local computer Usage: net user <username to add> <password to add> /add Example: net user chris library /add Explanation: This will add a user to the local computer with the name as chris, and the password as library Notes: Parameters that go BEFORE /add are /fullname:"Chris Reed" (Full name of user) /comment:"Chris is main systems admin" (users comments) /passwordreq yes (requires password at logon)

Example 2: net user Chris library /fullname:"Chris Reed" /comment: "Chris is main systems admin" /passwordreq yes /add Explanation: This adds user Chris with password library, and full user name of chris reed, with a user comment as "Chris is main systems admin" and requires a password at logon


Managing TCP/IP and Networking with WCL

TCP/IP is a transmission protocol for two computers communicating through a network. TCP/IP has error checking for the packets that are transferred, and is used in many internet and networking situations.

Windows Command Line allows you to view detailed information about your internet connection, DNS servers, DHCP servers, your internal IP address, and defualt gateway. The commands below will show you how to check your TCP/IP status, modify servers, and set static IP addresses.

Terms: [code]DHCP - Automatically determined IP set for your computer Static IP - A custom setup network configuration with unchanging IP TCP/IP - Communication protocol for 2 computers over a network Default Gateway - Your router or gateways IP address Subnet Mask - Defines the section of a network you are on DNS servers - Computer that resolves URL to IP (www.yoursite.com to IP) IP address - Your individual computers network ID


Note: To check your TCP/IP connections, network adapter, and settings, you can type ping "127.0.0.1". This will send a ping out through your network adapter, and redirect it right back to your computer. Upon successful ping, you can tell that your computer is setup properly to use TCP/IP.

== TCP/IP from Windows Command Line ==

IPCONFIG - Shows IP information for your computer Usage: IPCONFIG or IPCONFIG /all Example: ipconfig /all Explanation: Shows all information about current TCP/IP connection Notes: Other parameters /flushDNS (resets DNS cache) /release (releases your network adapter) /renew (renews your network adapter)

Windows-command-ipconfig.jpg


SET ADDRESS - sets tcp/ip settings via WCL Usage: Set address name="<connection name>" source=<static or dhcp> addr=<static IP> mask=<subnet mask> gateway=<gateway IP>

Static IP Network Configuration Example: Set address name="Local Area Connection" source=static addr=192.168.1.201 mask=255.255.255.0 gateway=192.168.1.1 Explanation: This will make the computer setup a new connection called Local Area Connection, with a STATIC IP of 192.168.1.201, a subnet mask of 255.255.255.0 and a gateway of 192.168.1.1

DHCP Network Configuration Example: set address name="Local Area Connection" source="dhcp" Explanation: This will setup a new connection name Local Area Connection that will try to automatically set your IP using DHCP protocol. (Your router or gateway will be the DHCP server usually)

DELETE ADDRESS - Removes a connection from your list Usage: Delete Address name=<address name> addr=<IP address>

DNS Server Setup DNS servers are computers connected to the internet with records of URLs (www.yoursite.com) and the matching IP address for the URLs server (68.48.0.12). Your computer connects to a DNS server every time you enter a web page in your address bar. Although you dont usually see the IP address information, it is hiding beneath your browser or internet program.

To setup custom DNS servers using WCL, you will need the following commands. For your DNS server, you can either contact your ISP, and use one of theirs, or you can try using your router.

ADD DNS - Sets custom DNS servers to use Usage: ADD DNS name=<DNS server name> addr=<ip of DNS server> Example: ADD dns name="My Pinacolataburg DNS" addr="68.48.0.12" Explanation: Adds a DNS server named My Pinacolataburg DNS with an IP address of 68.48.0.12

DELETE DNS - removes a specific DNS entry from a connection Usage: delete dns name=<name to delete> addr=<IP to delete> Example: delete dns name="My Pinacolataburg dns" addr=68.48.0.12 Explanation: This will remove the DNS server entry above



Windows Command Line has an array of tools for diagnosing network problems. You can view individual stats for network adapters and network connections. You can also view the path to a certain internet host, get ping information for the host, and find all open TCP/IP connections with information on what program is using the connection.

Tracert - Find ping, and number of hops to reach a given server Usage: Tracert <internet host to trace the route to> Example: tracert www.computershack.net Explanation: This command would give tracert information from the originating computer, to the opposite end of the connection.

PING - Find how long it takes for a message to be send to a server, and back again. Usage: Ping < internet host IP or URL > Example: ping www.computershack.net Explanation: Ping shows the time, in milliseconds it takes to contact the given IP or URL, plus the time it takes to get back to your computer.

NETSTAT - Shows information about network connections Usage: Netstat <parameters> Example: Netstat -b Explanation: With the parameter -b, netstat shows a list of all open connections, the port, and program that is managing the connection. Notes: Another parameter is -a to view all open connections/ports

Windows-command-netstat.jpg


NETSH - Network Shell is a collection of tools to diagnose network components. Usage: NETSH <parameter> Example: NETSH diag ping adapter Explanation: This will ping each item in your TCP/IP configuration. Included is DNS, DHCP, WINS, and Default Gateway


Conclusion: Using the information in this document, it is possible to control, maintain, diagnose, and fix problems in almost all categories. With this information, try using Windows Command Line more often. It will help you become a better technician, and be able to fix more problems faster.



More Resources: