vote up 8 vote down star
8

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

flag

migrated from stackoverflow.com

15 Answers

vote up 14 vote down

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|flag
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 at 20:10
+1, I knew about Ctrl+G was an alternative, but I had no idea about 0x07/BEL, good general knowledge! Thanks! – Wil Nov 2 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 at 21:57
vote up 13 vote down

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|flag
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. – Wil Nov 2 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 at 21:04
You could use doskey to alias cd to cd \d $* easily enough. – Phoshi Nov 2 at 21:12
Yeah, an alias is real nice, I'll add that to the answer as well – Ulf Lindback Nov 3 at 7:51
vote up 9 vote down

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|flag
Windows XP's command prompt says: 'clip' is not recognized as an internal or external command, operable program or batch file. – eleven81 Nov 2 at 20:09
Introduced in vista. You can get a copy to your windows XP – A Dwarf Nov 2 at 20:11
2  
Don't forget echo [text] | clip, very useful! – Phoshi Nov 2 at 21:23
Indeed, Poshi! +1. Certainly warrants an edit. Thanks – A Dwarf Nov 2 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 Nov 3 at 0:54
show 1 more comment
vote up 5 vote down

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|flag
vote up 4 vote down

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|flag
vote up 3 vote down

help
It's pretty handy...

link|flag
vote up 3 vote down

%CD% always holds the current working directory.

link|flag
vote up 2 vote down

ver - gives you the current version of the operating system

link|flag
vote up 2 vote down

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|flag
vote up 1 vote down
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|flag
vote up 1 vote down

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|flag
vote up 0 vote down
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|flag
vote up 0 vote down

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|flag
vote up 0 vote down

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|flag
vote up 0 vote down

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|flag

Your Answer

Get an OpenID
or
never shown

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