The Windows command line, also known as the Command Prompt or the Windows Terminal, is a powerful tool that allows you to execute various commands and scripts on your computer. Although many users prefer to use the graphical user interface (GUI) to perform tasks on their Windows machines, the command line can provide a more efficient and flexible way to manage your system, automate tasks, and troubleshoot issues.

In this article, we will provide you with a comprehensive cheat sheet of the most useful and commonly used Windows command line commands. Whether you are a system administrator, a developer, or an advanced user, these commands can help you perform a wide range of tasks, from managing files and folders to configuring networks and services.

We will divide the commands into several categories, including file and folder management, system information, network configuration, security, and more. For each command, we will explain its syntax, parameters, and common use cases, and provide examples of how to use it. By the end of this article, you will have a solid understanding of the Windows command line and how to use it to make your work more efficient and productive.

Navigation Commands

  • cd [path]: The cd command is used to change the current working directory to the specified path. For example, cd C:\Users\JohnDoe\Documents would change the working directory to the “Documents” folder of the “JohnDoe” user on the “C:” drive. You can also use relative paths (e.g. cd .. to move up one directory) and environment variables (e.g. cd %userprofile% to move to the current user’s home directory).
  • dir: The dir command is used to list the contents of the current directory. By default, it will display the file name, size, and date modified for each file in the directory. You can also use various options (e.g. dir /w for a wide list format or dir /s to include subdirectories).
  • mkdir [dir_name]: The mkdir command is used to create a new directory with the specified name. For example, mkdir MyFolder would create a new folder named “MyFolder” in the current working directory.
  • rmdir [dir_name]: The rmdir command is used to delete the specified directory (must be empty). For example, rmdir MyFolder would delete the folder named “MyFolder” in the current working directory.
  • type [file_name]: The type command is used to display the contents of the specified file. For example, type myfile.txt would display the contents of a file named “myfile.txt” in the current working directory.

File Management Commands

  • copy [source_file] [destination]: The copy command is used to copy a file to a new location. For example, copy myfile.txt C:\Users\JohnDoe\Documents would copy a file named “myfile.txt” to the “Documents” folder of the “JohnDoe” user on the “C:” drive.
  • del [file_name]: The del command is used to delete the specified file. For example, del myfile.txt would delete a file named “myfile.txt” in the current working directory.
  • move [source_file] [destination]: The move command is used to move a file to a new location. For example, move myfile.txt C:\Users\JohnDoe\Documents would move a file named “myfile.txt” to the “Documents” folder of the “JohnDoe” user on the “C:” drive.
  • ren [old_file_name] [new_file_name]: The ren command is used to rename a file. For example, ren myfile.txt newfile.txt would rename a file named “myfile.txt” to “newfile.txt”.

System Commands

  • systeminfo: The systeminfo command is used to display system information such as the operating system name, version, and build number, as well as information about the processor, memory, and network adapters.
  • tasklist: The tasklist command is used to display a list of currently running processes. By default, it will display the process name, process ID, and memory usage for each process.
  • taskkill /im [process_name].exe: The taskkill command is used to terminate the specified process. The /im option specifies that the process should be terminated by its image name (i.e. the name of the executable file). For example, taskkill /im notepad.exe would terminate any instances of the Notepad application that are currently running

Networking Commands

  • ipconfig: The ipconfig command is used to display network configuration information such as the IP address, subnet mask, and default gateway for each network adapter. You can also use various options (e.g. ipconfig /all to display detailed information for each adapter or ipconfig /release to release the IP address for a specific adapter).
  • ping [host_name]: The ping command is used to test network connectivity by sending ICMP echo request packets to the specified host. For example, ping google.com would send packets to the Google website to test whether it’s reachable.
  • tracert [host_name]: The tracert command is used to trace the route that packets take from the local computer to the specified host. For example, tracert google.com would display a list of routers that the packets pass through on their way to the Google website.

User Account Management Commands

  • net user [username]: The net user command is used to manage user accounts on the local computer. By default, it will display a list of user accounts, their status (i.e. whether they’re active or disabled), and their last logon time. You can also use various options (e.g. net user [username] * to change the password for a specific user).
  • net localgroup [groupname]: The net localgroup command is used to manage local groups on the local computer. By default, it will display a list of local groups and their members. You can also use various options (e.g. net localgroup [groupname] [username] /add to add a user to a local group).

Miscellaneous Commands

  • assoc [.ext]: The assoc command is used to display or change file associations for a specific file extension. For example, assoc .txt would display the current file association for text files (e.g. “txtfile”).
  • set: The set command is used to display environment variables such as the current user’s home directory, the system root directory, and the temporary folder location. You can also use it to set or modify environment variables (e.g. set MYVAR=value to create a new environment variable named “MYVAR”).
  • taskmgr: The taskmgr command is used to launch the Windows Task Manager, which displays information about currently running processes, performance metrics, and startup programs.

File and Directory Management Commands

  • cd [directory]: The cd command is used to change the current working directory to the specified directory. For example, cd C:\Users would change the working directory to the “Users” folder on the C drive.
  • dir: The dir command is used to display a list of files and directories in the current working directory. You can also use various options (e.g. dir /s to display files and directories in subdirectories as well).
  • md [directory]: The md command is used to create a new directory with the specified name. For example, md MyFolder would create a new directory called “MyFolder” in the current working directory.
  • rd [directory]: The rd command is used to delete a directory with the specified name. For example, rd MyFolder would delete the directory called “MyFolder” in the current working directory.
  • xcopy [source] [destination]: The xcopy command is used to copy files and directories from the source location to the destination location. You can also use various options (e.g. xcopy /s to copy subdirectories as well).

System Information Commands

  • systeminfo: The systeminfo command is used to display detailed information about the local computer’s system configuration, including the operating system version, processor type, and installed hotfixes.
  • hostname: The hostname command is used to display the hostname of the local computer.
  • netstat: The netstat command is used to display active network connections, including the protocol, local and remote IP addresses, and connection status.
  • tasklist: The tasklist command is used to display a list of running processes on the local computer, including the process name, process ID, and memory usage.
  • driverquery: The driverquery command is used to display a list of installed device drivers on the local computer, including the driver name, version, and file location.

Task Scheduler Commands

  • schtasks: The schtasks command is used to create, modify, and delete scheduled tasks on the local computer. You can use various options to set the task’s frequency, start time, and action to be performed.
  • at: The at command is used to schedule a command or program to run at a specific time. You can use various options to specify the date and time when the command should run.

System Administration Commands

  • netstat: The netstat command is used to display network statistics such as active TCP connections, open ports, and listening ports. You can use various options to display different types of network statistics.
  • sc: The sc command is used to manage Windows services. You can use various options to start, stop, pause, resume, or query the status of a service.
  • wevtutil: The wevtutil command is used to manage event logs on the local computer. You can use various options to view, export, or clear event logs.
  • gpresult: The gpresult command is used to display the Resultant Set of Policy (RSoP) for a user or computer on the local or remote computer. You can use various options to display different types of RSoP data.
  • reg: The reg command is used to manage the Windows registry. You can use various options to add, delete, modify, or query registry keys and values.

Batch Scripting

Basic Commands

  • @echo: The @echo command is used to display messages and variables on the console. The @ symbol suppresses the echoing of the command itself.
  • set: The set command is used to create, modify, or delete environment variables. You can use various options to display the current values, enable delayed expansion, and manipulate the strings.
  • echo off: The echo off command is used to turn off the echoing of the batch script commands on the console. This is useful when you want to hide the script contents from the user.
  • pause: The pause command is used to wait for the user to press a key before continuing the script execution. This is useful when you want to prompt the user for input or display a message.
  • goto: The goto command is used to jump to a label in the script. You can use various options to specify the label name, the condition, and the error handling.
  • call: The call command is used to call another batch script or command-line program from the current script. You can use various options to pass arguments, set the environment, and return to the calling script.

File and Folder Commands

  • dir: The dir command is used to display the list of files and folders in a directory. You can use various options to sort the output, filter the files, and display the attributes.
  • cd: The cd command is used to change the current directory to a specified directory or a relative path. You can use various options to navigate the directory tree, set the drive, and display the path.
  • copy: The copy command is used to copy one or more files from a source to a destination. You can use various options to overwrite the existing files, preserve the attributes, and create backups.
  • move: The move command is used to move one or more files from a source to a destination. You can use various options to overwrite the existing files, create backups, and rename the files.
  • del: The del command is used to delete one or more files from the current directory or a specified directory. You can use various options to force the deletion, prompt for confirmation, and delete the subdirectories.
  • md: The md command is used to create a new directory with the specified name in the current directory or a specified directory. You can use various options to set the attributes, create multiple directories, and display the help.

System Commands

  • tasklist: The tasklist command is used to display the list of running processes on the local or remote computer. You can use various options to filter the output, display the modules, and sort the processes.
  • taskkill: The taskkill command is used to terminate one or more running processes on the local or remote computer. You can use various options to force the termination, prompt for confirmation, and specify the process ID or name.
  • reg: The reg command is used to query or modify the Windows registry on the local or remote computer. You can use various options to view or edit the keys and values, export or import the data, and manage the permissions.
  • net: The net command is used to manage the network settings and services on the local or remote computer. You can use various options to view or modify the shares, users, groups, and printers.
  • ipconfig: The ipconfig command is used to display the IP address configuration and the network settings of the local computer. You can use various options to renew or release

Compression and Archiving Commands

  • compact: The compact command is used to compress files and directories to save disk space. You can use various options to set the compression level and exclude specific files or directories.
  • expand: The expand command is used to extract files from a compressed Windows cabinet (.cab) file. You can use various options to specify the output directory and overwrite existing files.
  • makecab: The makecab command is used to create a compressed Windows cabinet (.cab) file from one or more files. You can use various options to set the compression level and exclude specific files or directories.
  • tar: The tar command is used to create, extract, and manipulate tar archives. You can use various options to specify the archive format, compression level, and file filters.
  • zip: The zip command is used to create, extract, and manipulate ZIP archives. You can use various options to specify the archive format, compression level, and file filters.

Security Commands

  • cipher: The cipher command is used to encrypt or decrypt the files and folders on the local computer. You can use various options to specify the encryption algorithm, the key size, and the target directory.
  • sfc: The sfc command is used to scan the system files on the local computer and repair any corrupted or missing files. You can use various options to customize the scan, display the log, and restore the original files.
  • net user: The net user command is used to manage the user accounts on the local computer. You can use various options to create or delete user accounts, change the password, and assign user rights.
  • net group: The net group command is used to manage the groups on the local computer. You can use various options to create or delete groups, add or remove members, and assign group permissions.
  • netsh: The netsh command is used to configure the network settings on the local computer. You can use various options to configure the network adapters, the firewall, and the remote access.

Disk Management Commands

  • chkdsk: The chkdsk command is used to check the file system and the physical disk for errors and bad sectors. You can use various options to scan the disk, repair the errors, and recover the data.
  • diskpart: The diskpart command is used to manage the disk partitions and volumes on the local computer. You can use various options to create or delete partitions, format or assign drive letters, and set active partitions.
  • format: The format command is used to format the file system on a disk volume. You can use various options to specify the file system type, the cluster size, and the volume label.
  • fsutil: The fsutil command is used to manage the file system features on the local computer. You can use various options to query or modify the file system attributes, create or delete hard links, and manage the NTFS permissions.

Batch Processing Commands

  • for: The for command is used to execute a command or a series of commands for each item in a set of files or directories. You can use various options to customize the loop behavior, the delimiter, and the output format.
  • if: The if command is used to execute a command or a series of commands based on a specific condition or a set of conditions. You can use various operators to compare strings or numbers, test the file attributes, and check the errorlevel.
  • goto: The goto command is used to redirect the execution flow to a specific label in a batch script. You can use various labels to organize the script, jump to a different section, and handle the error conditions.
  • set: The set command is used to assign values to variables in a batch script. You can use various options to define the scope, the type, and the value of the variables.
  • echo: The echo command is used to display messages or variables in a batch script. You can use various options to customize the output format, the color, and the redirection.

User Interface Commands

  • mode: The mode command is used to configure the console display and input modes on the local computer. You can use various options to change the screen buffer size, the font size and type, and the keyboard layout.
  • color: The color command is used to change the foreground and background colors of the console screen on the local computer. You can use various options to specify the color codes, the intensity, and the default settings.
  • title: The title command is used to set the title of the console window on the local computer. You can use various options to specify the text, the color, and the font of the title.
  • cls: The cls command is used to clear the console screen on the local computer. You can use this command to remove the previous output, display a clean slate, and reduce the clutter.
  • pause: The pause command is used to pause the execution of a batch script and wait for the user to press any key to continue. You can use this command to prevent the script from closing immediately and provide feedback to the user.

Flow Control

In Windows command line, flow control refers to the ability to control the execution of batch files and scripts based on specific conditions or events. There are several flow control commands that you can use to add conditional logic and branching to your scripts, including:

Conditional Statements

  • if: The if command is used to test a condition and perform different actions based on the result. You can use various operators, such as ==, !=, >=, <=, to compare values, and use the else clause to specify an alternative action if the condition is not met. Example:
bashCopy codeif %ERRORLEVEL% == 0 (
   echo Command succeeded
) else (
   echo Command failed
)
  • for: The for command is used to loop through a set of values and perform an action for each value. You can use various modifiers, such as %%~nxa, %%~dpI, to extract parts of the value, and use the in clause to specify the values or a file set. Example:
javaCopy codefor %%I in (*.txt) do (
   echo Found file: %%I
)

Jump Statements

  • goto: The goto command is used to transfer the execution of the script to a specific label. You can use the :label syntax to define a label and use the goto label command to jump to it. Example:
phpCopy code:start
echo Starting script...
goto end
:middle
echo Middle of script...
:end
echo End of script.
  • call: The call command is used to call a subroutine or another batch file and return to the original script when it finishes. You can use the %0 variable to refer to the current batch file and use the %1, %2, … %9 variables to pass arguments to the called batch file. Example:
vbnetCopy codecall :subroutine arg1 arg2
echo Returned from subroutine.
goto end
:subroutine
echo Subroutine started with arguments %1 and %2.
goto :eof

These are some of the flow control commands that you can use in Windows command line to add conditional logic and branching to your scripts. Remember to use them with caution and avoid complex nesting and recursion, as they can make your script hard to read and debug.

FAQ

  • What is the Windows command line?

The Windows command line is a text-based interface that allows users to execute commands and scripts on a Windows computer. It is also known as the Command Prompt or the Windows Terminal.

  • How do I open the Windows command line?

To open the Windows command line, press the Windows key + R, type “cmd” and press Enter, or search for “Command Prompt” in the Start menu.

  • What are some basic Windows command line commands?

Some basic Windows command line commands include dir (list files and directories), cd (change directory), mkdir (create directory), del (delete file), type (display file contents), echo (display text), and ping (test network connectivity).

  • How do I run a batch file in the Windows command line?

To run a batch file in the Windows command line, navigate to the directory where the batch file is located and type the name of the file, including the .bat extension. Example: mybatchfile.bat

  • Can I use Windows command line to automate tasks?

Yes, you can use Windows command line to automate tasks by creating batch files or scripts that execute a series of commands. You can also use third-party tools, such as PowerShell or Task Scheduler, to create more complex automation workflows.

  • How do I get help with Windows command line commands?

To get help with Windows command line commands, you can use the /? or -h option after the command name, or type help to display a list of available commands and their descriptions.

  • How do I redirect output to a file in the Windows command line?

To redirect output to a file in the Windows command line, use the > or >> operator followed by the file name. The > operator overwrites the file if it exists, while the >> operator appends to the file if it exists or creates a new file if it doesn’t. Example: dir > files.txt

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *