DevTools

Cheatsheet Windows CMD

Comandos para terminal Windows

Back to languages
Windows CMD
35 cards found
Categories:
Versions:

Files and Folders


5 cards
Navigation
cd            # current folder
cd folder     # enter the folder
cd ..        # go up one level
cd \         # go to the root
cd /d D:\    # change drive
dir          # list content

Move around the system.

Attributes
attrib f.txt       # view attributes
attrib +h f.txt    # hide
attrib -h f.txt    # show
attrib +r f.txt    # read only
fc a.txt b.txt    # compare files

File properties.

dir in detail
dir       # list files
dir /a    # include hidden
dir /s    # recursive
dir /od   # sort by date
dir /p    # paginate
tree /f   # folder tree

dir options.

Create and delete
mkdir folder    # create folder
md a\b\c      # create nested
type nul > f.txt # create empty
del f.txt     # delete file
rmdir folder   # remove empty folder
rd /s folder   # remove with content

Create and remove.

Copy and move
copy a.txt b.txt   # copy file
copy *.txt dest\   # copy several
xcopy src dst /s /e # copy folder
robocopy src dst /e # robust
move a.txt b.txt   # move/rename
ren old.txt new.txt # rename

Copy, move and rename.

Text and Search


4 cards
View content
type f.txt    # show everything
more f.txt    # paginate
type f.txt | more # paginate output

# First/last lines (PS):
powershell "Get-Content f -Head 10"
powershell "Get-Content f -Tail 10"

Read text files.

findstr (search)
findstr "error" log.txt  # search
findstr /i "error" f.txt # ignore case
findstr /n "default" f  # with line no.
findstr /s /i "TODO" *.cs # recursive
findstr /v "ok" f.txt  # exclude

Filter lines by pattern.

find
find "text" f.txt   # search string
find /i "text" f    # ignore case
find /c "error" f.txt # count occurrences
find /n "line" f   # with numbers

Simple search.

Redirection
dir > list.txt    # overwrite
dir >> list.txt   # append
command 2> errors.txt # stderr
type a.txt > b.txt  # copy via type
sort f.txt > ord.txt # sort

Output flow.

System and Information


4 cards
System information
systeminfo       # full details
ver             # Windows version
hostname        # PC name
whoami          # current user
echo %USERNAME%  # username
echo %OS%        # operating system

System data.

Hardware
wmic cpu get name    # processor
wmic memorychip get capacity # RAM
wmic diskdrive list brief # disks
wmic bios get serialnumber # serial
msinfo32            # graphical info

Hardware details (WMIC).

Environment variables
set            # list all
echo %PATH%    # view PATH
set NAME=value  # set (session)
setx NAME "value" # permanent
echo %TEMP%    # temporary folder

Environment variables.

Date and time
date /t  # current date
time /t  # current time
echo %DATE%  # date (variable)
echo %TIME%  # time (variable)
timeout /t 5  # wait 5 seconds

Time and pauses.

Processes and Services


4 cards
List processes
tasklist           # all processes
tasklist | findstr chrome # filter
tasklist /v        # detailed
wmic process list brief # via WMIC

View active processes.

Terminate processes
taskkill /IM notepad.exe  # by name
taskkill /PID 1234       # by PID
taskkill /F /IM app.exe  # force
taskkill /T /IM app.exe  # with children

Stop processes.

Services
net start          # active services
net stop spooler   # stop service
net start spooler  # start service
sc query          # services state
services.msc      # graphical manager

Manage Windows services.

Shut down and restart
shutdown /s /t 0  # shut down now
shutdown /r /t 0  # restart
shutdown /a      # cancel
shutdown /l      # log off
shutdown /h      # hibernate

Power control.

Network


4 cards
ipconfig
ipconfig          # adapter IPs
ipconfig /all     # full details
ipconfig /release  # release IP
ipconfig /renew   # renew IP
ipconfig /flushdns # clear DNS cache

Network configuration.

Connectivity
ping google.com   # test connection
ping -n 4 host    # 4 packets
ping -t host      # continuous
tracert host      # packet path
pathping host     # ping + route

Diagnose the connection.

DNS and shares
nslookup dominio.com # resolve DNS
net view          # PCs on the network
net use           # mapped drives
netstat -an       # active connections
arp -a           # ARP table

Local network and DNS.

Firewall and ports
netstat -year      # ports + PID
netsh advfirewall show allprofiles

# Open port (admin):
netsh advfirewall firewall add rule
  name="App" dir=in action=allow
  protocol=TCP localport=8080

Firewall and ports.

Disk and Maintenance


4 cards
Disk space
dir C:\         # free space
fsutil volume diskfree C: # detail
wmic logicaldisk get size,freespace,caption

View available space.

chkdsk
chkdsk C:        # check disk
chkdsk C: /f    # fix errors
chkdsk C: /r    # recover sectors
chkdsk /scan    # online check

Check and repair the disk.

System repair
sfc /scannow       # check files
DISM /Online /Cleanup-Image
  /RestoreHealth  # repair image
cleanmgr          # disk cleanup

Windows maintenance.

diskpart
diskpart        # open utility
list disk       # list disks
select disk 1   # choose disk
list volume     # list volumes
format fs=ntfs quick # format

Disk management (admin).

Scripts Batch


5 cards
Basic script (.bat)
@echo off
echo Hello World!
pause

# Save the script.bat
# Run with double click

First batch script.

Arguments and labels
# %0 = script, %1..%9 = args
echo First: %1

# Jumps with goto
goto :end
:end
echo Terminado
exit /b 0

Parameters and flow.

Variables
set NAME=Anna
echo %NAME%

set /a SUM=5+3
echo %SUM%

set /p RESP="Type: "
echo %RESP%

Variables in batch.

Conditions
if exist f.txt (
  echo File exists
) else (
  echo Not exists
)

if "%1"=="" echo Sem arguments

if/else structures.

Loops (for)
# Iterate files
for %%f in (*.txt) do echo %%f

# Numeric range
for /l %%i in (1,1,5) do echo %%i

# Read file lines
for /f %%l in (list.txt) do echo %%l

Repetition loops.

Shortcuts and Tricks


5 cards
Terminal shortcuts
Tab        # autocomplete
Ctrl+C    # interrupt
Ctrl+V    # paste (Win10+)
arrow ↑/↓  # history
doskey /history # view history
F7        # command list

CMD productivity.

Open tools
explorer .    # open current folder
notepad f.txt # open in Notepad
calc         # calculator
mspaint      # Paint
control      # Control Panel
taskmgr      # Task Manager

Launch applications.

Help
help          # command list
command /?    # command help
dir /?        # dir options
where command # locate executable

Get help.

PowerShell: files
Get-ChildItem       # = dir
Get-Content f.txt   # = type
Copy-Item a b      # = copy
Remove-Item f -Recurse # = rd /s
Get-ChildItem -Filter *.log

PowerShell equivalents.

PowerShell: system
Get-Process        # processes
Stop-Process -Name app # terminate
Get-Service        # services
Get-NetIPAddress   # IPs
Get-CimInstance Win32_OperatingSystem

Administration with PowerShell.