Run these in Command Prompt (cmd.exe). Tasks marked (as admin) need Command Prompt opened with Run as administrator. In a batch file, write loop variables with two percent signs, such as %%f. Names, paths and addresses are examples, so swap in your own.
Getting around #
-
Show the current folderWindows
cd -
Go to a folderWindows
cd C:\Projects -
Go up one folderWindows
cd .. -
Switch to another driveWindows
D: -
Change drive and folder in one goWindows
cd /d D:\Backups -
Go to your user folderWindows
cd %USERPROFILE% -
List all built-in commandsWindows
help -
Show help for a commandWindows
ipconfig /? -
Clear the screenWindows
cls -
Show the commands you've typed in this windowWindows
doskey /history -
Find where a program is installedWindows
where python -
Open File Explorer in the current folderWindows
start . -
Open a new Command Prompt windowWindows
start cmd -
Open a file with its default appWindows
start report.pdf
Files and folders #
-
List files and foldersWindows
dir -
List files, including hidden and system onesWindows
dir /a -
List files in this folder and all subfoldersWindows
dir /s -
Find files by name in all subfoldersWindows
dir /s /b *.log -
List files by date, newest firstWindows
dir /o-d -
List files by size, largest firstWindows
dir /o-s -
Show the folder tree with filesWindows
tree /f -
Create a folder and any missing parent foldersWindows
mkdir projects\site\assets -
Create an empty fileWindows
type nul > notes.txt -
Copy a fileWindows
copy report.docx D:\Backup\ -
Copy a folder and everything in itWindows
xcopy source backup /e /i /h -
Copy a folder with RobocopyWindows
robocopy C:\Source D:\Backup /e -
Copy a folder to a share, with retries and a logWindows
robocopy C:\Source \\server01\share /e /z /r:3 /w:5 /log:copy.log -
Make a folder an exact mirror of anotherWindows
robocopy C:\Source D:\Backup /mirWarning: Deletes files in the destination that aren't in the source.
-
Move a file to another folderWindows
move report.docx D:\Archive\ -
Rename a fileWindows
ren old-name.txt new-name.txt -
Change the extension of every .txt fileWindows
ren *.txt *.bak -
Delete a fileWindows
del file.txt -
Delete matching files in all subfolders without askingWindows
del /s /q *.tmpWarning: Deletes without asking and skips the Recycle Bin.
-
Delete an empty folderWindows
rmdir old-folder -
Delete a folder and everything in itWindows
rmdir /s /q old-folderWarning: Deletes without asking, skips the Recycle Bin and can't be undone.
-
Compare two filesWindows
fc file1.txt file2.txt -
Show a file's attributesWindows
attrib file.txt -
Hide a fileWindows
attrib +h file.txt -
Unhide a file and remove read-onlyWindows
attrib -h -r file.txt -
Show a file's SHA-256 hashWindows
certutil -hashfile setup.exe SHA256 -
Create a shortcut (symbolic link) to a file (as admin)Windows
mklink link.txt C:\Data\target.txt -
Create a link to a folder (junction)Windows
mklink /j Docs C:\Users\me\Documents -
Give a folder its own drive letterWindows
subst X: C:\Projects -
Remove a drive letter made with substWindows
subst X: /d
Text and output #
-
Show a file's contentsWindows
type notes.txt -
Show a long file one page at a timeWindows
more app.log -
Print textWindows
echo Hello -
Search for text in a fileWindows
findstr "error" app.log -
Search for text in all files in subfolders, ignoring caseWindows
findstr /s /i "error" *.log -
Show lines that don't contain some textWindows
findstr /v "DEBUG" app.log -
Count lines that contain some textWindows
find /c "error" app.log -
Sort the lines of a fileWindows
sort names.txt -
Sort a file and save the resultWindows
sort names.txt /o sorted.txt -
Save a command's output to a fileWindows
ipconfig /all > network.txt -
Add a command's output to the end of a fileWindows
echo Backup done >> log.txt -
Save output and errors to the same fileWindows
dir C:\missing > out.txt 2>&1 -
Hide a command's outputWindows
ping 8.8.8.8 > nul -
Copy a command's output to the clipboardWindows
ipconfig | clip -
Run a second command only if the first worksWindows
mkdir build && cd build -
Run a second command only if the first failsWindows
ping -n 1 server01 || echo Server is down
Variables and batch files #
-
Show all environment variablesWindows
set -
Show one variableWindows
echo %PATH% -
Set a variable for this window onlyWindows
set APP_ENV=dev -
Set a variable permanently for your accountWindows
setx APP_ENV dev -
Set a variable permanently for everyone (as admin)Windows
setx APP_ENV prod /m -
Show today's date and the timeWindows
echo %date% %time% -
Ask the user to type somethingWindows
set /p NAME=Enter your name: -
Do some arithmeticWindows
set /a 5*4 -
Run a command for each fileWindows
for %f in (*.txt) do echo %f -
Run a command for each number in a rangeWindows
for /l %i in (1,1,5) do echo %i -
Run a command for each line in a fileWindows
for /f "tokens=*" %l in (servers.txt) do ping -n 1 %l -
Do something only if a file existsWindows
if exist config.ini echo Found -
Show the exit code of the last commandWindows
echo %errorlevel% -
Run another batch file and come backWindows
call setup.bat -
Wait 10 secondsWindows
timeout /t 10 -
Wait for a key pressWindows
pause -
Stop a batch file showing each commandWindows
@echo off -
Change the window titleWindows
title Build server -
Change the window coloursWindows
color 0a
System information #
-
Show detailed system informationWindows
systeminfo -
Show when the computer last startedWindows
systeminfo | find "System Boot Time" -
Show the Windows versionWindows
ver -
Show the computer nameWindows
hostname -
Show the current userWindows
whoami -
Show your groups and privilegesWindows
whoami /all -
Show today's dateWindows
date /t -
Show the current timeWindows
time /t -
List installed driversWindows
driverquery -
Show Windows licence and activation detailsWindows
slmgr /dli -
Show which Group Policies apply to youWindows
gpresult /r -
Apply Group Policy changes nowWindows
gpupdate /force -
Create a battery health reportWindows
powercfg /batteryreport -
Open System InformationWindows
msinfo32
Processes #
-
List running processesWindows
tasklist -
Find a process by nameWindows
tasklist /fi "imagename eq chrome.exe" -
Show the services running in each processWindows
tasklist /svc -
End a process by nameWindows
taskkill /im notepad.exe -
End a process by its IDWindows
taskkill /pid 1234 -
Force a process and its child processes to endWindows
taskkill /f /t /im chrome.exeWarning: The program can't clean up, so unsaved work may be lost.
-
Start a programWindows
start notepad -
Start a program and wait for it to finishWindows
start /wait setup.exe -
Run a program as another userWindows
runas /user:Administrator cmd -
Open Task ManagerWindows
taskmgr -
Open Resource MonitorWindows
resmon
Services #
-
List running servicesWindows
sc query -
List all services, including stopped onesWindows
sc query state= all -
Show a service's statusWindows
sc query spooler -
Show a service's settingsWindows
sc qc spooler -
Start a service (as admin)Windows
net start spooler -
Stop a service (as admin)Windows
net stop spooler -
Restart a service (as admin)Windows
net stop spooler && net start spooler -
Make a service start automatically (as admin)Windows
sc config spooler start= auto -
Disable a service (as admin)Windows
sc config spooler start= disabled -
Open the Services consoleWindows
services.msc
Users and groups #
-
List local usersWindows
net user -
Show a user's detailsWindows
net user alice -
Show a domain user's detailsWindows
net user alice /domain -
Create a local user (asks for the password) (as admin)Windows
net user alice * /add -
Change a user's password (as admin)Windows
net user alice * -
Disable a user (as admin)Windows
net user alice /active:no -
Enable a user (as admin)Windows
net user alice /active:yes -
Delete a local user (as admin)Windows
net user alice /deleteWarning: Deletes the account and can't be undone.
-
List local groupsWindows
net localgroup -
List the members of the Administrators groupWindows
net localgroup Administrators -
Add a user to the Administrators group (as admin)Windows
net localgroup Administrators alice /add -
Remove a user from the Administrators group (as admin)Windows
net localgroup Administrators alice /delete -
Show password and lockout settingsWindows
net accounts -
Show who is signed inWindows
query user -
Lock the computerWindows
rundll32.exe user32.dll,LockWorkStation
Network #
-
Show your IP addressWindows
ipconfig -
Show full network details, including DNS servers and MAC addressesWindows
ipconfig /all -
Release your IP addressWindows
ipconfig /release -
Get a new IP addressWindows
ipconfig /renew -
Clear the DNS cacheWindows
ipconfig /flushdns -
Show the DNS cacheWindows
ipconfig /displaydns -
Show MAC addressesWindows
getmac /v -
Check you can reach a hostWindows
ping 8.8.8.8 -
Ping a host until you press Ctrl+CWindows
ping -t 8.8.8.8 -
Ping a host 10 timesWindows
ping -n 10 8.8.8.8 -
Trace the route to a hostWindows
tracert google.com -
Trace the route without looking up namesWindows
tracert -d google.com -
Find where packets are being lostWindows
pathping google.com -
Look up a domain's IP addressWindows
nslookup example.com -
Look up a domain using a specific DNS serverWindows
nslookup example.com 1.1.1.1 -
Look up a domain's mail (MX) recordsWindows
nslookup -type=mx example.com -
Look up the name for an IP addressWindows
nslookup 8.8.8.8 -
List connections and listening ports with process IDsWindows
netstat -ano -
List listening portsWindows
netstat -ano | findstr LISTENING -
Find what's using a portWindows
netstat -ano | findstr :8080 -
Show the program behind each connection (as admin)Windows
netstat -b -
Show network traffic totalsWindows
netstat -e -
Show the routing tableWindows
route print -
Add a route (as admin)Windows
route add 10.0.0.0 mask 255.0.0.0 192.168.1.1 -
Show the ARP tableWindows
arp -a -
Test whether a TCP port is openWindows
curl -v telnet://server01:443 -
Show your public IP addressWindows
curl ifconfig.me -
Download a fileWindows
curl -o file.zip https://example.com/file.zip -
Connect to a server over SSHWindows
-
Copy a file to a server over SSHWindows
scp file.txt [email protected]:/home/user/ -
Connect to a computer with Remote DesktopWindows
mstsc /v:server01
Network adapters and Wi-Fi #
-
List network adaptersWindows
netsh interface show interface -
Show each adapter's IP settingsWindows
netsh interface ipv4 show config -
Set a static IP address (as admin)Windows
netsh interface ipv4 set address name="Ethernet" static 192.168.1.50 255.255.255.0 192.168.1.1 -
Switch an adapter back to DHCP (as admin)Windows
netsh interface ipv4 set address name="Ethernet" source=dhcp -
Set an adapter's DNS server (as admin)Windows
netsh interface ipv4 set dnsservers name="Ethernet" static 1.1.1.1 primary -
Disable a network adapter (as admin)Windows
netsh interface set interface "Ethernet" disable -
Enable a network adapter (as admin)Windows
netsh interface set interface "Ethernet" enable -
Show your current Wi-Fi connection and signalWindows
netsh wlan show interfaces -
List Wi-Fi networks in rangeWindows
netsh wlan show networks mode=bssid -
List saved Wi-Fi networksWindows
netsh wlan show profiles -
Show a saved Wi-Fi network's password (as admin)Windows
netsh wlan show profile name="HomeWiFi" key=clear -
Show proxy settingsWindows
netsh winhttp show proxy -
Reset TCP/IP settings (as admin)Windows
netsh int ip resetWarning: Resets your network settings. Restart afterwards.
-
Reset Winsock (as admin)Windows
netsh winsock resetWarning: Resets network settings, which can affect VPN and security software. Restart afterwards.
Firewall #
-
Show the firewall status for each profileWindows
netsh advfirewall show allprofiles -
Turn the firewall on (as admin)Windows
netsh advfirewall set allprofiles state on -
Turn the firewall off (as admin)Windows
netsh advfirewall set allprofiles state offWarning: Leaves the computer unprotected. Turn it back on when you're done.
-
Allow incoming traffic on a port (as admin)Windows
netsh advfirewall firewall add rule name="Allow 8080" dir=in action=allow protocol=TCP localport=8080 -
Allow incoming ping (as admin)Windows
netsh advfirewall firewall add rule name="Allow ping" protocol=icmpv4:8,any dir=in action=allow -
Block a program from the internet (as admin)Windows
netsh advfirewall firewall add rule name="Block app" dir=out action=block program="C:\Apps\app.exe" -
Show a firewall ruleWindows
netsh advfirewall firewall show rule name="Allow 8080" -
Delete a firewall rule (as admin)Windows
netsh advfirewall firewall delete rule name="Allow 8080" -
Reset the firewall to its defaults (as admin)Windows
netsh advfirewall resetWarning: Removes every rule you've added.
Disks and storage #
-
Show free space on a driveWindows
fsutil volume diskfree C: -
Check a drive for errors without fixing themWindows
chkdsk C: -
Check a drive and fix errors (as admin)Windows
chkdsk C: /f -
Check a drive, fix errors and recover bad sectors (as admin)Windows
chkdsk C: /r -
Format a drive (as admin)Windows
format E: /fs:NTFS /q /v:BackupWarning: Erases everything on the drive.
-
Change a drive's nameWindows
label E: Backup -
List volumes and where they're mountedWindows
mountvol -
Check whether a drive needs optimising (as admin)Windows
defrag C: /a -
Optimise a drive (as admin)Windows
defrag C: /o -
Show BitLocker status for each drive (as admin)Windows
manage-bde -status -
Open Disk CleanupWindows
cleanmgr -
Open Disk ManagementWindows
diskmgmt.msc
DiskPart #
-
Open DiskPart (as admin)Windows
diskpart -
In DiskPart, list disksWindows
list disk -
In DiskPart, choose a disk to work onWindows
select disk 1 -
In DiskPart, list the chosen disk's partitionsWindows
list partition -
In DiskPart, list volumesWindows
list volume -
In DiskPart, wipe the chosen diskWindows
cleanWarning: Erases every partition on the chosen disk. Check the disk number with list disk first.
-
In DiskPart, create a partition using the whole diskWindows
create partition primary -
In DiskPart, quick-format the new partitionWindows
format fs=ntfs quick label=Backup -
In DiskPart, give the partition a drive letterWindows
assign letter=E -
In DiskPart, close DiskPartWindows
exit
Permissions #
-
Show a folder's permissionsWindows
icacls C:\Projects -
Give a user modify access to a folder and everything in itWindows
icacls C:\Projects /grant alice:(OI)(CI)M -
Remove a user's permissions from a folderWindows
icacls C:\Projects /remove alice -
Back up a folder's permissionsWindows
icacls C:\Projects /save perms.txt /t -
Restore permissions from a backupWindows
icacls C:\ /restore perms.txt -
Reset permissions to the inherited defaultsWindows
icacls C:\Projects /reset /tWarning: Removes every permission set directly on the folder and its contents.
-
Take ownership of a folder and everything in it (as admin)Windows
takeown /f C:\Projects /r /d y
Registry #
-
Show the programs that start when you sign inWindows
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" -
Add or change a registry valueWindows
reg add "HKCU\Software\MyApp" /v Mode /t REG_SZ /d dev -
Delete a registry valueWindows
reg delete "HKCU\Software\MyApp" /v Mode /fWarning: Deletes without asking. Export the key first if you might need it back.
-
Back up a registry key to a fileWindows
reg export "HKCU\Software\MyApp" myapp.reg -
Restore a registry key from a fileWindows
reg import myapp.reg -
Open Registry EditorWindows
regedit
Apps and Windows features #
-
List installed appsWindows
winget list -
Search for an app to installWindows
winget search vscode -
Install an appWindows
winget install Microsoft.VisualStudioCode -
See which apps have updatesWindows
winget upgrade -
Update all appsWindows
winget upgrade --all -
Uninstall an appWindows
winget uninstall Microsoft.VisualStudioCode -
List optional Windows features (as admin)Windows
dism /online /get-features /format:table -
Turn on a Windows feature (as admin)Windows
dism /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all -
Install WSL and Ubuntu (as admin)Windows
wsl --install -
List WSL distros and their versionsWindows
wsl -l -v -
Open SettingsWindows
start ms-settings: -
Open Control PanelWindows
control
Repair and troubleshooting #
-
Scan and repair Windows system files (as admin)Windows
sfc /scannow -
Check whether the Windows image is marked as damaged (as admin)Windows
DISM /Online /Cleanup-Image /CheckHealth -
Scan the Windows image for damage (as admin)Windows
DISM /Online /Cleanup-Image /ScanHealth -
Repair the Windows image (as admin)Windows
DISM /Online /Cleanup-Image /RestoreHealth -
Clean up old Windows update files (as admin)Windows
DISM /Online /Cleanup-Image /StartComponentCleanup -
Show the 20 most recent system errorsWindows
wevtutil qe System /c:20 /rd:true /f:text /q:"*[System[(Level=2)]]" -
Clear an event log (as admin)Windows
wevtutil cl ApplicationWarning: Deletes the log's entries and can't be undone.
-
Open Event ViewerWindows
eventvwr -
Open Device ManagerWindows
devmgmt.msc -
Open Reliability MonitorWindows
perfmon /rel -
Test your computer's memory on the next restartWindows
mdsched -
Show the boot configuration (as admin)Windows
bcdedit -
Show what's stopping the computer from sleeping (as admin)Windows
powercfg /requests -
Show what last woke the computerWindows
powercfg /lastwake -
List devices that can wake the computerWindows
powercfg /devicequery wake_armed
Shut down and restart #
-
Shut down nowWindows
shutdown /s /t 0 -
Restart nowWindows
shutdown /r /t 0 -
Shut down in one hourWindows
shutdown /s /t 3600 -
Cancel a scheduled shutdownWindows
shutdown /a -
Sign outWindows
shutdown /l -
HibernateWindows
shutdown /h -
Restart into advanced startup optionsWindows
shutdown /r /o /t 0 -
Restart into the BIOS or UEFI settings (as admin)Windows
shutdown /r /fw /t 0 -
Restart another computerWindows
shutdown /r /m \\server01 /t 0 -
List power plansWindows
powercfg /list -
Turn off hibernation (as admin)Windows
powercfg /h off
Scheduled tasks #
-
List scheduled tasksWindows
schtasks /query -
Show a scheduled task's detailsWindows
schtasks /query /tn "Backup" /v /fo list -
Run a script every day at 2amWindows
schtasks /create /tn "Backup" /tr "C:\Scripts\backup.bat" /sc daily /st 02:00 -
Run a script when you sign inWindows
schtasks /create /tn "Startup script" /tr "C:\Scripts\start.bat" /sc onlogon -
Run a scheduled task nowWindows
schtasks /run /tn "Backup" -
Disable a scheduled taskWindows
schtasks /change /tn "Backup" /disable -
Delete a scheduled taskWindows
schtasks /delete /tn "Backup" /f -
Open Task SchedulerWindows
taskschd.msc