Command Prompt | CMD

Forget drag’n’drop or the clunky control panel. As Darien Graham-Smith reveals, there are far more efficient ways to get the job done.

You probably think of the Windows Command Prompt as a historical hangover from the MS-DOS days. To be fair, that’s pretty much what it is. But it’s still a powerful and efficient way of getting things done, some of which would be complicated or impossible with the mouse. Here’s our pick of commands worth knowing – all of which still work in Windows 10.

1: attrib
View and change file properties

Example: attrib -h -r c:\example.dat

Every file in Windows has a set of basic attributes: some are “system” files, and Windows will complain if you try to delete or move them. Others might be hidden, so by default they won’t be shown in Explorer. The “attrib” command lets you view or change these attributes. The example above removes the “hidden” and “read-only” flags from the target file – useful if you’re clearing out old, unneeded files. You can use wildcards to apply attributes – so you could type “attrib -s *.*” – and apply attributes to folders as well as files.

Note that these low-level attributes co-exist with the more sophisticated multi-user permission system in Windows. Just because a file doesn’t have its “read-only” attribute set, that doesn’t guarantee that you’ll be allowed to delete it.

2: del
Selectively delete files and folders

Example: del /p /s c:\example\

You doubtless know that you can use “del” (or its synonym, erase) to delete files – but the command has some hidden tricks. Use the /p switch, for example, and you’ll be prompted for confirmation before deleting each file. That can be useful if you only want to clean out selected items, although be warned that files deleted through the Command Prompt don’t go to the Recycle Bin – instead they’re instantly wiped.

As our example shows, you can use del with a directory path, rather than a filename, to delete all of the items in a folder in one go. Moreover, if you use the /s switch, files in all subdirectories within the specified path will be recursively deleted. You can’t use del to delete a directory, however: for that you must use the “rmdir” command.

3: robocopy
A hugely versatile file-copy tool

Example: robocopy c:\example\ c:\destination\ /s

If you need to copy files and folders around, “robocopy” has you covered. Our example above copies the contents of a folder into a folder called destination – creating it if it doesn’t already exist. The /s switch means any subdirectories inside our example folder will also be copied.

But robocopy can handle much more complicated tasks. With the right switches, you can tell it only to copy files with certain attributes, or files older or younger than a certain age, or files of a certain size. If files already exist in the destination folder, you can tell it to overwrite them, or overwrite them only if they’re older, or newer. You can even tell it to “mirror” a directory, deleting any files in the destination folder that aren’t in the source location.

4: tree
See what’s where on your hard disk

Example: tree c:\example /f

Ever get lost trying to navigate around a complex directory structure? “Tree” outputs a hierarchical map of all subfolders below the current directory – or below the specified one if you supply a path, as in our example. We’ve also used the /f switch, which lists all of the files in each directory, as well as showing the overarching folder structure.

Unless you’re working with a very simple layout, tree is likely to produce pages and pages of output, which will scroll past in the blink of an eye – so it’s a good candidate for use with “more” (see overleaf). Alternatively, if you’re writing a batch file, you can pipe its output into another command, or redirect its output to a file you can process or peruse at your leisure (see Pipes and redirects tip).

5: where
Search for specific files and folders

Example: where /r /t *.xls

The “dir” command shows the contents of a specified directory, but what if you don’t know which directory a particular file is in? The “where” command is designed for this specific job. The example syntax above will find all files with the specified extension at or below the current directory – the /r tells where to recursively search subdirectories. The /t switch tells it to show complete information about any files it finds, including each file’s size and when it was last modified.

The power of where is that it will let you search in multiple specified locations, or for files matching multiple filename templates: meaning, for example, that you could use one command to find all files from Word and Excel located either on your desktop or in your documents folder.

6: findstr
Find files containing a specific phrase

Example: findstr /s “password”

The “find” command lets you search through a particular file for a specified phrase. Its big brother, “findstr”, is designed for more advanced tasks. It can search for strings in multiple files at once – the example usage above will return all files in the current directory, and all subdirectories, containing the word “password”.

Findstr also supports regular expressions, providing enormous flexibility when it comes to specifying the text you’re looking for. For example, if you want to find files containing either “password” or “passport”, you could search for “pass.o..” – just remember to use the /r switch to activate regular expression parsing. You can also use the /g switch to specify a text file as a source for search strings, so you can easily search for a whole list of phrases all in one go.

7: fc
Find differences between two files

Example: fc /b c:\example1.dat c:\example2.dat

“Fc” stands for file compare, and the command simply compares files to see if they’re identical. What comes back isn’t necessarily a “yes” or “no”: if you’re dealing with text files, fc will output the differences that have been found, and if you use the /n switch, it will tell you on which lines of your files they appear. The /LBn switch will tell fc to give up the comparison if more than n lines don’t match. You can also perform a binary comparison using the /b switch, as in our example usage.

You can use wildcards in the filenames you provide. For example, the command “fc *.txt template.txt” will go through all the text files in the current directory and compare each one in turn to your target file.

8: for
Repeat one action across multiple files

Example: for %a in (*.txt) copy %a c:\textfiles\

The “for” command lets you apply one operation to a whole set of files. Our example above may look daunting, but it’s easy to understand if we break it into two halves. The first half says we want to work with every file matching the pattern in brackets (you can use the /r switch to search not only the current directory, but also subdirectories too). The variable %a is a placeholder for matching files. Meanwhile, the second half of the command tells Windows what to do with each file – in this case, copying it into a folder called c:\textfiles.

The “forfiles” command does the same basic job, but its more complex syntax lets you select files by date, and you can pass a wider range of parameters (such as the path of each matching file) to the target command.

9: more
View information one page at a time

Example: dir | more

When a Command Prompt program has a lot of information to show, it will normally zoom past far too quickly for you to read. You can always scroll upwards to catch up on what you missed, but next time, consider using the “more” command. This makes the output automatically pause after each page; press Return to advance by a single line, or Space to show the next page.

As in our example, a common way to use more is by piping the output from a command through it (see Pipes and redirects tip). You can also use it to read a text file, by entering “more example.txt”. If you do this you can use the /e switch to enable “extended features”, such as the ability to skip both forward and backward by a certain number of lines.

10: sort
Shuffle data into alphabetical order

Example: sort mydata.txt /o sorted.txt

The “sort” command sorts the lines in a file into alphabetical order. By default, the sorted data appears onscreen, but you can use the /o switch to specify a target file, as above. You can also pipe the output of another command into sort; for example, you might use the “where” command to generate a list of files matching a given template, then pipe the output through sort to alphabetise the list.

Note that sort isn’t case sensitive, so “aardvark” will always come before “Apple”. The /r switch tells sort to use reverse alphabetical order. If you just type sort without any parameters at all, it will take its input from the keyboard, so you can paste in a list of data that you’ve copied from elsewhere. To end your list, press Ctrl+Z followed by return.

11: clip
Copy data to the Windows clipboard

Example: dir *.txt | clip

“Clip” copies data onto the Windows clipboard, so you can easily paste it into an application such as Notepad or Excel. One common way to use clip is by piping the output of another command through it. Our example makes a list of all text files in the current directory (the /b switch tells dir to output bare filenames, with no other information), then places it on the clipboard. It’s a lot easier than copying and pasting by hand, especially if you’re dealing with a long list of files.

You can also use redirection to copy the contents of a file to the clipboard, using the syntax “clip < example.txt”. Keep in mind that this doesn’t copy the file itself – you can’t then press Ctrl+C to duplicate it in Windows File Explorer.

12: mklink
Create a shortcut to a specified file or folder

Example: mklink /d c:\docs c:\Users\PCTA\Documents

If you frequently jump back and forth between certain directories, you might find a shortcut useful. The “mklink” command allows you to create a symbolic link – a virtual folder that acts as a gateway to some other location on your hard disk (or even on a different volume).

The example above creates a link that takes you straight from the root directory into our personal Documents folder – to access it, simply type “cd docs”. You can use links in Windows too – your symbolic link will appear as a shortcut.

Note the /d switch above, which tells mklink to create a virtual directory: if you omit it, you can create links to individual files. You can delete a link just as if it were a regular shortcut; the original folder won’t be affected.

13: subst
Assign a virtual drive letter to a path

Example: subst f: c:\example\

Symbolic links are great for hopping from one location to another. If you want a shortcut that works everywhere, “subst” is the answer: it creates a virtual drive that maps to a path on your local disk. Once you’ve set up a virtual drive, it’ll be available in Windows as well as the Command Prompt. To remove a virtual drive, use the /d switch and no target path.Note that subst can’t create mappings across a network. If you want to assign a drive letter to a particular place on a local server or NAS appliance, you can do this through the Windows File Explorer – or you can achieve it from the Command Prompt using the net command: the syntax would be “net use f: \\server\share”.

14: ipconfig
View and troubleshoot network settings

Example: ipconfig /all

Windows’ network settings are, frankly, a bit of a maze. If you just want to check your IP address and gateway settings, it’s much quicker to open a Command Prompt and type “ipconfig” for an overview of your network status. If you use the /all switch, as in our example above, you’ll see a selection of additional details, including your DNS server and DHCP details.

Ipconfig also has a useful selection of troubleshooting functions, accessed with switches. For example, “ipconfig /renew” will try to renew your IP address. If you’re suffering from DNS problems then “ipconfig /displaydns” will show you all cached DNS resolutions (be warned, there may be a lot of them) while “ipconfig /flushdns” will forget them all and tell Windows to look everything up afresh.

15: doskey
Create command-line shortcuts

Example: doskey txt=dir *.txt

In older versions of Windows, “doskey” let you use the cursor keys to browse previously entered commands. Nowadays, that capability is built into the Command Prompt, but doskey has another use, too: it can create macros (that is, shortcuts) for commonly used commands. The example above lets you simply type “txt” to list all the text files in the current directory. If you want your macro to run multiple commands in a row, use the $t marker to tell doskey where to “press Return”.

Note that doskey macros only work within the Command Prompt window where they were defined. If you want your macros to work everywhere, create an AutoRun script that automatically declares them every time you open a new Command Prompt window (see AutoRun fun tip).

16: bootrec
Repair startup information when Windows won’t boot

Example: bootrec /fixboot

When you make a change to your PC hardware, the arrangement of your hard disks may change, preventing Windows from starting up. “Bootrec” can save the day. This tool isn’t built into the standard Command Prompt, but it’s accessible from the recovery console, which you can access by booting from your Windows installation media.

Our example above tells bootrec to write a new Windows-compatible boot sector to the primary hard disk: if that doesn’t get the OS working, try the /fixmbr switch to update the Master Boot Record, then use the /scanos and /rebuildbcd switches in turn to locate your Windows installation and make it bootable. Note that bootrec doesn’t know about dual-boot systems; additional trickery with the bcdedit command will probably be needed to regain access to a second OS.

17: diskpart
Create, edit and delete disks and partitions

Example: select disk 0

“Diskpart” is a toolbox for all sorts of low-level disk operations. To use it, type “diskpart”, then type “list disk” to see the volumes available. Use the select keyword to indicate which disk you want to work on – our example above would select the first hard disk on the system, which is usually the system disk.

From here you are able to resize partitions (using the shrink and extend commands), convert MBR disks to GPT or vice versa, or completely erase a disk using the “clean” command. Using diskpart for the operations has two advantages over Windows: first, it often lets you delete volumes or partitions that appear locked in Windows; and second, you can launch it with the /s switch to pass it a script, to automate processes such as converting or partitioning disks.

18: powercfg
Manage power settings and monitor energy usage

Example: powercfg /setactive SCHEME_BALANCED

The “powercfg” command lets you manage your power options. You can see a list of power schemes by entering powercfg /l, and switch between them using the /setactive switch. Our example syntax sets the current power profile to “Balanced”, using an alias rather than a lengthy hexadecimal GUID (Globally Unique Identifier). Type “powercfg /aliases” to see a list of all available aliases.

Powercfg also provides useful diagnostic information. Entering “powercfg /energy” will analyse your system and create an HTML report warning you of anything that might be draining your battery. The /sleepstudy switch will create an HTML page detailing all of the times your computer has recently been to sleep, meaning you can track down any problems. Furthermore, you can get a report on the state of your battery by using the /batteryreport switch.

19: sfc
Check and repair system files

Example: sfc /scanfile=c:\windows\example.sys

“Sfc” is Windows’ built-in System File Checker. Hopefully, you’ll never need to use the command, but if your computer gets infected by a meddling virus – or if it happens to crash in the middle of an update – then your system could end up in a semi-working state. If either scenario comes to pass, sfc checks that all your system files are working, and that they’re the correct versions: in most cases, you will want to scan your entire Windows folder, which you can do by entering “sfc /scannow”. (If you run the command with no argument you’ll just see the help text.)

Scanning your whole system can be slow, however, and depending on the error messages you’re seeing, you may only want to check one particular file. You can do this using the /scanfile switch, as in our example above.