Operating Systems · Topic 7 of 15

Windows File and Disk Commands

Use the Windows Command Prompt commands cd, dir, md, rd, copy, xcopy, robocopy, format, chkdsk, sfc and diskpart with correct syntax and safe habits.

  • 4 min
In a nutshell
  • Open an elevated Command Prompt for commands that change the system.
  • cd changes folders, and cd /d also changes drive.
  • md makes a directory. rd removes one, and rd /s removes it with its contents.

What it is

The Windows Command Prompt runs text commands. Some tasks, such as repairing system files or partitioning a disk from a recovery environment, are easier or only possible from the command line. Commands that change the system must be run in an elevated Command Prompt, opened with Run as administrator.

OpenCommand Prompt as adminType commandwith switchesRead outputFix or repeat

How it works

Navigating and managing folders

Examples in this page are for the Windows Command Prompt.

cd C:\Users          change to a folder
cd ..                go up one level
cd /d D:\Data        change drive and folder together
dir                  list files and folders
dir /a               include hidden and system items
md Reports           make a directory (also mkdir)
rd Reports           remove an empty directory (also rmdir)
rd /s Reports        remove a directory and everything inside

Without the /d switch, cd stays on the current drive. Typing a drive letter such as D: on its own also switches drives.

Copying files

CommandPurpose
copyCopies one or more files, but not folder trees
xcopyCopies files and folder trees, with more switches
robocopyRobust file copy, built for large jobs and backups
copy report.txt D:\Backup
xcopy C:\Data D:\Backup /s /e
robocopy C:\Data D:\Backup /e
robocopy C:\Data D:\Backup /mir

For xcopy, /s copies subfolders that are not empty, and /e also includes empty ones. robocopy retries failed files, keeps timestamps and can resume interrupted copies in restartable mode (/z), which suits network copies. The /mir switch mirrors the source, so it deletes files in the destination that are not in the source.

Formatting and checking disks

format prepares a volume with a file system and erases what was there.

format E: /fs:ntfs /q

The /fs switch selects the file system, and /q runs a quick format.

chkdsk checks a volume for file system errors and bad sectors.

chkdsk C: /f
chkdsk C: /r

The /f switch fixes file system errors. The /r switch also locates bad sectors and recovers readable data, and it takes longer. If the volume is in use, Windows offers to run the check at the next restart.

Repairing system files

System File Checker scans protected Windows files and replaces damaged ones from a cached copy.

sfc /scannow

Run it from an elevated prompt. It is a first step when Windows shows odd errors or missing components.

diskpart

diskpart is an interactive tool for disks, partitions and volumes. It starts its own prompt, and you type commands one by one.

diskpart
list disk
select disk 1
clean
create partition primary
format fs=ntfs quick
assign letter=E
exit

The list disk command shows the disks, and select disk 1 makes disk 1 the target. The clean command wipes the partition information from the selected disk. Always confirm the disk number before running it.

Getting help

Add /? after any command to see its syntax and switches.

robocopy /?
Unsure about syntaxType command /?help switchRead switchesRun the command
Watch out

format, diskpart clean and robocopy /mir can destroy data when pointed at the wrong target, so check drive letters and disk numbers before pressing Enter.

What to remember for the exam

  • Open an elevated Command Prompt for commands that change the system.
  • cd changes folders, and cd /d also changes drive.
  • md makes a directory. rd removes one, and rd /s removes it with its contents.
  • xcopy /s /e copies folder trees. robocopy is the stronger copy tool.
  • robocopy /mir mirrors the source and deletes extra files in the destination.
  • chkdsk /f fixes errors. chkdsk /r also finds bad sectors.
  • sfc /scannow repairs protected system files.
  • diskpart uses list disk, select disk, clean, create partition and format.
  • command /? shows help for any command.

Common mistakes

Using copy for folder trees. The copy command handles files. Folder structures need xcopy or robocopy.

Confusing /f and /r. Both are chkdsk switches, but /r does a deeper, slower scan for bad sectors.

Forgetting to select the disk in diskpart. Commands act on the selected disk, so selecting the wrong one wipes the wrong drive.

Running without elevation. Many of these commands fail with an access denied message in a normal prompt.

Check yourself

  • Which command repairs damaged protected Windows files?

    sfc /scannow, run from an elevated prompt.

  • What does robocopy /mir do to files that exist only in the destination?

    It deletes them so the destination matches the source.

  • How do you see the switches for a command?

    Type the command followed by /?.

Topic 7 of 15 in Operating Systems

Test yourself on Operating Systems

The full CompTIA A+ 220-1202 question bank has 835 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by CompTIA. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.