Windows

Five Windows command prompt tips every IT pro should know

Most Windows users may prefer the GUI, but IT pros recognize the power of the command prompt. Bill Detwiler shows you five tricks for the IT ninja.

The Windows GUI may be easy for the average user to navigate, but power users and IT pros alike still recognize the usefulness of the command prompt. Bill Detwiler shows you five tricks that will help you become a command-prompt ninja.

In the video, I mention a tip from TechRepublic member Kiwi.Dusty and mytmous about using the Function keys from the command prompt. Here's the complete list Function Key actions as posted in the original article's discussion post:

F1 retypes the previous command one character at a time

F2 brings up a dialog and asks "Enter the char to copy up to:"

F3 retypes the last command in full

F4 brings up a dialog and asks "Enter char to delete up to:"

F5 as for F3

F6 Print EOF character (Ctrl+Z)

F7 brings up a dialog of all the recent command history

F8 brings up each of the most recent commands, one at a time

F9 brings up a dialog and asks "Enter command number:"

I also outline a tip from members Davids, jnickell and philrunninger. These individuals wrote a couple of batch files that let you log your command-line sessions and then query that log for specific information.

Here's the text for each file:

Filename: q.bat

@echo off

echo ---------------- %date% %time% ---------------- >> "%userprofile%\cmdHistory.txt"

doskey /history >> "%userprofile%\cmdHistory.txt"

exit

Filename: h.bat

@echo off

if %1. == . (

type "%userprofile%\cmdHistory.txt" 2> nul

echo ---------------- %date% %time% ----------------

doskey /history

) else (

if /i %1. == /edit. (

start "Revising History..." "%userprofile%\cmdHistory.txt"

) else (

echo ------------------------------------------- Previous Windows

type "%userprofile%\cmdHistory.txt" 2> nul | findstr /i "%*"

echo ------------------------------------------------ This Window

doskey /history | findstr /i "%*"

)

)

Here's a big "thank you" to all the members who shared these great command line tips. I'll be sending each of them a TechRepublic coffee mug.

For those of you who prefer text to video, you can click the Transcript link that appears below the video player window or you can also read Greg Shultz's article, "Five Windows command-prompt tips every IT pro should know," on which the first command line tip is based.

You can also sign up to receive the latest TR Dojo lessons through one or more of the following methods:

About

Bill Detwiler is Managing Editor of TechRepublic and Tech Pro Research and the host of Cracking Open, CNET and TechRepublic's popular online show. Prior to joining TechRepublic in 2000, Bill was an IT manager, database administrator, and desktop supp...

152 comments
accessecology123
accessecology123

Windows 7 is the very famous Operating System in India.Because it is user friendly OS.I think it is advanced OS in windows.

ciscogig
ciscogig

Great session - good for those of us whose primary interface is on other platforms - thanks!

The Truth
The Truth

Good God, Bill, I hope you don't get paid a lot for passing along these really basic tips. These are for only the newest of noobs.

thyuoo
thyuoo

it will make me play with cmd easier more than before thank you trdojo :):):)

harry_
harry_

I like the way Bill is speaking, its intelligible also for international users.

i.hashem
i.hashem

thnx for sweet info's i just love the command prompt i'm wondering if there's more scripts where we can learn more tricks about batch files

paul.gurusinghe
paul.gurusinghe

very neat! i used 3 of them really often, but the other 2 were as useful!

canddsol
canddsol

Thank you for this. I picked up a couple if tricks I didn't have before. I sent this video to the rest of my Divisional team.

saint
saint

I am not sure if anyone mention that you can also get directory and fiflename completion by using the ctrl-f and ctrl-d keys by starting cmd with the parameter /f:on. This is the way i prefer to start the cmd prompt especially for names that have special characters .

matthew.savige
matthew.savige

My favorite are the commands: tasklist and taskkill. I had a machine sitting at a remote site with Malware sitting on it which I couldnt get to at the console. Using systernals psexec I opened a remote cmd session on the machine then was able to find the process that was locking the nasty dll (tasklist /m nasty.dll) and kill it thus being able to kill the process and delete the file. (Taskkill -PID 1234). Best thing is with the exception of psexec. All these commands are built into XP pro. A work around if you don't have psexec is to remotly turn on the telnet service using computer management and telneting to the machine. I like to use the psexec method tho as I forget to turn the telnet service off again.

stephen.edie
stephen.edie

I've also created .bat files for drag/drop to an FTP server.

burbachj
burbachj

WOW your a genius cut and paste...

omagic
omagic

Excellent tips on using command prompt. I have never heard of this ones.

roger3mi
roger3mi

These where very helpful. Another thing that I have done is to copy notepad.exe to n.exe. This way I hit the Windows key + R key, type n and notepad opens right up.

hiteshthappa
hiteshthappa

great info! I only used directional keys to scroll through previous commands.

joe.mariadassou
joe.mariadassou

There is no need to use the mouse to bring up the context menu. Alt+Space does the trick

colonel_angel2004
colonel_angel2004

You can also use the left/right arrow keys also and edit the line if you have a spelling mistake.

adamzski
adamzski

I love linux, if your going to be typing things into black boxes linux really looks after you. doskey anyone?

maurice.c.boley
maurice.c.boley

Great tips. Although you windows guys kill me. You chide us mainframers about our 'green screen' but what do I see you using 'black screens' What's the diff. :-)

reisen55
reisen55

And even basic commands such as robocopy and xcopy run far faster in the command environment. And it also shows the older crowd that we still REMEMBER what DOS was.

ITGirl.2010
ITGirl.2010

Thanks for the extra tips. I've already used them and find them useful in speeding up my work.

emmet.jones
emmet.jones

A few helpful commands Prompt=$p$g ~ to show you where you are Title Job Name ~ so the title will show up in the window Log the progress of a job echo Job starts >C:\data\logs\Job.log echo Job step continues >>C:\data\logs\Job.log add a time stamp to the job log. who >>C:\data\logs\Job.log ~ find some unix like utils

kylehedspeth
kylehedspeth

Excellent...love the tips. Very useful!!

mukababi
mukababi

Ok, here's one I like from my old UNIX days that made it to the Dos prompt some years ago. To migrate thru directories from the command line you can can use; the old unix "cd ../../..". Each ".." moves you up one dir. Then if you know the name of the directory lower than the one you've moved up to, you can change directions and drill back down. Example: C:\MS Tools\SysinternalsSuite>cd ../../win* C:\Windows> So from the "SysinternalSuite" dir I jumped up two dirs (cd ../..) to the root and then back down into the Windows dir. (and used the wildcard tip from the cmd tips to boot) Yup, GUI's are nice but workin the cmd line is where it's at if you want to "amaze your friends". cheers all.

bjulias
bjulias

Learned a couple of new commands - Thanks!

pdm_pdq
pdm_pdq

Can someone tell me a good link to download DOSKEY?

C:\ prompt rather than the c:\%userdirectory%

hground
hground

And by the way - even though I am far from being a "noob" having been a software developer for over 23 yrs (Vax/UNIX/NT/XP/VISTA/W7) who has had to work plenty with command lines (really miss ksh) - there were still a few things that I had not seen before in Bill's article. (Probably because most of my work is done with C/C++.)

bitdoctor
bitdoctor

Actually, you can save a keystroke - if Notepad is pinned to your Start menu - which mine is - and I don't remember pinning it there - and if it is the only one starting with "N" (or first one) You simply hit Windows- THEN 'n' and it opens up - no need for hitting "r" nor for renaming notepad.exe.

afbell8
afbell8

I use batch files to set up DOS environment. I keep a batch file at the root directory of my PCs that sets up my command environment the way I want it. The first thing I do after starting the cmd session is to run the batch file. In the batch file, I make sure to change my path to be able to run other batch files, important utilities, etc., from the cmd line. example:setdos.bat @echo off path=%path%;"c:\program files\windows nt\accessories";c:\;"c:\Batch";"c:\util";"c:\UnixUtil"; SET dircmd=/o/p SET prompt=$p$g doskey @echo. @echo Path is set, DOSKEY is on @echo. @echo. @echo HOME directory set for GNU Bash I also do this with batch files for apps that may require environment var changes. (And, I know I can change this permanently elsewhere.) example:StartJava.bat @echo off REM Add Java directory to path path=.;%path%;c:\j2sdk1.4.2_10\bin; REM Add classpath environment variables REM (old CP) set CLASSPATH=.;c:\;%classpath%;c:\j2sdk1.4.2_10\lib; set CLASSPATH=.;%classpath%; REM Change to Java work directory, display env info,show commands c: cd "\j2sdk1.4.2_10\bin\Javafiles\Study" cls @echo. @echo. ** CLASSPATH and PATH environment set ** @echo. @echo. @echo To compile: javac ProgramName.java @echo. @echo. @echo To run: java ProgramName @echo.

teebes2004
teebes2004

prompt=$m$p$g to see the full UNC of a mapped drive.

pandppc
pandppc

Thank you for refreshing my memory of DOS commands and teaching me some new ones. I especially like the stuff about drop and drag - I didn't know that. And.. for those who think DOS is old news, think again. Not only do you need to use it within Windows, I often must use it to flash the BIOS and most commands also work in NTFS DOS which is necessary to repair installations that will not boot or otherwise not start Windows correctly.

bitdoctor
bitdoctor

Agreed - F7 is nice, But as for doing F-whatever to recall last command, duh, just press the UP-ARROW for last command. And keep pressing up-arrow and you will scroll through all previous commands.

afbell8
afbell8

In the properties for the command prompt, change "start in" to C:\

bphillip
bphillip

In the properties box for the command prompt (programs | accessories | command prompt) go to the START IN field and enter your desired location. C:\ or %homedrive% or any other location you desire.

pjboyles
pjboyles

Craete a shortcut that has this or type into run line: cmd /k C:&CD\ Setting it to always go to "C:" can cause other things to fail.

herbm_techrepublic
herbm_techrepublic

You can add a shortcut key to any shortcut on your Start menu -- just edit the properties. Ctrl-Alt is the automatic prefix so using 'N' gets Ctrl-Alt-N to open a Notepad if none is open, and to FOCUS it if it is already open but not in front. BTW: Using Notepad++ for your "notepad" is another expert trick (or your favorite SMALL super editor.)

brian.creno
brian.creno

I went down this road for awhile using batch files to compile my code, but because I am compiling on more than just a windows machine I needed something with great flexibilty that I could call from just about anywhere. Most of scripts now have been created in ANT. I still use a BAT file to set up an automated task, since I have not found a way to make ANT do that. Have you used ANT?

pdm_pdq
pdm_pdq

I have searched the whole computer, its not there. Thanks

bitdoctor
bitdoctor

Oops! My apologies - I finally was able to read the full transcript - something kept aborting on me - I'll never admit what it was ;-) So, I saw that you did, indeed, mention the various 'scoll up' options - but yes, I still love the F7 tip! Put that in the "things we [should] have known" category.

bitdoctor
bitdoctor

Sure, but Yuck, because ctrl-alt-ANYTHING is painful. Windows-N only requires 2 hands, whereas ctrl-alt-N requires 3 hands (I'm being a bit tongue-in-cheek there).

fmcgowan
fmcgowan

is that it (apparently) automatically loads command.com to run true DOS executable programs on the fly. The majority of my work is still FoxPro 2.6 (DOS) and I always use *.CMD scripts to start FoxPro rather than *.BAT scripts. That lets me use the IF...ELSE and other logic that is not supported under Command.com. I do the complex scripting via Rexx (specifically Regina) rather than learning PowerShell because the features, syntax and return values of Rexx commands are fixed and will remain so. Because MS periodically changes the Windows command line utilities in some subtle manner that breaks my scripts or - worse yet - changes their behavior without warning, I view them as undesirable for long term use. I am slowly switching from using embedded Win32 commands do the things Rexx cannot to the GNUWin32 Unix commands as I become familiar with them as they are also feature complete and syntax stable. They also *consistently* return 0 for success and something else for failure where the Win32 command utilities do not.

sjnelson
sjnelson

I believe cmd runs under cmd.exe and bat runs unde3r command.com.

REMAC
REMAC

I learned somewhere that it may be faster to run batch files under CMD by naming them .CMD rather than .BAT because in some versions of Windows command processors .CMD runs in native 32 bit mode where the .BAT needs to be supported by 16 bit emulation. The original source escapes me but it may be something I read from Woody Leonhard.

afbell8
afbell8

No, sorry. I had heard of it, but knew almost nothing about it until I looked it up just now. It does look like something I may want to check out in the future, though.

pdm_pdq
pdm_pdq

Thank You! Thank You! Thank You! ;-)

herbm_techrepublic
herbm_techrepublic

DosKey.exe is in the System32 directory of the current and most previous versions of Windows. It may not be necessary for some things (command recall and edit), but for saving/restoring History and for adding Macros you must run the command explicitly.

Datacommguy
Datacommguy

The old DOSKEY program is long gone but the CMD command includes the functionality. Open a command window, type in some command, and then press up-arrow.