Scott Hanselman

Forgotten (but Awesome) Windows Command Prompt Features

June 21, 2016 Comment on this post [56] 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
Hosting By
Hosted in an Azure App Service
June 21, 2016 2:22
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.
June 21, 2016 2:32
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
June 21, 2016 2:40
A newfound favorite of mine from XP: tree

https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/tree.mspx?mfr=true
June 21, 2016 2:42
You can also make cmd fullscreen by hitting F11 :)
June 21, 2016 2:45
cd | clip
dir /b /s | clip
mode con rate=32 delay=1
echo AT&F&C1&D2M1L3S11=33 > COM2
... old school
June 21, 2016 2:55
why Microsoft doesn't give some love to promp-command? it looks so archaic compared with bash.
June 21, 2016 4:56
I wanted to read your web in Edge using the reading mode, but when I switched I could only see the disclaimer.
June 21, 2016 5:03
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

June 21, 2016 5:11
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


June 21, 2016 5:13
Transparency disabled on my latest Microsoft Windows [Version 10.0.14342]
http://myscreenshot.info/i/0006/675681466471541254203136354016.png
June 21, 2016 5:14
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

June 21, 2016 7:04
Denis,
Disable legacy mode on options tab.
June 21, 2016 7:34
Create a text file

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

June 21, 2016 10:50
cd \win*

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

cd syste[tab][tab]
June 21, 2016 11:18
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
June 21, 2016 12:02
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.
June 21, 2016 13:00
Ctrl-V to paste from clipboard (how long we've waited for!).
June 21, 2016 13:11
copy con autoexec.bat
prompt $p$g
win

^Z
June 21, 2016 13:13
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
June 21, 2016 13:28
This stuff is gold :)
June 21, 2016 13:33
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...



June 21, 2016 14:15
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>

June 21, 2016 15:55
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
June 21, 2016 15:57
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.
June 21, 2016 15:58
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.
June 21, 2016 16:35
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
June 21, 2016 17:47
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".
June 21, 2016 18:46
ALT + ENTER isn't working for me =(
June 21, 2016 19:26
Sorry, one more favorite of mine


start .

Open a File Explorer for the current directory. Because sometimes dir doesn't cut it.
June 21, 2016 19:36
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. :)
June 21, 2016 19:45
dir /s | more
for pausing output per page
June 21, 2016 21:06
worth to mention that "|clip" and F7 works in Powershell also
June 21, 2016 23:47
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…
June 22, 2016 0:45
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.
June 22, 2016 3:33
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")


June 22, 2016 5:50
"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.
June 22, 2016 6:41
Am I the only a little bit nostalgic for edlin, or debug (it was a great hex editor for small files!)
June 22, 2016 10:24
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.
June 22, 2016 10:25
Awesome Scott and thanks a lot for sharing these :)
June 22, 2016 14:52
shutdown /s

is my favorite. It opens a hidden game in Windows. You will love it.
June 23, 2016 0:57
Thanks for sharing this.

All those times I manually copied "ipconfig /all" to notepad...
June 23, 2016 1:23
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.
June 23, 2016 3:37
Great post!
June 23, 2016 17:41
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
June 23, 2016 19:54
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 ;-)


June 25, 2016 23:12
type: hostname
in the command prompt to get your computer name.
Great post.
June 27, 2016 22:19
type whoami in a command prompt if you forgot who you started the shell as.
June 29, 2016 6:08
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.
June 30, 2016 11:30
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
July 02, 2016 3:15
'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
July 06, 2016 15:23
Note surprisingly, but fortunately for me, powershell can pipe output to clip. e.g.


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

...to get both output and error in the one file.
July 08, 2016 16:58
Thank you for the post :) Clip and F7 shortcuts was new to me. Thank you
July 08, 2016 18:57
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



July 11, 2016 2:13
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!

Comments are closed.

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