17

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?

3
  • 5
    Take a look at doskey and its macro feature...
    – aschipfl
    Commented Feb 25, 2019 at 13:51
  • 3
    As an aside, you might want to do cd /d instead of just cd. Commented Feb 25, 2019 at 14:28
  • 1
    Thanks guys, exactly as you said. Just one mroe thing, in order to make it work persistently, you need to add /K file_path to Target in cmd.exe properties, example: %windir%\system32\cmd.exe /K C:\use-macros.cmd. use-macros.cmd file should include the command: doskey /macrofile=macinit, which cmd.exe will run on startup and that command simply allows you to use the stored macros. Commented Feb 25, 2019 at 17:36

3 Answers 3

25

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.

20

Here is an alternative for Windows 10:

1. Create a file called init.cmd and save it to your user folder

C:\Users\YourName\init.cmd

@echo off
doskey c=cls
doskey d=cd %USERPROFILE%\Desktop
doskey e=explorer $*
doskey jp=cd C:\John\Pictures
doskey l=dir /a $*

2. Register it to be applied automatically whenever cmd.exe is executed

In the Command Prompt, run:

reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f

3. Restart the Command Prompt

Now close/open the Command Prompt and the aliases will be available.

To unregister, run:

reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun
3
  • Worked beautifully. However, a quick glance at your delete command suggests that it will get rid of ALL autoruns rather than limiting the deletion to a specific file. Because if I have more than one command file configured to be autorun when CMD launches, then I lose that too. Commented Dec 5, 2022 at 22:54
  • @EskayAmadeus Fair, thanks. I will test with multiple commands and update the answer accordingly.
    – rbento
    Commented Dec 5, 2022 at 23:29
  • it doesn't work, and your cuotes are placed wrongly on item 2. Commented Sep 21, 2023 at 0:05
0

Easiest way:
Open Notepad in admin mode
go to C:\Windows\System32
New file
Contents: dir %1
Save it here as ls.bat
This will give you ls

Same way, git status %1 saved as gs.bat would give you gs instead of having to type out git status all the time. In the newer releases of windows there are many different ways to do this and they are (hopefully) getting simpler and easier, but this method will take care of commands you would type lots of times every day. Just make sure to keep a backup of the files you've created, somewhere outside System32, because some windows updates will wipe them out.

Not the answer you're looking for? Browse other questions tagged or ask your own question.