This is a two-part series, highlighting the technique of command aliasing in Windows with DOSKey and PowerShell. Part 1 covers DOSkey.

I love scripting, but sometimes you need to test just commands on the command line. Quite often, the syntax is either too cryptic or too long to remember correctly. One solution is to use aliases—short words or even acronyms that perform the complex command.

UNIX has had aliases from the start, whether using the Bourne, Korn, or Bash shell, but what about Windows? Windows has not had the richest of command line tools until PowerShell was released; however, ever since 1995, the shell has included a little-used command that supports aliasing. Its name is DOSKey. If you use help, it describes itself as “edits command lines, recalls Windows commands, and creates macros.” While true, macros are out of scope here. I want to talk about simpler commands.

For this article, I will use the word “shell” to refer to the Windows command prompt (or DOS box/DOS prompt) that runs when you type “cmd” into the Start menu’s search field.

How to use DOSKey ^

The DOSKey syntax is very simple. You simply enter a word, which is the alias, followed by the equal symbol and the command or action you want to perform. For example:

doskey test=notepad

Microsoft uses the word “macro” instead of “alias,” especially in the help. The list below is a slight rewrite of the command help:

/MACROS Shows all the aliases

/HISTORY Shows the commands you’ve typed in the current session (or use F7)

/MACROFILE=filename Lets you specify a text file to load with aliases

You can use a few codes to extend the power:

$T Allows you to use multiple commands

$1-$9 Is the equivalent to %1-%9 in batch programs

$* Replaces the symbol with everything following the macro name on the command line (that is, it’s the argument)

The important thing to realize about DOSKey is that it only applies to the current command prompt. This is both a blessing and a curse. You could, for example, have four shells open, each with their own aliases loaded. The curse is that any new shell you open will forget any aliases you had, unless you type or load them into that instance.

DOSKey examples ^

Some common examples to help you be slightly more productive are as follows:

AliasWhat It Does
doskey d=dir $*Lists a directory
doskey n=notepad $*Opens a file with Notepad (for example, n text.txt)
doskey ..=cd ..\..Changes to the parent directory

These are very basic examples, but they get the point across. You can make things as simple as you like, but tailor them to your own vocabulary. I like to use aliases to turn the arcane into plain English. Some more deployment-related examples are as follows:

AliasWhat It Does
Doskey maker=wmic csproduct get vendorGets a machine’s verndor
Doskey model=wmic csproduct get name 

 

Gets a machine’s model name
Doskey version=wmic csproduct get Version 

 

Gets a machine’s model version
Doskey patches=wmic qfe list full /format:htableLists all patches installed on a machine
Doskey getBios=WMIC BIOS Get Manufacturer,Name,Version /Format:csvShows BIOS details

The most common use of aliases is to save from typing long, complex commands. This is done in a very UNIX-like style, using two- or three-letter abbreviations. For example, “ed” is great for handling text files.

ed=notepad $*

It’s also useful for creating aliases for DOSkey itself.

AliasWhat It Does
dk=doskey $*Creates a new alias
dkm=doskey /macrosLists current aliases
dkh=doskey /historyLists the command history

Getting a little more complex, fs=findstr /s /i $* will search files using another underused command, findstr. Here’s the sample output to search VBS files for the word “Canon”:

C:\>fs Canon* c:\s\*.vbs
C:\s\VB_LDAPnaming.vbs:'Read the canonicalName of Administrator
C:\s\VB_LDAPnaming.vbs:user.GetInfoEx Array("canonicalName"), 0

How to use aliases permanently ^

  1. Create a shortcut.
  2. Edit the “start in” path to something sensible, such as %homepath%.
  3. Create a batch shell-startup.bat with the following contents:
    Doskey /MACROFILE=myaliases.txt
  4. In the target, run the bat file by entering cmd.exe /k shell-startup.bat.

To force all shells to use a single alias file, you can use the registry key “autorun,” as specified on Technet. The following sample registry file will work fine:

Subscribe to 4sysops newsletter!

REGEDIT4
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"CompletionChar"=dword:00000009
"DefaultColor"=dword:00000000
"EnableExtensions"=dword:00000001
"PathCompletionChar"=dword:00000009
"Autorun"="c:\\s\\shell-startup.cmd"
4 Comments
  1. iMedia Designs 8 years ago

    Nice, article , helpful,thanks.

  2. Bhimji 6 years ago

    This is great article.

    I am trying to run my existing doskey aliases in PowerShell.  Could you share details on how to achieve same on PowerShell?

     

    Thanks

    • Sync 6 years ago

      Thank you this is really helpful 🙂

      I’m using Python Django and typing “python manage.py” before every server command is becoming painful.

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account