Windows Command Line Cheatsheet

Sources:

  • **
title: Contents 
style: nestedList # TOC style (nestedList|inlineFirstLevel)
minLevel: 1 # Include headings from the specified level
maxLevel: 4 # Include headings up to the specified level
includeLinks: true # Make headings clickable
debugInConsole: false # Print debug info in Obsidian console

Overview

The Windows Command Line, or the Windows Command Prompt, is the Command Line Interface (CLI) on Tool - Microsoft Windows machines, analogous to the Terminal in Unix/Linux.

It emulates many command-line abilities in Microsoft’s deprecated text-only operating system MS-DOS (but it’s not MS-DOS).

TIP

Another name for the Windows Command Line is “cmd”.

Scripts containing Windows commands (Batch Scripts) have .bat as the file extension. All cmd commands are case-insensitive, so arp and ARP are the same.

TIP

If you need help using any command, add /? to it.

Cheatsheet

  • Navigation
  • File Management
CommandDescription
c:Change the current drive to the C:\ drive
d:Change the current drive to the D:\ drive
CD c:\path\to\my_folderChange directory to c:\path\to\my_folder
CD ..Navigate to the parent directory of the current working directory
CD .\new_folderNavigate to the folder new_folder located in the current working directory
CD /D d:\videos\Change the current drive to D:\ and access the folder videos on it.
DIRDisplay files and folders in the current directory
DIR /A c:\apps\Display files and folders in the directory c:\apps\
DIR /A:DDisplay only folders (D: directories)
DIR /A:-DDisplay only files (D: directories; -: not)
DIR /A:HDisplay hidden files and folders
DIR /ODisplay files and folders sorted alphabetically
DIR /O:SDisplay files and folders sorted by file size from smallest to largest
DIR /O:-SDisplay files and folders sorted by file size from largest to smallest
DIR /BDisplay only the names of files and folders in the current working directory
SORTTake input from a source file/pipeline, sort its contents alphabetically (default: A to Z; in reverse: Z to A), and display the output
SORT "C:\music\playlist.m3u"Sort the contents of C:\music\playlist.m3u line by line
DIR /B | SORT /R /O ZtoA.txtList all file and folder names in the current working directory, sort them in reverse alphabetical order, and save the sorted output to a file ZtoA.txt:
MOVEMove a file or files
MOVE c:\f1\text.txt c:\f2Move a file text.txt from one folder c:\f1 to another folder c:\f2
MD new_folderMAKEDIR new_folderCreate a new folder called new_folder in the current directory
RD new_folderRMDIR new_folderDelete the folder called new_folder in the current directory
TREEShow the directory structure of a disk/folder
TREE "C:\Program Files"Show the directory structure of the folder “Program Files” on the disk C:|
TREE C:\ /FDisplay the names of the files in each folder in the directory structure of the C:\ drive
ATTRIBDisplay/set the attributes of the files in the current directory
ATTRIB +H +S +R myItemHide a file/folder myItem
ATTRIB -H -S -R myItemUnhide a file/folder myItem

File Management

CommandDescription
COPY text.txt C:\schoolworkCopy the file text.txt to a folder with the path C:\schoolwork
DEL text.txtERASE text.txtDelete the file text.txt
REN text.txt script.batRENAME text.txt script.batRename a file text.txt to script.bat
REPLACE .\src\hey.txt .\destOverwrite; replace a file named hey.txt in a local folder src with another hey.txt in a local folder dest, both files sharing the same name.
Warning: Don’t specify .\dest\hey.txt anywhere here.
XCOPYCopy files and directory trees to another folder.
XCOPY is similar to COPY but with additional switches to specify the source and destination paths in detail.
XCOPY /S folder1 folder2Copy folders and subfolders of folder1 to folder2
ROBOCOPYRobust copying of files and directories: by default, such copying only occurs if the source and destination differ in time stamps or file sizes.
EXPAND gameData.cabDecompresses the compressed .CAB cabinet file gameData.cab
FC file1.ext file2.extCompare the contents of two files (file1.ext, file2.ext) and display non-matching lines
COMP file1.ext file2.extCompare the contents of two files (file1.ext, file2.ext) and display non-matching items
FIND "python" in run.batOutput every line that contains a text string (which you must enclose in quotation marks) “python” in the file run.bat
FIND /C "python" in run.batCount every line that contains a text string (which you must enclose in quotation marks) “python” in the file run.bat
PRINT resume.txtPrint contents of a file resume.txt
OPENFILES /QUERYQuery/display open files
OPENFILES /DISCONNECTDisconnect files opened by network users.
TYPE test.txtDisplays the contents of the file test.txt
TYPE playlist.m3u | SORT /unique /o C:\work\unique_play.m3uSort a file playlist.m3u and output only the unique values to a file C:\work\unique_play.m3u
MOREDisplay contents of one or more files, one screen at a time.
ASSOCDisplay or change the association between a file extension and a file type
NOTEPADOpen the Notepad application from cmd
NOTEPAD filename.extOpen a file filename.ext in Notepad

Disk Management

CommandDescription
CHKDSKCheck and repair disk problems (local disks only)
CHKDSK /F A:Fix errors on A: drive
CHKDSK /R A:Recover data on A: drive
CHKDSK /X A:Dismount drive A:
CIPHER /E classifiedEncrypt the folder classified
CIPHER /D secret_recipe.txtDecrypt the file secret_recipe.txt
DEFRAGDisk Defragmentation
CHKNTFSDisplay/modify disk-checking on startup
COMPACTDisplay/change the compression of files in NTFS partitions
CONVERTConvert FAT disk volume to NTFS
DISKPARTDisplay and adjust disk partition properties
FORMATFormat the disk
FSUTILFile system management
LABEL d:xRename disk D:\ to X:|
SUBST p: c:\taxesAssign drive P:\ to the local folder c:\taxes
SUBST p: /DRemove the path represented by P:|
RECOVER d:\data.datRecover a file data.dat from a bad or defective disk D:|
VOLDisplay current disk volume label and serial number
POWERCFGControl power settings and configure Hibernate/Standby modes
SFC /SCANNOWScan and update protected system files