up vote 15 down vote favorite
10
share [g+] share [fb]

What are the lesser-known but useful features of the Windows command prompt?

link|improve this question
1  
What's the Programming tag for? You don't need to be a programmer to use the command line. – Phoshi Jun 10 '10 at 14:41
feedback

migrated from stackoverflow.com Nov 2 '09 at 19:57

This question came from our site for professional and enthusiast programmers.

closed as not a real question by random Aug 25 '10 at 4:33

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. See the FAQ.

17 Answers

If you mean the command prompt, I think the best (not widely known) feature is autocomplete when you press Tab, I find this saves so much time.

Another nice feature is pressing F7 to quickly see a list of the last commands typed, however, I still prefer pressing or .

Lastly, since there is no longer a beep command, for when I make scripts, I like including the Alt+Numerical Keyboard 7 symbol which causes a beep. You can test this by doing

echo •

(where • is Alt+Numerical Keyboard 7)

Also, prior to Windows Vista, They kept the MSDOS editor in there! I love this and you could load it by typing "edit" at the command prompt.

Lastly, the prompt command! I can't count the amount of pranks that I used to pull with this!... type the following:

prompt anything$g

will change the prompt to:

anything>

You can get up to a lot of fun here! To find out all symbols, type:

prompt /?
link|improve this answer
2  
The beep character is the ASCII character BEL which is the seventh character, or 0x07, which can also be typed using CTRL+G on my Windows XP machine's command prompt. – eleven81 Nov 2 '09 at 20:10
+1, I knew about Ctrl+G was an alternative, but I had no idea about 0x07/BEL, good general knowledge! Thanks! – William Hilsum Nov 2 '09 at 20:13
ALT+NUMPAD can be used to enter any ASCII value as a decimal value while the ALT key is held down. For example 'ALT+100' (where 100 is 1,0,0 typed on the numpad while ALT is held down) will type the letter 'd' which is ASCII 100 (in decimal). In other words, you can use ALT+32 as a very inefficient way to type a space character :-) – Adisak Feb 26 '10 at 21:57
Actually, edit.com is included in Windows Vista/7, just not the 64-bit editions, because of the 64/16-bit incompatibility. – Hello71 Aug 13 '10 at 14:57
i really like the last part.time to try it out on my team mates – Ravisha Sep 13 '11 at 4:33
feedback

CD /D which automatically switches drive as well as directory, e.g.:

c:> cd /d d:\temp

No more double commands such as:

C:> d:
D:> cd temp

To save even more typing you can add an alias (thanks @Phoshi)

C:> doskey cd=cd/d $*

Then you can just type:

c:> cd d:\temp
link|improve this answer
As much as it is a cool shortcut, I would argue that it takes a lot longer than just typing d:, then cd t - followed by a tab, or even typing temp. – William Hilsum Nov 2 '09 at 20:39
What I most often do, is type cd d:\temp which sort of succeeds, but leaves you at c: which sucks. Getting used to adding the /d will make your CD's work regardless on which drive you happend to be in currently... – Ulf Lindback Nov 2 '09 at 21:04
1  
You could use doskey to alias cd to cd \d $* easily enough. – Phoshi Nov 2 '09 at 21:12
Yeah, an alias is real nice, I'll add that to the answer as well – Ulf Lindback Nov 3 '09 at 7:51
feedback

For me the CLIP command. Works both with piped or redirected

dir | clip
clip < myfile.txt

Copies the output of the dir command to the clipboard.
Copies the contents of myfile.txt to the clipboard

Try it :)


Another nice use (see Poshi comment below):

echo [text] | clip
link|improve this answer
Windows XP's command prompt says: 'clip' is not recognized as an internal or external command, operable program or batch file. – eleven81 Nov 2 '09 at 20:09
Introduced in vista. You can get a copy to your windows XP – A Dwarf Nov 2 '09 at 20:11
2  
Don't forget echo [text] | clip, very useful! – Phoshi Nov 2 '09 at 21:23
Indeed, Poshi! +1. Certainly warrants an edit. Thanks – A Dwarf Nov 2 '09 at 23:00
awww... +1 useful, but where can i find a copy for XP? and... does it work with cygwin? that would make me so happy :) – quack quixote Nov 3 '09 at 0:54
show 1 more comment
feedback

Piping, just in general.

command1 | command2 -option | command3 > textfile.txt

would take the output of command 1 as the input of command 2, and the output of command 2 into the input of command 3, then route the output to a text file. Comes in incredibly useful with tools like sed or grep (Just install cygwin already, add the \bin folder to your path and you have (imo) the best *nix tool emulations out there.)

Also, using & and && to double-up commands. && can be used as a sort of if statement, too:

 command1 && command2

will only execute command2 if command1 succeeds.

Additionally, you can change the PATHEXT variable to include your favourite scripting language (like .py for python) and write quick CLI apps that you can call like any old executable (like I have sort.py, which intelligently chooses the best sorting algorithm for the given dataset (to a point, of course), and sorts them, and I can call it like sort 12 21 7, or command1 | sort)

Also, and most importantly, HKEY_CURRENT_USER\Software\Microsoft\Command Processor\Autorun lets you specify a command to run when the command window opens. This can be a batch files. Hello .bashrc-style extensions! You can set a custom prompt very easily there, see prompt /? for more details.

Want an alias? You'll need doskey, which is built in.

doskey pu=pushd $*

is one I like, to make push and popding easier. The $* expands much like %* in a batch file, allowing true argument support in aliases. Still want to tell me cmd has nothing to offer? It's still no bash, but it's a very usable environment that simply suffers from a slight deficit of inbuilt tools.

Some of the inbuilts that ARE there, though, can be used in ways you'd not think of. Got a notes file? How are you planning to append to it, notepad? Silly you.

echo >> notes.txt

What does that do, you ask? Why, it allows you to type text, line breaks and all, and then append to a file with control+c. Not bad.

link|improve this answer
Your echo >> notes.txt does not work, are you not thinking of copy con? – Robert MacLean Dec 1 '10 at 8:07
feedback

There's a wealth of information about the XP/2000/NT command prompt at SS64.com. This site is one of my favourite resources.

link|improve this answer
feedback

%CD% always holds the current working directory.

link|improve this answer
feedback

Function calls and "goto :eof" returns within batch files:

@echo off
::::::::::::
:: Main
setlocal

call :s_Foo
call :s_Bar
call :s_Foo

endlocal
goto :eof

::::::::::::
:: Foo
:s_Foo
echo foo
goto :eof

::::::::::::
:: Bar
:s_Bar
echo bar
goto :eof
link|improve this answer
feedback

Pressing F7 will bring up the command history (like history in Unix/Linux, but in a menu-like fashion). Select an item by using the up/down arrow keys. Execute the selected command by pressing Enter/Return.

This method gives a better overview than using the up/down arrow keys alone.

link|improve this answer
feedback

help
It's pretty handy...

link|improve this answer
feedback

Use the following command to get disk usage. Incl. Bytes free, total number of bytes, available bytes.

fsutil volume diskfree c:

Where C: is the letter of the drive. This could be easily calculated in to MB/GB if one wanted, and put into a batch file.

For a silent pause in batch files, i tend to use this command here

@ping 127.0.0.1 -n 3 -w 10 > nul

Where 'n' is the total number of echo requests to send And 'w' is the time to wait in milliseconds.

Resize your ShadowStorage ( storage that is used for System Restore )

 vssadmin Resize ShadowStorage /For=C: /On=C: /MaxSize=3GB

For more info; use the /? switch :)

link|improve this answer
feedback
netsh winsock reset

Prior to Windows XP, TCP/IP could be uninstalled and reinstalled easily. This would correct problems where the implementation of the protocol became corrupt. In Windows XP, reinstalling TCP/IP was a pain in the rear to do manually, and so in service pack 2, they added the above functionality.

link|improve this answer
feedback

Not sure if this is "hidden" feature per se, but did you know you can use the dir command to list the contents of two directories at once?

dir c:\directory1 c:\directory2
link|improve this answer
feedback

ver - gives you the current version of the operating system

link|improve this answer
feedback

Here's a script that uses quite a few little used features in order to time another batch file.

  • Subroutines (call :LABEL / goto :EOF)
  • Substrings (i.e. "%TEMPRESULT:~0,1%")
  • Computations (set /A)
  • Varargs (%*)

It's a bit of a hack but it does work.

@echo off
@rem --------------------------------------------
setlocal ENABLEEXTENSIONS

set start_time=%time%
echo Beginning at: %start_time%
echo Running Timed Batch File
echo.

@rem echo %*  <-- Extension for all args -- easier than following line
@rem echo %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 ... ...

@rem DO YOUR WORK HERE
@rem call userscript.bat %*
PAUSE

set stop_time=%time%
echo.
echo Timed Batch File Completed
echo Start time: %start_time%
echo Stop time : %stop_time%


set TEMPRESULT=%start_time:~0,2%
call:FN_REMOVELEADINGZEROS
set start_hour=%TEMPRESULT%
@rem
set TEMPRESULT=%start_time:~3,2%
call:FN_REMOVELEADINGZEROS
set start_min=%TEMPRESULT%
@rem
set TEMPRESULT=%start_time:~6,2%
call:FN_REMOVELEADINGZEROS
set start_sec=%TEMPRESULT%
@rem
set TEMPRESULT=%start_time:~9,2%
call:FN_REMOVELEADINGZEROS
set start_hundredths=%TEMPRESULT%

set TEMPRESULT=%stop_time:~0,2%
call:FN_REMOVELEADINGZEROS
set stop_hour=%TEMPRESULT%
@rem
set TEMPRESULT=%stop_time:~3,2%
call:FN_REMOVELEADINGZEROS
set stop_min=%TEMPRESULT%
@rem
set TEMPRESULT=%stop_time:~6,2%
call:FN_REMOVELEADINGZEROS
set stop_sec=%TEMPRESULT%
@rem
set TEMPRESULT=%stop_time:~9,2%
call:FN_REMOVELEADINGZEROS
set stop_hundredths=%TEMPRESULT%

set /A start_total=(((((%start_hour%*60)+%start_min%)*60)+%start_sec%)*100)+%start_hundredths%
set /A stop_total=(((((%stop_hour%*60)+%stop_min%)*60)+%stop_sec%)*100)+%stop_hundredths%

set /A total_time=%stop_total% - %start_total%

set /A total_hundredths=%total_time% %% 100
set total_hundredths=00%total_hundredths%
set total_hundredths=%total_hundredths:~-2%
set /A total_time=%total_time% / 100

set /A total_sec="%total_time% %% 60"
set total_sec=00%total_sec%
set total_sec=%total_sec:~-2%
set /A total_time=%total_time% / 60

set /A total_min="%total_time% %% 60"
set total_min=00%total_min%
set total_min=%total_min:~-2%
set /A total_time=%total_time% / 60

set /A total_hour="%total_time% %% 60"
@rem Handle if it wrapped around over midnight
if "%total_hour:~0,1%"=="-" set /A total_hour=%total_hour% + 24

echo Total time: %total_hour%:%total_min%:%total_sec%.%total_hundredths%

@rem --------------------------------------------
@rem Exit the BAT Program
endlocal
goto :EOF

@rem --------------------------------------------
@rem FN_REMOVELEADINGZEROS function
@rem  Used to remove leading zeros from Decimal
@rem  numbers so they are not treated as Octal.
:FN_REMOVELEADINGZEROS
if "%TEMPRESULT%"=="0" goto :EOF
if "%TEMPRESULT:~0,1%" NEQ "0" goto :EOF
set TEMPRESULT=%TEMPRESULT:~1%
goto FN_REMOVELEADINGZEROS
link|improve this answer
feedback

subst

Associates a path with a drive letter. Used without parameters, subst displays the names of the virtual drives in effect.

Syntax

subst [drive1: [drive2:]Path]

subst drive1: /d

Parameters

drive1: Specifies the virtual drive to which you want to assign a path.

drive2: Specifies the physical drive that contains the specified path (if different from the current drive).

Path: Specifies the path that you want to assign to a virtual drive.

/d : Deletes a virtual drive.

/? : Displays help at the command prompt.

link|improve this answer
feedback
whoami

and

whoami /groups

to display the current user and their group memberships, respectively when trouble shooting things like permission related problems or group policy.

Double-useful if you are running several command windows as different users using runas or Run As Administrator, and you are confused which one is which.

As far as I recall, this is native from Vista / Longhorn onwards, you can probably get it in XP by copying it from another machine.

Also, the new /MT switch for Robocopy which allows you to do multithreaded copying.

link|improve this answer
feedback

Start typing a command that is in your history, then hit F8 to complete the command from your history. Repeatedly hitting F8 cycles through matching elements of your history.

eg if you've run the following commands:

C:\> directions /to Home /from work
C:\> type output.txt
C:\> dir c:\temp

If you enter "di" then hit F8 it will complete firstly with:

C:\> dir c:\temp

press F8 again to see:

C:\> directions /to Home /from work

Great for those long compilation command-lines.

link|improve this answer
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.