Besides all the changes to the GUI that happened post-Windows XP, they also made one useful change to the command prompt. When you run a command, it’s now possible to pipe output to the clipboard.
If you’re like me and write a lot of documentation, or you just take a lot of notes while doing computer maintenance, it’s a big boon.
All you have to do is pipe the output to a command called clip, like this:
dir | clip
DOS veterans probably remember piping the output of dir through more to make the command pause at each screenful in the bad old days before dir got the /p switch. Exact same concept.
Then you can paste the output into any clipboard-aware application, like Notepad or Word.
If you want a quick way to empty the clipboard from a batch file to cut memory usage in a hurry, save this as a batch file and run it as desired:
type "a" | clip
It doesn’t quite clear the clipboard, but having a single character on the clipboard is almost as good.
More fun with pipes
You can also use pipes to glean more information from dir. Maybe you need to know how many PDF files you have. Pipe a dir command through find, and you can know in a matter of seconds:
dir /s c:\ | find /c /i ".pdf"
Find is a useful command. Use the command find /? to learn more about what it can do. I once had to use it to try to find every textfile containing known passwords on a large network after a team of penetration testers gained one of the other administrator’s passwords, took a stroll around the network, and sent a bunch of screenshots to upper management. I had all weekend to find them all, and they were going to try again first thing Monday morning. If they succeeded, I might not have had a job afterward. My boss suggested I start writing some scripts. I did, and then some of my coworkers helped me run them. We found a a bunch of files, changed all the passwords we found in them, deleted the files, and the penetration testers didn’t succeed.
I guess you could say find and pipes saved my job once. It didn’t get me a raise–not directly–but I got a pretty good employee evaluation that year, as I recall.
You can string pipes too. Let’s say you need to do an operation on every database server on your network. Assuming all those machines have the string DB in their name (big assumption, I know), you can capture them all with this command:
net view | find /c /i "DB" | clip
Open up or switch to Notepad and hit ctrl-v, and there’s your list.
What to do with that list? Let’s say you want to mess with your security people. Take that list of servers, delete the extraneous information and clean it up so the names are all on one line. Then you can do something like this:
for %i in (\\stldb01 \\stldb02 \\stldb03) do copy never_gonna_give_you_up.txt %%i\c$\passwords.txt
If Rickrolling them is too obvious, there’s a photo of David Hasselhoff in a speedo floating around that you could distribute as passwords.jpg.
On a more practical level, I used to use this trick to copy the quarterly Oracle security updates out to all of our database servers. Only I used robocopy, and let it run over the weekend. That way it didn’t matter how long it took or how many retries it needed. By start of business on Monday, all the servers had local copies of the updates.
Pipes are normally associated with Unix. While Unix certainly makes a lot more use of them, they’re a powerful tool in Windows too.
I have a few more useful Windows scripting tricks if you’re interested.