A command line interpreter is available in all Windows operating systems. The Windows command prompt (CMD) is a powerful tool used to, as its name implies, execute commands.
These commands range from simple things like switching directories and viewing the contents of a folder, to more advanced things like automating tasks by means of batch files and scripts, advanced troubleshooting and “hacking”, or installing dependencies and packages.
Depending on your workload, regular CMD may not be enough. You may need to use more advanced tools like PowerShell or Azure PowerShell. But let’s focus on the old and faithful CMD here.
With all the power packed in this important tool, it could get complicated and confusing at times. Most regular computer users have no idea how to use the Windows command prompt. Even some advanced users struggle with it. The fact however, remains that CMD wasn’t built for experts only.
This article shows you how to do some basic stuff via CMD.
First Things First
Few things to point out before we proceed. First, the commands here will work well on Windows 7, 8, and 10, and even older Windows versions. But my screenshots are taken on Windows 10.
Second, for the most part, the Windows command line and most CMD commands are not case-sensitive (unlike Linux). That means if you see a command typed here as dir, you can type it as DIR, diR, or DiR. There are, however, some commands with additional options (switches) that are case-sensitive. But we won’t be getting that far in this article.
1. Listing Directory Contents And Changing The Working Directory
When you fire up CMD by running the cmd command in Run, you see something like the screenshot below. The current directory is C:\Users\<YourUsername> by default.
To follow along, you may wish to open up File Explorer and go to the corresponding directory. To change the working directory, we will be using the cd command, which means “Change Directory”.
For example, the command cd\ takes you to the top of the directory tree. To see how it works, type cd\ and press Enter on your keyboard. You will be taken to the top of the directory tree which in this case is the C: drive.
If you need to go to a specific folder from this drive run the command cd FolderName. Sub-folders must be separated by a \ (backslash character). For example, if you want to go back to your user directory, type cd Users\<YourUsername> and then press Enter on your keyboard.
When you need to go one folder up, use the cd.. command. So, if you’re in the C:\Users\<YourUsername> folder and you want to go one level up to the C:\Users folder (the parent directory), type cd.. and press Enter.
Say I go back to my users directory (the default one when CMD is launched). Let’s see what’s inside this current directory. Type dir and hit Enter. You should see something closely resembling the screenshot below – a list of all the files and folders in the directory, along with the dates on which they were created.
2. Changing Drives
Sometimes, you may need to navigate to another drive altogether. The default operating system drive on most PCs is the C: drive. However, you may also have a D: drive with stuff stored there.
To change drives, type the name of the drive, followed by a colon (:) symbol. Assuming your PC has a drive labelled D: simply type d: and hit Enter.
Sometimes, you may need to do some more-than-basic directory navigation. Suppose you are in the root of drive D: on your machine, but you want to navigate to the Users folder on the C: drive in one step. You’ll have to use the /d switch.
The /d switch is used to navigate into a specific folder from another disk volume.
In the above scenario, you’ll have to navigate out of the current folder, using the cd command. Then you tell Windows to take you out of the current drive using the /d parameter. And then you specify the exact directory path you wish to enter.
All of that can be done with this one command: cd /d C:\Users
When using the /d parameter if you don’t type the exact folder to which you want to navigate, stopping only at the drive letter (C: for example), Windows will take you to the most current folder in which you were.
3. Creating New Directories
This one is fairly simple. The mkdir command (or the md command) creates a new folder in your working directory. mkdir stands for “make directory”.
Give it a shot – create a folder by typing the command in this format: mkdir FolderName
Confirm that it worked by navigating to the new folder you created. Or use the dir command to view the contents of your current folder and the newly created folder should appear in the list. Note that these directory commands work relative to the current directory.
At times, you may want to create a sub-folder while creating its parent folder. Simply use the mkdir command in the following format: mkdir ParentFolder/SubFolder
4. Copying Files And Folders Using The Command Prompt
Files can be copied using the copy command. Use the following syntax: copy Location\Filename.extension NewLocation\NewName.extension
If you are copying files within the same directory, you do not have to put the path. You can just do: copy Filename.extension NewName.extension
For copying whole folders and their contents (as opposed to individual files) use the xcopy command instead. When using the xcopy command, you will often also use the /s and /i operators.
The /s switch ensures that all directories and sub-directories are copied, but leaves out empty ones, while the /i switch creates a new folder in the target destination if it does not exist. This is the syntax: xcopy /s /i D:\Test D:\Backup_Test
5. Renaming Files Via CMD
To rename files and folders, use the ren command. This format is used to rename folders: ren OldFolderName NewFolderName
To rename files, use ren OldFileName.extension NewFileName.extension
So, suppose you wanted to rename a folder from “MyGames” to “OurGames”. You’ll type ren MyGames OurGames
And to rename a file from “passport.jpg” to “cropped_passport.jpg”, type ren passport.jpg cropped_passport.jpg
Be careful when changing filenames via the command prompt so that you don’t inadvertently change the file extension as well.
In addition, if you have spaces in the file or folder names, remember to wrap the names in double quotes when using the ren command.
The above principle applies to many other commands as well. In general, paths (or folder names or filenames) with spaces in them should be wrapped in double quotes.
6. Deleting Files And Folders
The del command is used to delete files (and folder contents). To delete the contents of a folder, just type del followed by the name of the folder. You will be prompted to confirm that you want to delete. Type y (for “yes”) and hit Enter.
NOTE: If you also want hidden files in the folder to be deleted, you must add the /h parameter.
To delete a single file, type type the del command, followed by the path to the file, including its file extension. So, your command should look something like del test_folder\test_file.txt
The del command does not work for deleting a folder. Only the folder contents are deleted, leaving an empty folder behind. If you want to delete the container folder, use the rd command. Type rd followed by the name of the folder, hit Enter and everything will be gone.
The del command is more powerful when used with wildcards.
For example…
- To delete all documents with a .jpg extension, simply type del *.jpg
- To delete all files with filenames beginning with “Demo”, type del Demo*.*
CMD will search for all files starting with “Demo” irrespective of their file extensions and delete them in the working folder, or the folder you specify. - To delete all files in the working folder, type del *.*
7. Launching Applications From The Command Prompt
To launch an application from the command prompt, navigate (using the cd command) to the folder where the application’s executable is and the enter the program’s executable filename and extension.
Let’s use MS Paint as an example. Assuming you just launched CMD and are in your users folder (by default). Type cd..\.. and hit Enter to take you to the root of your C: drive. Then, type cd \Windows\System32 and hit Enter.
Now, type mspaint.exe and hit Enter. The MS Paint application will be launched.
8. Getting Help In The Command Prompt
Working with CMD and getting stuck? Don’t fret about it. You can get help from CMD itself without having to Google everything. To get help, just type the help command and hit Enter.
You will see a list of all available commands displayed in alphabetical order as shown in the screenshot below.
To learn more about (or get help with) a particular command, type help followed by the name of the command. So, to find out more about the dir command for example, just type help dir and you will be presented with detailed help information for the dir command. Looks like this:
Alternatively, instead of typing help dir to find help for the dir command, you can type the command name followed by the /? parameter. So both help dir and dir /? will produce the same help output.
Final Words
Without a doubt, the command prompt is really powerful. This article barely scratched the surface. Go ahead and explore this versatile tool and you will find that it always has new tricks for you to discover.
If you have any questions, just reach out to me via the comments section below.
Leave a Reply