Forgotten (but Awesome) Windows Command Prompt Features
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."
Windows folk: Did you know you can pipe cmd prompt commands into the clipboard? pic.twitter.com/fSE7ZHBBbB
— Scott Hanselman (@shanselman) June 20, 2016
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.
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?
Transparent Command Prompt
After Windows 10, you can make the Command Prompt transparent!
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.
Finally New Microsoft has its priorities straight. Early builds of ANSI Art support making its way into Windows 10! pic.twitter.com/UjWCrnrED2
— Scott Hanselman (@shanselman) February 5, 2016
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.
About Newsletter
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.
https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/tree.mspx?mfr=true
dir /b /s | clip
mode con rate=32 delay=1
echo AT&F&C1&D2M1L3S11=33 > COM2
... old school
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
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
http://myscreenshot.info/i/0006/675681466471541254203136354016.png
e.g.
where dotnet.exe
C:\Program Files\dotnet\dotnet.exe
copy con lazydog.txt
The quick brown fox jumps over the lazy dog
Ctrl-z
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
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.
Like:
dir /b | find ".exe" > findlist.txt | notepad findlist.txt
https://technet.microsoft.com/en-us/library/bb490982.aspx
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...
notepad *.txt <tab>
@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
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.
findstr /s /i /n foobar *.txt
doskey /reinstall /exename=[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.
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)
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".
start .
Open a File Explorer for the current directory. Because sometimes dir doesn't cut it.
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. :)
@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.
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")
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.
All those times I manually copied "ipconfig /all" to notepad...
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.
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
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 ;-)
'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.
ip config /all
or you can see are you getting the correct bytes or not by typing
ping google.com
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.....
Get-AzureRmResource | clip
Example:
ping google.com & ping bing.com
Comments are closed.
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.