Scott Hanselman

Forgotten (but Awesome) Windows Command Prompt Features

June 20, '16 Comments [61] Posted in Musings
Sponsored By

It's always the little throwaway tweets that go picked up. Not the ones that we agonize over. I was doing some work at the command line and typed "dotnet --version | clip" to copy the .NET Core version number into the clipboard. Then I tweeted a little "hey, remember this great utility?" and then the plane took off. I landed two hours later and it had over 500 RTs. Madness.

It's funny that 10 year old command prompt utility (this was added in Vista) that everyone's forgotten elicits such an enthusiastic response.

Since you all love that stuff, here's a few other "forgotten command prompt features."

Some of these have been in Windows since, well, DOS. Others were added in Windows 10. What did I miss? Sound off in the comments.

Pipe command output to the clipboard

In Vista they added clip.exe. It captures any standard input and puts in the clipboard.

That means you can

  • dir /s | clip
  • ver | clip
  • ipconfig /all | clip

You get the idea.

Piping to Clip.exe puts the standard output in your clipboard

F7 gives you a graphical (text) history

If you have already typed a few commands, you can press F7 to get an ANSI popup with a list of commands you've typed. 4DOS anyone?

More people should press F7

Transparent Command Prompt

After Windows 10, you can make the Command Prompt transparent!

It's see through

Full Screen Command Prompt

Pressing "ALT-ENTER" in the command prompt (any prompt, cmd, powershell, or bash) will make it full screen. I like to put my command prompt on another virtual desktop and then use CTRL-WIN-ARROWS to move between them.

The Windows 10 Command Prompt supports ANSI natively.

The cmd.exe (conhost in Windows 10 1511+, actually) now supports ANSI directly. Which means BBS Ansi Art, of course.

Word wrapping

Oh, and the Windows 10 command prompt supports active word wrapping and resizing. It's about time.

Little Fit and Finish Commands

  • You can change the current command prompt's title with "TITLE"
  • You can change its size with MODE CON COLS=x LINES=y
  • You can change the colors from a batch file with COLOR (hex)

What did I miss?


Sponsor: Working with DOC, XLS, PDF or other business files in your applications? Aspose.Total Product Family contains robust APIs that give you everything you need to create, manipulate and convert business files along with many other formats in your applications. Stop struggling with multiple vendors and get everything you need in one place with Aspose.Total Product Family. Start a free trial today.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Monday, 20 June 2016 22:22:21 UTC
Here's another Scott: type myfile.txt | clip

type followed by the filename prints the contents of the file. Use the pipe (|) and clip to copy to the clipboard. Works in the same way cat does in PowerShell.
Monday, 20 June 2016 22:32:43 UTC
Never knew about the clip command, very nice!

A pair that I find useful that seem to be relatively unknown are pushd and popd. Great for keeping a pin in a folder and going off to do something in another, then popping back once you're done.
Max
Monday, 20 June 2016 22:40:54 UTC
A newfound favorite of mine from XP: tree

https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/tree.mspx?mfr=true
Brandyn Thornton
Monday, 20 June 2016 22:42:22 UTC
You can also make cmd fullscreen by hitting F11 :)
Christopher Haws
Monday, 20 June 2016 22:45:02 UTC
cd | clip
dir /b /s | clip
mode con rate=32 delay=1
echo AT&F&C1&D2M1L3S11=33 > COM2
... old school
Monday, 20 June 2016 22:55:27 UTC
why Microsoft doesn't give some love to promp-command? it looks so archaic compared with bash.
cleiton
Tuesday, 21 June 2016 00:56:37 UTC
I wanted to read your web in Edge using the reading mode, but when I switched I could only see the disclaimer.
Donald Adams
Tuesday, 21 June 2016 01:03:05 UTC
There's always good old fsutil.exe


C:\temp$ fsutil
---- Commands Supported ----
8dot3name 8dot3name management
behavior Control file system behavior
dirty Manage volume dirty bit
file File specific commands
fsinfo File system information
hardlink Hardlink management
objectid Object ID management
quota Quota management
repair Self healing management
reparsepoint Reparse point management
resource Transactional Resource Manager management
sparse Sparse file control
tiering Storage tiering property management
transaction Transaction management
usn USN management
volume Volume management
wim Transparent wim hosting management

Tuesday, 21 June 2016 01:11:01 UTC
Also, when on remote servers I use the following things a lot:

remote desktop command line (use /console if all slots full)
c:\> mstsc /v:SERVERNAME [/console]

list other RDP sessions on the server
c:\> query session

kick someone off -- useful when paired with sysinternals psexec, or powershell remoting
c:\> logoff SESSIONID


Tuesday, 21 June 2016 01:13:59 UTC
Transparency disabled on my latest Microsoft Windows [Version 10.0.14342]
http://myscreenshot.info/i/0006/675681466471541254203136354016.png
Denis Ibragimov
Tuesday, 21 June 2016 01:14:11 UTC
Never knew aboput clip, and while investigating a problem yesterday I found another command I knew nothing about, the "where" command, it displays the location of a file.

e.g.
where dotnet.exe
C:\Program Files\dotnet\dotnet.exe

Fergus Rourke
Tuesday, 21 June 2016 03:04:58 UTC
Denis,
Disable legacy mode on options tab.
Michael
Tuesday, 21 June 2016 03:34:09 UTC
Create a text file

copy con lazydog.txt
The quick brown fox jumps over the lazy dog
Ctrl-z

Hiren B
Tuesday, 21 June 2016 06:50:35 UTC
cd \win*

type *.log[tab][tab][tab][tab]

cd syste[tab][tab]
Chris
Tuesday, 21 June 2016 07:18:30 UTC
Command prompt also supports drag and drop: If you want to copy the exact path to a folder or file to run the file or change the present working directory, you can simply drag and drop the file or the folder on the command prompt.

And there are more command history features available via function keys:

  • F1: Pastes per character last used command
  • F2: Pastes last used command only to a specified command character
  • F3: Pastes Last used command
  • F4: Delete command only to a specified command character
  • F5: Pastes last used command without cycling
  • F6: Pastes ^Z
  • F7: Provides a list of already used commands (selectable)
  • F8: Pastes cycleable used commands
  • F9: Will let you paste command from the list of recently used commands
Tuesday, 21 June 2016 08:02:13 UTC
I didn't know about clip, but then I've been using the UnxUtils collection for years and that has gclip, which does the same thing. I would probably have discovered clip by now if it wasn't for that.

Someone mentioned pushd and popd; there's a good use case for these that people may not be aware of. While you can navigate to a UNC path in File Explorer you can't cd to one on the command line, but you can use pushd to achieve the same thing. It will actually map a drive to the share with the next available drive letter, before changing to the directory. When the previous directory is popped off the stack with popd, the mapped drive is released.

I find Rob van der Woude's Scripting Pages to be a great resource for researching command line and batch language stuff. Look under Scripting Languages | Batch Files in the sidebar. There is info on a number of other scripting languages there too.
Tuesday, 21 June 2016 09:00:04 UTC
Ctrl-V to paste from clipboard (how long we've waited for!).
Rob K
Tuesday, 21 June 2016 09:11:05 UTC
copy con autoexec.bat
prompt $p$g
win

^Z
Rosdi
Tuesday, 21 June 2016 09:13:03 UTC
Redirecting output to txtfile always handy
Like:

dir /b | find ".exe" > findlist.txt | notepad findlist.txt

https://technet.microsoft.com/en-us/library/bb490982.aspx
Niels L
Tuesday, 21 June 2016 09:28:00 UTC
This stuff is gold :)
Rob Allen
Tuesday, 21 June 2016 09:33:00 UTC
SET /A for integer arithmetic, including bitwise operators.

The FOR command, for, well, just about anything. FORFILES.EXE, for one special case thereof.

DOSKEY.EXE for command prompt macros, and also macros for any command-line application.

GETMAC.EXE for getting adapter MAC addresses on the command line.

The CMD batch language itself.

And many, many more...



Raj Chaudhuri
Tuesday, 21 June 2016 10:15:56 UTC
How about the fact that its tab completion is actually more powerful than it at first appears? Do this in a folder which has some .txt files

notepad *.txt <tab>

John Ludlow
Tuesday, 21 June 2016 11:55:12 UTC
My standard header for .bat files:

@echo off
setlocal
if "%echo%" neq "" echo %echo%
pushd %~dp0


What it does:

  • turn off output of commands.
  • make changes to the environment local to the script
  • check if the env-var echo contains something. by doing "set echo=on" before calling the script I can switch on output of commands for debugging.
  • Set the current directory to the scripts directory. this is optional, but most of my scripts assume relative path (My old saying: The absolute path is the singleton of config management)


Catching errors:

dir I-do-not-exist || goto :error

goto :eof

:error
echo ERROR in %~dpnx0 %*
exit /b 1


Explanation:

  • "||" executes the follwing command if the first fails
  • skip the ":error" block
  • echo the file in which the error occured (full path) and all the parameters
  • return some error code
Ralf Kretzschmar
Tuesday, 21 June 2016 11:57:31 UTC
btw: Most of that I got from a book about Windows batch file skripting. I still have it somewhere. I'll post the title if I find it. But I assume it is out of print.
Ralf Kretzschmar
Tuesday, 21 June 2016 11:58:48 UTC
Something I always have to do on new machines is create a new environment variable DIRCMD and set to /ogn

setx DIRCMD /ogn

Then output from 'dir' is always sorted correctly with directories grouped at the top, can't believe it's not the default.
Pete Dishman
Tuesday, 21 June 2016 12:35:13 UTC
I often use findstr to locate strings in files. A 'memory helper' for parameters is 'sin' to search for sub directories, case insensitive, display line number. Example to find foobar in txt files:

findstr /s /i /n foobar *.txt
dan moyer
Tuesday, 21 June 2016 13:47:29 UTC
doskey.exe can clear, set the size of, and list the command history of an executable that's attached to the current console. The implementation uses the following undocumented console APIs:
doskey /reinstall /exename=[ExeName]
VOID ExpungeConsoleCommandHistory(LPTSTR ExeName)
doskey /listsize=[Number] /exename=[ExeName]
BOOL SetConsoleNumberOfCommands(DWORD Number,
LPTSTR ExeName)
doskey /history /exename=[ExeName]
DWORD GetConsoleCommandHistoryLength(LPTSTR ExeName)
DWORD GetConsoleCommandHistory(LPTSTR CommandBuffer,
DWORD CommandBufferLength,
LPTSTR ExeName)
I can't be the only person frustrated by how opening a new console doesn't load the command history from previous (not current) instances of console applications. The console needs a SetConsoleCommandHistory function. Please ask the team that owns the console subsystem (conhost.exe, conhostv2.dll, condrv.sys) and doskey.exe to implement something like this -- and to document these functions as public APIs. A doskey "/historyfile" option could support [ExeName] sections, like the current "/macrofile" option.

cmd.exe supports an AutoRun registry value, which would allow running doskey.exe to load command history, but it doesn't allow automatically saving history. I'd prefer that cmd.exe handled this automatically (but only for its own history) by appending to a per-user history file stored in "%APPDATA%\Microsoft\Command Processor\history.txt", which reflects the location of its registry key, "HKCU\SOFTWARE\Microsoft\Command Processor".
eryk sun
Tuesday, 21 June 2016 14:00:02 UTC
I really work with cmd most of the time this helped me nice post
Tuesday, 21 June 2016 14:00:42 UTC
really like to work in comment prompt like a coder.
Tuesday, 21 June 2016 14:00:44 UTC
really like to work in comment prompt like a coder.
Tuesday, 21 June 2016 14:46:41 UTC
ALT + ENTER isn't working for me =(
Mike Cheel
Tuesday, 21 June 2016 15:26:32 UTC
Sorry, one more favorite of mine


start .

Open a File Explorer for the current directory. Because sometimes dir doesn't cut it.
Ralf Kretzschmar
Tuesday, 21 June 2016 15:36:44 UTC
I say this half jokingly, but I'll never understand this trend of moving back from GUI's to command prompts (Package Manager, PowerShell, etc.). It's like we're going backwards. The whole point of GUIs was so we didn't have to remember all these obscure/archaic commands, parameters, and symbols. Intuitive menus with sub-menus, context-sensitive menus, icons, drop-downs, etc. -- these all made life simpler for power users, developers, and IT folks. Now it seems the trend is leading us right back to the days of nothing but command prompts where we have to memorize and type everything; every command, every option/parameter, every little obscure trick.

Honestly, I don't mind the typing -- as a developer it comes with the territory. It's the memorization that sucks. We've got enough things to remember already. And yeah, I'm getting old -- so get off the lawn. :)
Tuesday, 21 June 2016 15:45:21 UTC
dir /s | more
for pausing output per page
Peter
Tuesday, 21 June 2016 17:06:20 UTC
worth to mention that "|clip" and F7 works in Powershell also
Denis Mitropolskiy
Tuesday, 21 June 2016 19:47:30 UTC
So any “cool” features of such a dump shell. Over twenty years and still Microsoft didn't bother to copy good shell from Unix. Instead, they boast that CTRL+V finally works…
Konewka
Tuesday, 21 June 2016 20:45:42 UTC
You can find out a lot of neat options by running an executable from the command prompt with /? at the end of it. The command "help" also lists several common commands that are available.

@Pete Dishman: Think about it: that forces dir to list all the files first so it can sort them. If there are 100,000 files in a single folder that's on a network, that will not perform well. Make it 1995 again and you can drop a couple 0s off that number, make it directly attached, and you'll still have "it will not perform well" issues, thus why it's not the default.

@Oisin Grehan: mstsc /console is deprecated (the concept of logging into the "Console Session" (session 0) went away in Windows Server 2008), use mstsc /admin instead. Use mstsc /? for a list of commands.
Tuesday, 21 June 2016 23:33:24 UTC
dir /a,


Hidden dir parameter - shows all files (hidden, system, etc) without have to specify the full list of things to unhide. (note that is a comma after the "/a")


Scott Wilson
Wednesday, 22 June 2016 01:50:47 UTC
"mklink" is a good use of "cmd". I prefer symbolic links to shortcuts. I haven't seen a way to make symbolic links in the file manager.
Pete Smith
Wednesday, 22 June 2016 02:41:57 UTC
Am I the only a little bit nostalgic for edlin, or debug (it was a great hex editor for small files!)
C Johnson
Wednesday, 22 June 2016 06:24:14 UTC
A great companion to clip is pclip.exe in UnxUtils.zip: UnxUtils @ Sourceforge

While clip copies TO the clipboard, pclip prints the clipboard to stdout where it can be piped to other filters.

Have some text in notepad that needs sorting? Ctrl+C copy it to the clipboard, then:

pclip | sort | clip
... and it is now in the clipboard sorted.

Used with Windows ports of gawk, uniq, etc. you can all manner of text filtering.
Mark Cranness
Wednesday, 22 June 2016 06:25:31 UTC
Awesome Scott and thanks a lot for sharing these :)
Wednesday, 22 June 2016 10:52:58 UTC
shutdown /s

is my favorite. It opens a hidden game in Windows. You will love it.
Paul
Wednesday, 22 June 2016 13:57:47 UTC
NICE POST AND THANKS FOR THE INFORMATION...
Wednesday, 22 June 2016 13:58:10 UTC
NICE POST THANKS FOR THE INFORMATION...
Wednesday, 22 June 2016 20:57:36 UTC
Thanks for sharing this.

All those times I manually copied "ipconfig /all" to notepad...
Kevin Adams
Wednesday, 22 June 2016 21:23:52 UTC
What did you miss? Well, it doesn't directly concern the stock Windows console, but I recently discovered it is now possible to run ConEmu inside a Visual Studio tool window.

Some details here:

http://philipdaniels.com/blog/2016/06/running-conemu-inside-visual-studio/

Basically, this is freaking awesome, IMNSHO. I am using it to get access to Cygwin directly inside Visual Studio and am loving it. GIT CLI FTW!

The project has only 600 downloads at present on Visual Studio Gallery and deserves to be better known.
Wednesday, 22 June 2016 23:37:53 UTC
Great post!
Byron
Thursday, 23 June 2016 13:41:10 UTC
shutdown /s is my favorite. It opens a hidden game in Windows. You will love it.

Indeed, love this one.
Hard mode: shutdown /s /t 0
Play with coworkers: shutdown /s /m \\another-pc
Thursday, 23 June 2016 15:54:05 UTC
Try this on command line on NTFS drive:
echo hidden > test.txt:hidden
echo hidden2 >> test.txt:hidden

do a dir test.txt , what's the file size?
do a type test.txt or use notepad to edit , it's empty
do a more < test.txt:hidden

you see what you have echo and pipe and append into the test.txt section hidden ;-)


Hun Boon Teo
Saturday, 25 June 2016 19:12:06 UTC
type: hostname
in the command prompt to get your computer name.
Great post.
Monday, 27 June 2016 18:19:51 UTC
type whoami in a command prompt if you forgot who you started the shell as.
Wednesday, 29 June 2016 02:08:41 UTC
Don't forget pushd and popd.

'Pushd' changes directory to the parameter and remembers the current directory on a stack. It's a little better than 'cd' because you can also change folders and drive letters in one command.

'Popd' pops the directory off the stack and changes to it -- including changing drive letters.

So with pushd and popd you can really drill in and then pop back. I use a DOSKEY macrofile that makes 'cd' a macro for pushd and '.' a macro for popd. This turns the '.' key into a back button.
Howard Hoffman
Thursday, 30 June 2016 07:30:25 UTC
you can see all the ip address by using the command

ip config /all

or you can see are you getting the correct bytes or not by typing
ping google.com
Friday, 01 July 2016 23:15:34 UTC
'Call' for subroutines

To execute a set of custom commands based on command line arguments
1. Parse/validate command line arguments
2. Use ECHO to write .bat file to execute the sequence of commands
3. Call that batch file

This provides equivalent function call functionality to bash.

Current command set:
- The built in dos batch commands
- gnuwin32 - most of the basic unix commands
- sysinternals suite
- image processing
- optipng - compress and fix badly formatted png files
- imagej - open dicom and lots of basic image formats, image processing, opens source
- xpdf - extract text from pdf, render pdf pages to 300 or higher dpi image files, extrac images from pdf, split pdf into individual pages, concatenate pdf files into a single pdf, ...
- jpegtran - optimize jpeg files, drop partial blocks from edge of file
- imagemagick
- gnuwin32's tiff2pdf convert a multipage tiff file to a single pdf
- media
- mplayer
- videolan
- audio
- lame

That's let me watch/convert from most video and audio formats, convert/extract/build pdf files, ...

If VS would only add one thing.....tell the actual DLL that was not found when it reports a DLL or one of its dependencies could not be loaded........................

The OS has the information since it could not find/load the DLL and should have a way to report it back to VS in debug mode.....




bob
Wednesday, 06 July 2016 11:23:31 UTC
Note surprisingly, but fortunately for me, powershell can pipe output to clip. e.g.


Get-AzureRmResource | clip
Thursday, 07 July 2016 11:29:11 UTC
somecommand.bat > my.out 2>&1

...to get both output and error in the one file.
Renny
Friday, 08 July 2016 12:58:10 UTC
Thank you for the post :) Clip and F7 shortcuts was new to me. Thank you
Friday, 08 July 2016 14:57:54 UTC
Use & to run commands sequentially, whenever you are waiting for a command to end before entering a second one.

Example:
ping google.com & ping bing.com



Sunday, 10 July 2016 22:13:41 UTC
Pushd and popd are getting a lot of love here already, but add to that: they're cross-platform! Not just valid in cmd.exe and PowerShell, but bash and pretty much every Linux and Mac terminal I've used as well!
brianary
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.