I’ve been revisiting an old friend from the days of DOS and ancient versions of Windows. I had forgotten how much work this friend did for me. The more I thought about it, and the more I caught up with this friend, the more I realized there is a entirely new generation of system administrators that have never met him.
My old friend is DosKey. DosKey was one of the earliest automation tools and I think it’s still got some legs. Now before you jump to any age-ist conclusions, you need to see what he can still do. If you are like me, you spend a lot of time at a command prompt. I really hate typing and am always in a hurry. I also dislike having to reach for a mouse and navigating my increasingly complex Start Menu. With DosKey, if I want to start Microsoft Excel, all I need to do is type xl. It doesn’t matter what directory I’m in. Nor does the application I’m starting have to be in the path. All I need to do is to define a doskey macro at the command line by typing:
doskey xl=”%programfiles%\Microsoft Office\Office11\Excel.exe” $1 $2 $3 $4 $5
You can use variables like $1 and $2 (all the way up to $9) much like you would use %1 and %2 in a batch file. In this situation, from a command prompt I can type xl file1.xls file2.xls file3.xls and have Excel open all three files. Sure, with file association if I type file1.xls, Excel will open. My macro lets me open multiple files with a single,easy command. And what if I want to open a file with a different application?
CSV files (at least for me) are associate with Excel. If I type testusers.csv at a command prompt, Excel will launch the file. But what if I want Notepad? Well I can save a few keystrokes (they do add up) with a macro like this:
doskey np=Notepad.exe $1 $2 $3 $4 $5
With this macro loaded, I can run np testusers.csv at a command prompt and the file will open in Notepad.
To see what macros are loaded into memory type
doskey /macros
I can create a doskey macro to execute any command or series of commands. Suppose I always use /all when I run ipconfig. That’s too much typing. I’d rather have a macro called ip that does the work for me.
doskey ip=ipconfig /all
Now at a command prompt I can simply type ip.
I did a MCPMag.com TipSheet column on some of my favorite one-line expressions. They can be defined as DosKey macros. I also took some one-liners from a helpful reader’s followup and added macros like these:
doskey mac=nbtstat -A $1 |find /i “MAC Address”
doskey disabled=dsquery user domainroot -disabled -limit 0
doskey stale=dsquery computer domainroot -stalepwd 180 -limit 0
I can call batch files with a Doskey macro. I created a short batch file to launch PowerShell directly from the command line. I can call that batch file with a macro.
doskey posh=s:\posh.bat
Or I can call a PowerShell command directly via a macro. Here are several:
doskey ps=powershell -nologo -command “& {get-process}”
doskey svc=powerShell -nologo -command “&{get-service|where {$_.status -eq ‘Running’}}”
doskey showproc=powershell -nologo -command “& {get-process -name $1}”
By now, I hope you are beginning to see the possibilities.
One downside to Doskey macros is that they only exist for the life of your command window. And they have to be loaded everytime. Fortunately, you can save all your doskey macros to a text file and load that file into Doskey. I’ve attached a copy of my doskey macros. To load them into memory run:
doskey /Macrofile=savedtopath\MyMacros.txt
If you want the macros to be available immediately and automatically, you’ll need to modify the shortcut that starts CMD.Exe. In the Target field you’ll see something like
C:\WINDOWS\system32\cmd.exe /K
Simply add
doskey /macrofile=path\mymacros.txt
to the end of the command. This will load the macro file every time you start CMD from this shortcut.
With a little help from an old friend, you can accomplish a lot of work with very little effort. Your biggest challenge will be trying to remember all the macros you’ve created (so you aren’t constantly running doskey /macros), which if you are my age, gets a little more challenging all the time.
how do u use doskey with internet explorer as a keylogger?
DOSKey is only available in a CMD session. If you are trying to automate a task in IE, you could use the SendKeys method of the WshShell object. Or look at a product like AutoIT.