Thursday, 28 July 2016

Command prompt tricks and hacks

Command prompt tricks and hacks



The Windows Command Prompt tool, and many of its commands, might seem boring or even relatively useless at first glance, but as anyone who has ever used the Command Prompt very often can tell you, there's much to love!

I guarantee that these several Command Prompt tricks and other Command Prompt hacks will get you excited about many of the mundane sounding Command Prompt commands like telnet, tree, or robocopy and so many of them. Some of these Command Prompt tricks and hacks are special features or fun uses for the Command Prompt itself, while others are just neat or relatively unknown things you can do with certain CMD commands. Whatever you do, don't miss the crazy trick where you can watch the entire Star Wars Episode IV movie, for free, from right inside the Command Prompt.


  • Use Ctrl-C to Abort a Command


Just about any command can be stopped in its tracks with the abort command: Ctrl-C. If you haven't actually executed a command, you can just backspace and erase what you've typed, but if you've already executed it then you can do a Ctrl-C to stop it. Ctrl-C isn't a magic wand and it can't undo things that aren't undoable, like a partially complete format command. However, for things like the dir command that seem to go on forever or questions you're asked at the prompt that you don't know the answer to, the abort command is an excellent Command Prompt trick to know.
  • View a Command's Results One Page (or Line) at a Time


Ever run a command, like the dir command, that produces so much information on the screen that it's almost useless? You're not alone. One way around this is to execute the command in a special way so whatever information is generated is shown to you one page, or one line, at a time. To do this, just type the command, the dir command for example, and then follow it with the pipe redirection operator and then the more command. For example, executing dir /s | more will generate the thousands of lines of results that you expect from the dir command, but the more command will pause each page of results with -- More -- at the bottom of the page, indicating that the command is not done running. Just press the space bar to advance by page or press the Enter key to advance one line at a time. 
  • Run Command Prompt as an Administrator Automatically

Many commands require that you execute them from an elevated Command Prompt in Windows - in other words, execute them from a Command Prompt that's run as an administrator. You can always right-click on any Command Prompt shortcut and choose Run as administrator but creating a shortcut to do the same thing can be a huge time saver if you're a frequent Command Prompt power user. To complete this Command Prompt trick, just create a Command Prompt shortcut on the desktop, enter the shortcut's properties and then select the Run as administrator box located in the Advanced button on the Shortcut tab. 
  •  Become a Command Prompt Power User with Function Keys
F1: Pastes the last executed command (character by character)
F2: Pastes the last executed command (up to the entered character)
F3: Pastes the last executed command
F4: Deletes current prompt text up to the entered character
F5: Pastes recently executed commands (does not cycle)
F6: Pastes ^Z to the prompt
F7: Displays a select able list of previously executed commands
F8: Pastes recently executed commands (cycles)
F9: Asks for the number of the command from the F7 list to paste

Another Command Prompt trick coming up soon is full of arrow key shortcuts, a few of which are similar to these function key tricks.
  • View a Drive's Entire Directory Structure 
I think one of the neatest little commands is the tree command. With tree, you can create a kind of map of the directories on any of your computer's drives. Execute tree from any directory to see the folder structure under that directory. With so much information created with this command, it's probably a good idea to export the results of tree to a file so you can actually look through it. For example, tree /a > c:\treeresults.txt, just as explained in the last Command Prompt trick about redirection operators.
  • Use Robocopy as a Backup Solution 
 Thanks to the robocopy command, you don't need to use Window's backup software or install a third party program to manage your backups. Just execute robocopy c:\users\tim\documents f:\backup\documents /copyall /e /r:0 /dcopy:t /mir, obviously replacing the source and destination folders with whatever you'd like to backup and where. The robocopy command with these options functions identically to an incremental backup software tool, keeping both locations in sync. You don't have the robocopy command if you're using Windows XP or earlier. However, you do have the xcopy command, which can be used to do something very similar: xcopy c:\users\tim\documents f:\backup\documents /c /d /e /h /i /k /q /r /s /x /y. No matter which command you choose to use, just create a script file containing the command and schedule it to run in Task Scheduler and you'll have your own custom made backup solution.
  • View Your Computer's Important Network Information
Maybe just for your own information, but certainly when you're troubleshooting a network or Internet problem, you'll probably at some point need to know details about your computer's network connection. Everything you'd want to know about your network connection is available somewhere in the Control Panel in Windows, but it's much easier to find, and much better organized, in the results from the ipconfig command. Open Command Prompt and execute ipconfig /all. What displays on screen next is everything important about your network connection: your IP address, hostname, DHCP server, DNS information, and much, much more. Combine this hack with the one about redirection operators that you learned about several slides back and you've got a very easy way to get information about your connection to someone helping you with a problem.
  • Find a Website's IP Address


Like to know the IP address of a website? You can use the nslookup command or the ping command but the former is probably faster. First let's use the nslookup command to find the IP address of about.com: Just execute nslookup about.com and view the result. Make sure you don't confuse any private IP addresses that also show up in the nslookup results alongside the public IP address of about.com, which is what IP address we're after. Now let's try using the ping command to find it: Execute ping about.com and then look at the IP address between the brackets in the first line shown. Don't worry if the ping command "times out" during execution; all we needed here was the IP address. Using either Command Prompt trick, the IP address of about.com is shown as 207.241.148.80. You can use the same procedure with any website or any hostname on your local network. 
  • Watch Star Wars Episode IV
Yes, you read that correctly, you can watch an ASCII version of the full Star Wars Episode IV movie right in the Command Prompt window! Just open Command Prompt and execute telnet towel.blinkenlights.nl. The movie will start immediately. Check out the tip below if this doesn't work. True, this isn't a terribly productive use of the Command Prompt, nor is it really a trick of the Command Prompt or any command, but it sure is fun! We can't imagine the work that went in to this!

Tip: The telnet command isn't normally enabled in Windows but can be turned on by enabling Telnet Client from Windows Features in the Programs and Features applet in Control Panel. If you'd rather not enable Telnet but would like to see the movie, you can also watch it in your browser at Star Wars ASCIIMation.

No comments:

Post a Comment