CMD commands cheat sheet

This cheat sheet brings together the most useful commands of CMD, the Windows command prompt, for working on a server: files and folders, backups with robocopy, permissions, services, networking, users, scheduled tasks, and batch files. You can search for a command, filter by topic, and copy it with one click.

Help

  • help

    Lists the built-in CMD commands, with a short description of each one.

  • robocopy /?

    Adding /? after a command name shows its help: the syntax, the options, and sometimes examples.

Navigation and folders

  • cd /d D:\backup

    Switches to a folder on another drive. Without /d, the cd command does not change the current drive. The cd command on its own shows the current folder, and cd .. moves up one folder.

  • dir /a

    Lists the contents of the folder, including hidden files and system files, which are not shown by default.

  • dir /o:-s

    Lists the contents of the folder sorted by size, from the largest file to the smallest.

  • dir D:\data\*.log /s /b

    Searches for files by name in the folder and in all its subfolders, and shows one full path per line.

  • tree D:\data /f /a

    Shows the folder structure as a tree, including the file names, in plain text characters that are easy to copy to a file.

  • mkdir D:\backup\2026\daily

    Creates a folder, including any intermediate folders that don't exist yet.

  • rmdir /s /q D:\temp\old

    Caution: deletes the folder with all its files and subfolders, without asking for confirmation. The files are not moved to the Recycle Bin.

Files: view, copy, and delete

  • type C:\logs\app.log | more

    Shows a text file one screen at a time. The Space key moves to the next screen, and the q key exits. For a short file, the type command on its own is enough.

  • copy web.config web.config.bak

    Copies a file, for example as a quick backup before you edit it. The /y option overwrites an existing file without asking.

  • move C:\temp\report.txt D:\archive\

    Moves a file to another folder.

  • ren web.config web.config.old

    Renames a file. The command does not move the file to another folder.

  • xcopy D:\site E:\backup\site /e /i /h /y

    Copies a folder with all its subfolders, including empty folders, hidden files, and system files. The /i option creates the destination folder when it doesn't exist, and the /y option overwrites files without asking. For backing up large folders, robocopy is more convenient.

  • del /s /q C:\temp\*.tmp

    Caution: deletes all the matching files in the folder and in all its subfolders, without asking for confirmation. Deleted files are not moved to the Recycle Bin, so run dir with the same pattern first and check the list.

  • attrib -r D:\site\*.* /s

    Removes the read-only attribute from all the files in the folder and its subfolders. The attrib command without options shows the attributes: R for read-only, H for hidden, S for system file, and A for archive.

Searching text in files

  • findstr /i "error" C:\logs\app.log

    Shows the lines in the file that contain the text, ignoring case. The /n option adds the line number, and the /v option shows only the lines that don't contain the text.

  • findstr /c:"Access is denied" C:\logs\app.log

    Searches for a whole phrase that contains spaces. Without /c:, each word is a separate search, and every line that contains any of the words is shown.

  • findstr /s /i /m "connectionString" *.config

    Searches all the config files in the current folder and its subfolders, and shows only the names of the files where the text was found.

Backup and copy with robocopy

  • robocopy D:\data E:\backup\data /E /R:2 /W:5

    Copies a folder with all its subfolders, including empty folders. On a repeat run, identical files are skipped, and only new or changed files are copied. The /R:2 /W:5 options limit the retries to two, 5 seconds apart. Without a limit, a locked file can hold up the copy for a long time.

  • robocopy D:\data E:\backup\data /MIR /L

    A test run: shows which files would be copied and which would be deleted at the destination, without making any changes. We recommend running it before every run with /MIR.

  • robocopy D:\data E:\backup\data /MIR /LOG:C:\logs\backup.log

    Caution: creates an exact copy of the source at the destination, and deletes from the destination any files and folders that don't exist in the source. Swapping the source and the destination by mistake deletes data. The /LOG option saves the output to a file and overwrites an existing file, and the /LOG+ option appends to the end of the file.

  • robocopy D:\data \\backupserver\backup /E /Z /MT:16 /LOG+:C:\logs\backup.log

    Copies to a shared network folder with 16 copies running in parallel, which is faster for folders with many files. The default for /MT is 8, and the range is 1 to 128. The /Z option resumes an interrupted file copy from the point where it stopped.

  • robocopy D:\logs E:\archive\logs /MOV /MINAGE:30

    Moves files older than 30 days to an archive, and deletes them from the source after copying them.

Permissions and ownership

  • icacls D:\data

    Shows the permissions of a folder or a file. F is full control, M is modify, RX is read and execute, R is read, and W is write. The (OI) and (CI) flags mark a permission that is inherited by files and subfolders.

  • icacls D:\data /grant "username:(OI)(CI)RX"

    Grants a user read and execute permission on the folder, inherited by all its files and subfolders. The permission is added to the existing permissions, while /grant:r replaces the permissions previously set for the user.

  • icacls D:\data\* /save C:\backup\acl-data.txt /T

    Saves the permissions of all the files and folders to a file, before a major change. To restore them, run icacls D:\data\ /restore C:\backup\acl-data.txt.

  • icacls D:\data /reset /T /C

    Caution: replaces the permissions with the default permissions inherited from the parent folder, and removes permissions that were set manually. The /C option continues even when an error occurs on a specific file.

  • takeown /f D:\olddata /a /r /d y

    Caution: gives the Administrators group ownership of the folder and everything in it, for example when access to an old folder is blocked. The /d y option answers "yes" for folders you have no access to, and in those folders, the permissions are replaced with full control for the user only. After you take ownership, grant permissions with icacls.

System information

  • systeminfo

    Shows the system details: the server name, the Windows version, the last boot time, memory, installed updates, and network adapters. For a quick check of the version number only, run ver.

  • whoami /all

    Shows the current user, the groups the user belongs to, and the user's privileges. Useful for checking whether the window was opened with administrator privileges.

  • set

    Shows all the environment variables. When you type a letter after it, for example set P, only the variables that start with that letter are shown, and the echo %PATH% command shows the value of a single variable.

  • setx APP_ENV production

    Saves a permanent environment variable for the user. The value is available only in windows opened from now on, not in the current window. With /m, the variable is saved for the whole system, in a window with administrator privileges. Caution: setx truncates values longer than 1024 characters, so don't use it to update a long variable such as PATH.

Processes and services

  • tasklist

    Lists the running processes, with the process ID (PID) and the memory usage of each process.

  • tasklist /fi "pid eq 1234"

    Shows the name of a process by its PID, for example after netstat -ano showed you which process is listening on a port.

  • taskkill /IM notepad.exe /F

    Caution: forcibly closes all the processes with this name, and any unsaved data in them is lost. The /T option also closes the processes that the process started. To ask a single process to close without force, run taskkill /PID followed by the process ID.

  • sc query Spooler

    Shows the state of a service by its internal name, here the print spooler service. The sc query command without a name shows all the active services, and the sc qc Spooler command shows the configuration of the service.

  • sc stop Spooler

    Stops the service, and the sc start Spooler command starts it again.

  • sc config Spooler start= disabled

    Changes the startup type of the service: auto, delayed-auto, demand (manual), or disabled. You must leave a space after the equals sign, or the command fails.

Maintenance, repair, and restart

  • sfc /scannow

    Scans the protected Windows system files and repairs corrupted files when possible. Requires a window with administrator privileges.

  • DISM /Online /Cleanup-Image /RestoreHealth

    Repairs the Windows component store, which sfc restores files from. When sfc cannot repair a file, run DISM and then run sfc again.

  • chkdsk D: /f

    Caution: fixes file system errors on the drive, and locks the drive to do so. When the drive cannot be locked, for example the system drive, you are asked whether to run the check the next time the server starts. On a large drive, the check can take a long time, and the /r option makes it even longer. The chkdsk command without options only checks the drive and shows a report.

  • gpupdate /force

    Reapplies all the Group Policy settings, including settings that haven't changed.

  • shutdown /r /t 0

    Caution: restarts the server immediately, and unsaved data in open programs may be lost. The RDP connection drops, and you can reconnect once the server is back up. With /t 300, the restart happens in 5 minutes, and the shutdown /a command cancels it.

  • shutdown /s /t 0

    Caution: shuts down the server. After the shutdown, you can't connect to the server remotely until it is started again, for example from the control panel of your server provider.

Networking

  • ipconfig /all

    Shows the full network details of each adapter: IP addresses, default gateway, DNS servers, MAC address, and DHCP settings. The ipconfig command on its own shows a summary.

  • ipconfig /flushdns

    Clears the DNS cache of the server, for example after a domain record changes. The ipconfig /displaydns command shows the records in the cache.

  • ipconfig /release

    Caution: releases the address obtained from DHCP, and the ipconfig /renew command requests a new one. Relevant only for a network adapter that gets its address from DHCP. On a server you are connected to remotely, the command drops the connection.

  • ping -n 10 example.com

    Checks whether the remote server responds and how long the response takes, with 10 requests instead of the default 4.

  • ping -t 1.1.1.1

    Sends requests nonstop until you press Ctrl+C, which is useful for spotting intermittent disconnections. Ctrl+Break shows statistics and keeps going.

  • tracert -d example.com

    Shows the routers on the way to the destination. The -d option skips resolving the addresses to names, so the result comes back faster.

  • pathping /n example.com

    Shows the routers on the way to the destination, and also measures packet loss at each router and on each link. Collecting the data takes a few minutes before the result is shown.

  • nslookup -type=mx example.com 1.1.1.1

    Shows the MX records of the domain from a specific DNS server. Instead of mx, you can type a, aaaa, txt, or ns, and without -type=mx, the IP addresses of the domain are shown.

  • netstat -ano | findstr :443

    Shows the connections and listening ports on port 443, with the PID of the process in the last column. The netstat -ano command on its own shows all the connections.

  • route print

    Shows the routing table of the server, for IPv4 and IPv6.

  • arp -a

    Shows the ARP cache: the IP and MAC addresses of the devices that the server has communicated with on the local network.

  • curl -I https://example.com

    Shows the HTTP headers that the website returns. curl is built into recent versions of Windows and Windows Server. In a Windows PowerShell window, type curl.exe, because there the word curl runs a different command.

Firewall (netsh)

  • netsh advfirewall show allprofiles state

    Shows whether Windows Firewall is on in each of the profiles: Domain, Private, and Public.

  • netsh advfirewall firewall add rule name="Block 198.51.100.25" dir=in action=block remoteip=198.51.100.25

    Blocks inbound traffic from an IP address. Caution: make sure it isn't the address you use to connect to the server.

  • netsh advfirewall firewall delete rule name="Block 198.51.100.25"

    Deletes the rule by its name. When several rules have the same name, all of them are deleted.

Users, groups, and shares

  • net user username

    The net user command on its own lists the local users, and with a user name, it shows the account details: whether the account is active, when the password expires, the last logon, and the groups the user belongs to.

  • net user username * /add

    Creates a new user and prompts you to type a password. The password is not shown on the screen and is not saved in the command history.

  • net localgroup administrators

    Shows the users and groups that have administrator privileges on the server.

  • net localgroup administrators username /add

    Caution: adds the user to the Administrators group, with full control of the server. The same command with /delete instead of /add removes the user from the group.

  • net localgroup "Remote Desktop Users" username /add

    Allows the user to connect to the server over RDP, without administrator privileges.

  • net use Z: \\fileserver\backup /persistent:yes

    Maps a shared folder to drive Z, which is kept after you log in again. The /user:name option connects as a different user, and the net use Z: /delete command disconnects the drive.

  • net share Backup=D:\backup /grant:username,READ

    Shares a folder on the network with read permission for a user. The permissions of the files themselves, set with icacls, also apply to the access. The net share command on its own lists the existing shares.

Scheduled tasks

  • schtasks /query /tn "DailyBackup" /v /fo list

    Shows the full details of a task: when it last ran, the result, the next run, and the account it runs under. The schtasks /query command on its own lists all the tasks.

  • schtasks /create /tn "DailyBackup" /tr "C:\scripts\backup.bat" /sc daily /st 02:00 /ru SYSTEM

    Creates a task that runs every day at 02:00 under the SYSTEM account, which doesn't require a password. Use the full path to the file, and write the time in 24-hour format.

  • schtasks /run /tn "DailyBackup"

    Runs the task immediately, to check that it works as expected.

  • schtasks /delete /tn "DailyBackup" /f

    Deletes the task without asking for confirmation.

Batch files

  • @echo off

    A common first line in a batch file: hides the commands themselves, so only their output is shown. A line that starts with rem is a comment.

  • set "BACKUP=D:\backup"

    Sets a variable in the current window or script only. The quotes prevent an extra space at the end of the value, and you then use the variable as %BACKUP%.

  • if not exist "D:\backup" mkdir "D:\backup"

    Creates the folder only when it doesn't exist. The if exist condition checks the opposite.

  • for %%f in (*.log) do echo %%f

    Runs a command on every file that matches the pattern. In a batch file, you write %%f, and when you type the command directly in a CMD window, you write %f.

  • for /f "tokens=1,2 delims=," %%a in (users.csv) do echo %%a %%b

    Reads a file line by line and splits each line at the commas: the first column goes into %%a and the second into %%b.

  • if errorlevel 1 echo Failed

    Checks the exit code of the previous command. The condition is true when the code is 1 or higher, and the echo %ERRORLEVEL% command shows the code itself.

  • mkdir D:\backup && echo Created

    The && operator runs the second command only when the first one succeeded.

  • ping -n 1 example.com >nul || echo Host unreachable

    The || operator runs the second command only when the first one failed, and >nul hides the output of the first command.

  • robocopy D:\data E:\backup /E > C:\logs\copy.txt 2>&1

    The > operator saves the output to a file and overwrites it, and the >> operator appends to the end of the file. Adding 2>&1 also saves the error messages to the same file.

CMD or PowerShell

CMD is the classic Windows command prompt, and it is available in every version of Windows and Windows Server. Most of the tools on this page, such as robocopy, ipconfig, netstat, and icacls, are separate programs that also work in a PowerShell window. Commands built into CMD, such as dir /s, copy, and del, and the batch file syntax work only in a CMD window. In Windows PowerShell, type sc.exe and curl.exe with the extension, because there the words sc and curl run other commands. PowerShell commands are on the PowerShell commands page.

A window with administrator privileges

Commands that change system settings, such as sc config, sfc, chkdsk, net user, and netsh, require a CMD window with administrator privileges. Search for cmd in the Start menu, right-click it, and select Run as administrator. Before you delete files, run dir with the same pattern, and before you change the firewall, make sure you have another way to access the server.

robocopy exit codes

The robocopy command returns an exit code from 0 to 7 when the copy finished without a failure, for example 1 when files were copied successfully, and a code of 8 or higher when at least one file was not copied. So in a batch file, you check for a robocopy failure with the if errorlevel 8 condition, not with if errorlevel 1, which is also true after a successful copy.

What happened to the wmic command

Microsoft has deprecated the wmic tool, and it has been removed from newer versions of Windows 11, so don't rely on it in new scripts. Use PowerShell commands such as Get-CimInstance instead. WMI itself, which the tool relies on, keeps working as usual.

Did you know?

On a Windows VPS, you connect over RDP with full administrator access, so you can run every command on this page, from backing up folders with robocopy to scheduled tasks and batch files.

Windows VPS plans

CMD commands FAQ

When is CMD the better choice, and when is PowerShell?

CMD is convenient for short, familiar commands, such as ipconfig, ping, and robocopy, and for existing batch files, which run on every version of Windows without any extra setup. PowerShell is better suited to advanced server management, such as services, the firewall, users, and event logs, and to scripts that process data. Most external tools, such as robocopy and netstat, work in both environments, so you can combine the two.

How do I open CMD as an administrator?

Type cmd in the Start menu, right-click Command Prompt, and select Run as administrator. The window title then starts with the word Administrator. To make sure the window has administrator privileges, run whoami /groups and check that the Administrators group appears in the list.

How do I back up a large folder without copying all the files again every time?

The robocopy command with the /E option copies the folder with all its subfolders, and on the next run, it skips files that haven't changed and copies only new or changed files. You can run it every night with a scheduled task created with schtasks, and save the output to a log file with /LOG+. The /MIR option also deletes from the destination any files that were deleted from the source, so run it with /L first to check.

How do I find which program is using a port on a Windows server?

Run netstat -ano | findstr :443, where 443 is the port you are checking. The last column shows the process ID (PID), and then the tasklist /fi "pid eq 1234" command, with the number you got, shows the name of the program. If needed, you can stop the process with taskkill /PID followed by the number.

What replaces the wmic command?

Microsoft has deprecated wmic, and the tool has been removed from newer versions of Windows 11. The information you used to get from it, such as processor, disk, process, and service details, is now available through the PowerShell command Get-CimInstance, for example Get-CimInstance Win32_OperatingSystem. Some of the information is also available through CMD commands, such as systeminfo and tasklist.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement