4.10 Use the appropriate Microsoft command-line tool

Introduction 

Think of the Command Prompt as a toolkit for your computer—just like a mechanic uses specialized tools to fix an engine, you'll use commands to manage files, navigate directories, and optimize system performance.

This lesson will show you how to use the Command Prompt for precise control over your computer, making tasks easier and troubleshooting more efficiently.

By learning these commands, you’ll gain the ability to solve problems quickly and keep systems running smoothly.

Command Prompt Overview 

The Command Prompt is used to run commands and view their output. You can open the Run dialog to execute single commands, but for a series of commands or more detailed output, you need to use the cmd.exe shell. This shell handles legacy commands that have been part of Windows for a long time. 

Even though Windows now has PowerShell and Windows Terminal (in Windows 11), you can still run older commands in these interfaces too. 

Run Dialog

Power Shell

Command Prompt

Administrative Command Prompt 

Some commands require elevated privileges to run. If you try to run one of these commands without proper privileges, you'll see the error message: "The requested operation requires elevation." This means the command needs to be run as an administrator

Here’s how to open an administrative command prompt: 

  • Right-click the Command Prompt shortcut and choose Run as administrator

  • Alternatively, type cmd in the search bar, then press CTRL+SHIFT+ENTER to open the command prompt as an administrator. 

When in administrator mode, the title bar will say "Administrator: Command Prompt", and the default folder will be C:\Windows\System32 instead of your user folder. 

This method can also be used to open other tools, like Explorer or Notepad, with administrative privileges. 

Command Syntax 

To run a command, you need to type it at the prompt (>) using the correct format, which includes the command name, any switches, and arguments. After typing the command, press ENTER to run it. 

  • Switches are options that modify the behavior of a command, usually preceded by a / symbol. 

  • Arguments are the values or settings the command needs to operate on. If an argument contains spaces, enclose it in quotation marks

If the screen gets cluttered with too much text, you can use the cls command to clear it. 

Some commands, like nslookup or telnet, can run in interactive mode, meaning they keep running and accept multiple inputs until you tell them to exit. You can use exit, quit, or CTRL+C to end these commands. If you're not in interactive mode, the exit command will close the command prompt window. 

Getting Help 

The command prompt offers a simple help system to assist with commands: 

  • Type help at the prompt and press ENTER to see a list of available commands. 

  • For help on a specific command, type help [command] to get detailed information about the command's syntax and available switches. 

  • You can also type [command] /? to display help for that command. For example, netstat /? will give you help on the netstat command. 

 

Navigation Commands Overview 

In the Command Prompt, the string before the > symbol shows the current working directory. Any commands you run will affect this directory unless you specify another directory path. 

Windows uses backslashes (\) to separate directories in a path, but you can also use forward slashes (/), which will be interpreted correctly, similar to Linux. 

Listing Files and Directories 

The dir command lists all files and subdirectories within the current directory or a specified path. 

You can use various switches to control how the files are displayed: 

/o 

:Sorts files:

  • n: By name. 

  • s: By size. 

  • e: By extension. 

  • d: By date. 

/t 

: Displays file dates: 

  • c: Created on. 

  • a: Last accessed. 

  • w: Last modified. 

/a 

: Displays files by attribute: 

  • r: Read-only. 

  • h: Hidden. 

  • s: System. 

  • a: Archive. 

You can use wildcard characters to search for specific file patterns: A question mark (?) matches a single character. 

Example: dir ????????.log lists all .log files with exactly eight characters in the name. 

Changing the Current Directory 

The cd command is used to navigate between directories. You can use either a full path or shortcuts

  • cd C:\Users\David: Changes to the C:\Users\David directory. 

  • cd Documents: If you're in C:\Users\David, this changes to C:\Users\David\Documents

  • cd ..: Moves up one level in the directory tree. If you're in C:\Users\David\Documents, it moves to C:\Users\David

  • **cd *: Moves to the root directory of the current drive (e.g., **C:*). 

  • cd \Windows: Moves to the Windows directory from anywhere on the C drive. 

Changing the Current Drive 

To change the drive you're working on, simply type the drive letter followed by a colon, then press ENTER

Example: D: switches to the D: drive, and the prompt will change to D:>

This allows you to move between drives while keeping your current directories intact. 

File Management Commands Overview 

The move and copy commands allow you to transfer files from one directory to another. Both commands use a simple syntax: Command Source Destination, where Source is the location of the file, and Destination is where you want the file moved or copied to. 

Copying Directory Structures 

  • xcopy: This command copies not only files but also entire directory structures. You can copy multiple directories while retaining the directory structure. The syntax is: 

    • xcopy Source [Destination] [Switches] 

    • Switches allow you to filter files or folders by attributes like read-only, hidden, or system files. 

  • robocopy: Robocopy (robust copy) is recommended over xcopy by Microsoft. It handles long file names and NTFS attributes better than xcopy. You can use it to copy or move files. 

    • Example: robocopy Source Destination /mov to move files.

    • Use the help command for additional switches and syntax options. 

Creating a Directory 

To create a new directory, use the md (make directory) command. The syntax is simple: md DirectoryName 

  • Example: md A:\Data\Docs creates a folder named Docs inside Data on the A drive. 

Note: Folder and file names cannot contain these reserved characters: \ / : * ? " < > | 

  • Example: md Data creates a folder named Data in the current directory. 

To delete a directory, use the rd (remove directory) or rmdir commands: 

  • rd DirectoryName or rmdir DirectoryName will delete the directory if it's empty. 

  • To delete a directory and all its contents, use the /s switch. 

    • Example: rd DirectoryName /s 

  • To avoid confirmation messages, add the /q (quiet mode) switch: 

    • Example: rd DirectoryName /s /q 

Disk Management Commands Overview 

While the Disk Management tool provides a user-friendly interface, there are cases where managing volumes through the command line is necessary. The diskpart, format, and chkdsk commands allow you to manage disks and volumes directly from the command prompt. 

The diskpart Command 

diskpart is a command-line utility used for advanced disk management, offering more control than the Disk Management tool. It is a powerful tool, so use it cautiously, especially when dealing with critical system partitions. 

Note: Unlike Disk Management, diskpart is not restricted from performing dangerous actions, such as deleting a system or boot volume. 

Here is a basic process for inspecting and managing disks and partitions with diskpart

1.Run diskpart to open the utility. 

2.Enter select disk 0 (or select the appropriate disk number). 

3.Enter detail disk to view detailed configuration info about the disk. 

  • This will show whether the disk’s partitions or volumes are healthy. 

  • If diskpart reports no partitions, the partition table may be corrupted. 

4.To inspect a partition or volume, enter select partition 0 or select volume 0

5.Enter detail partition or detail volume for more information. 

6.You can now use commands like: 

  • assign: Changes the drive letter. 

  • delete: Deletes the partition or volume. 

  • extend: Expands the size of the partition or volume. 

7.Enter exit to quit diskpart

The format Command 

The format command is used to write a new file system to a drive, deleting any existing data.

The basic syntax is: format X: /fs ,where X is the drive letter, and SYS is the file system type (e.g., NTFS, FAT32, or EXFAT). 

By default, format scans for bad sectors. To skip this, use the /q (quick format) switch. 

Important Points: 

  • Quick format: Removes references to files in the volume boot record but does not erase the actual data. In principle, the data can be recovered by third-party tools.

  • Secure format: Some utilities perform secure formatting, overwriting sectors with zeroes to ensure data is not recoverable.

The chkdsk Command 

The chkdsk (Check Disk) command scans for file system errors and faulty disk sectors. It can also attempt to repair detected problems. It operates in three modes: 

1.chkdsk X: 

Scans the drive in read-only mode, reporting errors without attempting repairs. 

2.chkdsk X: /f

Fixes file system errors. 

3.chkdsk X: /r

Attempts to fix file system errors and recover data from bad sectors. 

Important Points: 

  • If chkdsk detects open files, it will prompt you to schedule the scan for the next system restart. 

  • /f and /r can take a long time to complete. It’s best to run a read-only scan first to determine whether fixing is necessary. 

System Management Commands 

The shutdown Command 

The shutdown command is used to safely halt, restart, or log out of a computer. The basic syntax includes various options to customize how the system will be stopped: 

  • Shutdown (shutdown /s): This command closes all open programs and services before powering off the computer. Users are prompted to save any open files before the system shuts down. 

    • Use shutdown /t nn to set a delay (in seconds) before the shutdown begins. The default delay is 30 seconds. 

    • shutdown /a aborts the shutdown if used quickly enough. 

  • Hibernate (shutdown /h): Saves the current session to disk and powers off the computer. On startup, the session will be restored. 

  • Log off (shutdown /l): Closes all programs and services started under the user account but keeps the computer running. 

  • Restart (shutdown /r): Reboots the system without powering down, closing all programs and services first. This is called a soft reset.

System File Checker (sfc) 

System File Checker is a tool that scans and repairs damaged or corrupt system files. It uses a local cache to restore files and works within the Windows Resource Protection mechanism to protect system files, registry keys, and folders from unauthorized changes. 

  • sfc /scannow: Runs a scan immediately and repairs any detected issues. 

  • sfc /scanonce: Schedules a scan to run when the computer restarts. 

  • sfc /scanboot: Schedules a scan that runs each time the PC boots. 

Important Information: The system files are stored in the WINSxS folder, where they are version-controlled. This ensures that product media isn't required during the repair process, but this folder can consume a significant amount of disk space. 

Reporting the Windows Version 

The winver command reports the current Windows version, which is useful for support purposes. Understanding the version details helps in identifying the system’s update status. 

  • Windows 10 or 11: The brand version that distinguishes client versions from server versions, even though they use the same codebase. 

  • Version: Refers to a feature update using a year/month code, such as 1607 (July 2016) or 21H1 (first half of 2021). 

  • OS Build: The build number followed by a revision number (rev), which indicates the quality update status (patches). This rev number can be used to find information on changes or known issues in the Microsoft Knowledge Base

While winver provides basic version information, the About settings page gives more detailed information, such as the edition and licensing details. 

Summary 

The Command Prompt is a powerful tool that allows you to manage files, directories, and system settings using a variety of commands. Whether you need to navigate directories, copy files, or manage disks, the Command Prompt offers precise control over your system. This lesson only scratches the surface of what’s possible using the Command Prompt. More commands are introduced in the flashcards, so be sure to study them to ensure you don’t miss anything. By mastering these commands, you'll be able to troubleshoot issues and optimize system performance effectively.