
Mastering Windows Command Line: Your Ultimate Cheat Sheet for Increased Productivity
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 pre
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]: Thecdcommand is used to change the current working directory to the specified path. For example,cd C:\Users\JohnDoe\Documentswould 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: Thedircommand 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 /wfor a wide list format ordir /sto include subdirectories).mkdir [dir_name]: Themkdircommand is used to create a new directory with the specified name. For example,mkdir MyFolderwould create a new folder named "MyFolder" in the current working directory.rmdir [dir_name]: Thermdircommand is used to delete the specified directory (must be empty). For example,rmdir MyFolderwould delete the folder named "MyFolder" in the current working directory.type [file_name]: Thetypecommand is used to display the contents of the specified file. For example,type myfile.txtwould display the contents of a file named "myfile.txt" in the current working directory.
File Management Commands
copy [source_file] [destination]: Thecopycommand is used to copy a file to a new location. For example,copy myfile.txt C:\Users\JohnDoe\Documentswould copy a file named "myfile.txt" to the "Documents" folder of the "JohnDoe" user on the "C:" drive.del [file_name]: Thedelcommand is used to delete the specified file. For example,del myfile.txtwould delete a file named "myfile.txt" in the current working directory.move [source_file] [destination]: Themovecommand is used to move a file to a new location. For example,move myfile.txt C:\Users\JohnDoe\Documentswould 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]: Therencommand is used to rename a file. For example,ren myfile.txt newfile.txtwould rename a file named "myfile.txt" to "newfile.txt".
System Commands
systeminfo: Thesysteminfocommand 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: Thetasklistcommand 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: Thetaskkillcommand is used to terminate the specified process. The/imoption specifies that the process should be terminated by its image name (i.e. the name of the executable file). For example,taskkill /im notepad.exewould terminate any instances of the Notepad application that are currently running
Networking Commands
ipconfig: Theipconfigcommand 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 /allto display detailed information for each adapter oripconfig /releaseto release the IP address for a specific adapter).ping [host_name]: Thepingcommand is used to test network connectivity by sending ICMP echo request packets to the specified host. For example,ping google.comwould send packets to the Google website to test whether it's reachable.tracert [host_name]: Thetracertcommand is used to trace the route that packets take from the local computer to the specified host. For example,tracert google.comwould display a list of routers that the packets pass through on their way to the Google website.
User Account Management Commands
net user [username]: Thenet usercommand 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]: Thenet localgroupcommand 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] /addto add a user to a local group).
Miscellaneous Commands
assoc [.ext]: Theassoccommand is used to display or change file associations for a specific file extension. For example,assoc .txtwould display the current file association for text files (e.g. "txtfile").set: Thesetcommand 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=valueto create a new environment variable named "MYVAR").taskmgr: Thetaskmgrcommand 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]: Thecdcommand is used to change the current working directory to the specified directory. For example,cd C:\Userswould change the working directory to the "Users" folder on the C drive.dir: Thedircommand is used to display a list of files and directories in the current working directory. You can also use various options (e.g.dir /sto display files and directories in subdirectories as well).md [directory]: Themdcommand is used to create a new directory with the specified name. For example,md MyFolderwould create a new directory called "MyFolder" in the current working directory.rd [directory]: Therdcommand is used to delete a directory with the specified name. For example,rd MyFolderwould delete the directory called "MyFolder" in the current working directory.xcopy [source] [destination]: Thexcopycommand is used to copy files and directories from the source location to the destination location. You can also use various options (e.g.xcopy /sto copy subdirectories as well).
System Information Commands
systeminfo: Thesysteminfocommand is used to display detailed information about the local computer's system configuration, including the operating system version, processor type, and installed hotfixes.hostname: Thehostnamecommand is used to display the hostname of the local computer.netstat: Thenetstatcommand is used to display active network connections, including the protocol, local and remote IP addresses, and connection status.tasklist: Thetasklistcommand is used to display a list of running processes on the local computer, including the process name, process ID, and memory usage.driverquery: Thedriverquerycommand 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: Theschtaskscommand 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: Theatcommand 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: Thenetstatcommand 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: Thesccommand is used to manage Windows services. You can use various options to start, stop, pause, resume, or query the status of a service.wevtutil: Thewevtutilcommand is used to manage event logs on the local computer. You can use various options to view, export, or clear event logs.gpresult: Thegpresultcommand 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: Theregcommand 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@echocommand is used to display messages and variables on the console. The@symbol suppresses the echoing of the command itself.set: Thesetcommand 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: Theecho offcommand 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: Thepausecommand 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: Thegotocommand 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: Thecallcommand 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: Thedircommand 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: Thecdcommand 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: Thecopycommand 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: Themovecommand 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: Thedelcommand 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: Themdcommand 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: Thetasklistcommand 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: Thetaskkillcommand 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: Theregcommand 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: Thenetcommand 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: Theipconfigcommand 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: Thecompactcommand 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: Theexpandcommand 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: Themakecabcommand 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: Thetarcommand 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: Thezipcommand 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: Theciphercommand 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: Thesfccommand 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: Thenet usercommand 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: Thenet groupcommand 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: Thenetshcommand 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: Thechkdskcommand 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: Thediskpartcommand 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: Theformatcommand 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: Thefsutilcommand 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: Theforcommand 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: Theifcommand 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: Thegotocommand 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: Thesetcommand 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: Theechocommand 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: Themodecommand 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: Thecolorcommand 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: Thetitlecommand 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: Theclscommand 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: Thepausecommand 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: Theifcommand 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 theelseclause to specify an alternative action if the condition is not met. Example:
bashCopy codeif %ERRORLEVEL% == 0 (
echo Command succeeded
) else (
echo Command failed
)
for: Theforcommand 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 theinclause to specify the values or a file set. Example:
javaCopy codefor %%I in (*.txt) do (
echo Found file: %%I
)
Jump Statements
goto: Thegotocommand is used to transfer the execution of the script to a specific label. You can use the:labelsyntax to define a label and use thegoto labelcommand to jump to it. Example:
phpCopy code:start
echo Starting script...
goto end
:middle
echo Middle of script...
:end
echo End of script.
call: Thecallcommand is used to call a subroutine or another batch file and return to the original script when it finishes. You can use the%0variable to refer to the current batch file and use the%1,%2, ...%9variables 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
COMMENTS (0)
Sign in to post intel on this briefing.