Search code examples
cmdalias

Create alias for a specific command in cmd


Is there anyway to create an alias to cd PATH command in cmd?

For example, instead of typing cd C:\John\Pictures I'd like to type just pictures in cmd and press Enter and it should simply take me to C:\John\Pictures.

Is that possible and how?


Solution

  • You will need to use the doskey command which creates aliases. For example:

    doskey note = "C:\Windows\System32\notepad.exe"
    note
    

    creates a macro to open Notepad, then calls it. The macro name (note in the above example) must be valid (e.g. no spaces are allowed, may begin with an underscore).

    You can also use parameters:

    doskey note = "C:\Windows\System32\notepad.exe" $1
    note "C:\Users\....\textfile.txt"
    

    By default, doskey macros are only saved for the current session. You can work around this limitation in two ways:

    1. Save the macros in a text file, then load them each time you need them:

      A command like:

      doskey /macros > %TEMP%\doskey-macros.txt
      

      Will save all the current macro definitions into a text file.


      Then to restore all the macros at a later date:

      doskey /macrofile=%TEMP%\doskey-macros.txt
      
    2. After saving the macros in a text file as shown above, instead of loading them every time, run:

      reg add "HKCU\Software\Microsoft\Command Processor" /v Autorun /d "doskey /macrofile=\"%TEMP%\doskey-macros.txt\"" /f
      

      so that the macros are set each time you open the cmd.

      See this SuperUser answer for more information.


    Note: You cannot use command aliases in a batch file.