Command Prompt Cheat Sheet

January 16, 2025

  • cd - Change directory
  • cd \ - Go to root directory
  • cd .. - Go up one directory
  • cd /d D: - Change drive and directory
  • dir - List files and directories
  • dir /a - Show all files including hidden
  • dir /w - Wide list format
  • dir /s - List recursively
  • pushd path - Change directory and save
  • popd - Return to saved directory

File Operations

  • copy file1 file2 - Copy file
  • copy /y file1 file2 - Copy without prompting
  • xcopy dir1 dir2 /e - Copy directory recursively
  • move file1 file2 - Move/rename file
  • ren file1 file2 - Rename file
  • del file - Delete file
  • del /f file - Force delete file
  • rd dir - Remove empty directory
  • rd /s dir - Remove directory and contents
  • md dir - Create directory
  • type file - Display file contents
  • more file - Display file page by page
  • echo text > file - Write to file
  • echo text >> file - Append to file

File Attributes

  • attrib file - Show file attributes
  • attrib +r file - Set read-only
  • attrib -r file - Remove read-only
  • attrib +h file - Hide file
  • attrib -h file - Unhide file
  • attrib +s file - Set system attribute

Search & Find

  • find "text" file - Search for text in file
  • findstr "pattern" file - Advanced search
  • findstr /s "pattern" *.txt - Search in files recursively
  • dir /s *.txt - Find all .txt files
  • where command - Find command location

Text Processing

  • echo text - Display text
  • type file1 file2 - Concatenate files
  • find /c /v "" file - Count lines
  • sort file - Sort file lines
  • sort /r file - Reverse sort
  • more file - Display file page by page

Process Management

  • tasklist - List running processes
  • tasklist /v - Detailed process list
  • taskkill /pid PID - Kill process by ID
  • taskkill /im process.exe - Kill process by name
  • taskkill /f /im process.exe - Force kill process
  • start command - Start new window
  • start /b command - Start in background

System Info

  • systeminfo - System information
  • wmic logicaldisk get size,freespace,caption - Disk space
  • dir /s - Directory size (in listing)
  • mem - Memory usage (older Windows)
  • wmic os get version - OS version
  • hostname - Computer name
  • whoami - Current user
  • date - Display/set date
  • time - Display/set time
  • ver - Windows version

Network

  • ipconfig - IP configuration
  • ipconfig /all - Detailed IP info
  • ipconfig /release - Release IP
  • ipconfig /renew - Renew IP
  • ping host - Ping host
  • tracert host - Trace route
  • netstat -an - Network connections
  • arp -a - ARP table

Environment Variables

  • set - List all variables
  • set VAR=value - Set variable (session)
  • setx VAR value - Set variable (persistent)
  • echo %VAR% - Display variable
  • %PATH% - Command search path
  • %USERPROFILE% - User home directory
  • %TEMP% - Temp directory
  • %CD% - Current directory

Redirection & Pipes

  • command > file - Redirect output to file
  • command >> file - Append output to file
  • command < file - Redirect input from file
  • command1 | command2 - Pipe output
  • command >nul - Discard output
  • command 2>nul - Discard errors
  • command 1>file 2>&1 - Redirect both output and errors

Batch Scripts

  • @echo off - Hide commands in batch
  • echo on - Show commands
  • pause - Wait for keypress
  • call script.bat - Call another script
  • goto label - Jump to label
  • :label - Define label
  • if condition command - Conditional execution
  • for %%f in (*.txt) do command - Loop through files
  • %1, %2 - Command line arguments
  • %ERRORLEVEL% - Last error code

Useful Tips

  • cls - Clear screen
  • exit - Exit command prompt
  • help command - Show command help
  • command /? - Show command help
  • tab - Auto-complete file/directory names
  • F3 - Repeat last command
  • F7 - Command history
  • Up/Down arrows - Navigate command history