Alias в windows cmd

Be it Linux, macOS, or Windows, we sometimes need to use the terminal or the command line to perform certain commands. If in such situations, we find ourselves repeating a few long commands we can use an alias to save time and make it easier.  In windows, we can create an alias as a batch command from the command prompt or the Powershell. We will see how to create and work with an alias in Batch scripting.

What is an Alias?

Alias is a shorthand command to replace another command. We can think of it as a shortcut command for multiple or long commands. It can be also understood as a map of the shortcut keyword command with the actual command. The shortcut command is replaced with the string of commands which gets executed. 

Creating an Alias

To create an alias in a windows environment or a BATCH script, we can open up a CMD or Powershell instance.

The structure of an alias is as follows:

doskey parameters  [shortcut command] = [commands to be executed]

A simple alias in BATCH scripting will look something like the following:

doskey cdoc = cd  C:\Users\Admin\Documents

We use the keyword doskey to create an alias, the alias is simply created by specifying the shorthand command in this example the word cdoc and the final piece of information is the actual command we want to replace with this alias. Thus when we create this alias, the command cdoc executes the command cd C:\Users\Admin\Documents and hence we optimize and create a shortcut to save some time and effort.

This will create an alias for this CMD/Powershell instance only, once you exit the session or window the alias is no longer working and hence was a temporary alias.

Creating a permanent alias

To create a permanent alias, we can specify these same commands of doskey in a .bat file which is also known as the Batch script or file. After the batch script/file is created with the alias you required we can copy the path of this batch file and add it to the CMD/Powershell target to add the alias in the initialization of those environmental shells(CMD/Powershell)

Follow the steps to create a permanent alias:

Creating a batch file

We can create a simple file with any name you prefer but with a .bat extension. This batch file will contain the alias that we can use in the shells(CMD/Powershell) permanently. After creating the batch file, you can add your preferred alias in separate lines as follows:

@echo off
doskey cdoc = cd  C:\Users\Admin\Documents
doskey ls = dir

You can add other aliases as per your requirements and desire. We have used the command @echo off so that the script doesn’t print itself while executing.

Save the file and copy the location of the file by Shift + Right-clicking the file in the windows explorer. Then select the option Copy as path and the path of the batch file will be copied in your system clipboard.  

After copying the path of the file, we can move on to the next step which is to add the path to the shell program target.

Adding the batch file path to the CMD/Powershell target

  • Right-click the CMD/Powershell icon and then click on the properties option.
  • There you will get a window that should have multiple options, click on the shortcut tab.
  • In the shortcut tab, add the character /k and paste the file location.
  • And now if we restart the CMD/Powershell the alias will work and will stay permanently until we delete them from the batch file which is linked to the shell environments.

As we were able to see the thing working, the alias was loaded from the batch file which we created and linked in the CMD or Powershell’s target. This way we can keep multiple aliases in a single place or assemble multiple alias from different paths to a shell in Windows.

Replacing an Alias

To replace an alias in a batch environment, we can simply edit the shortcut name to the command we want to replace with. 

doskey [shortcut command] = [new commands]

We can simply redefine the shortcut alias to the new command just as we do with variables in programming.

Note: You need to use the doskey if you are writing your aliases in the file or a batch script, but if you are creating aliases just in the CMD/Powershell instances then you don’t need to write the keyword doskey in the command.

For example, we can write the cdoc alias again as follows:

We have redefined the alias to a new command and it can now work with the latest command it was changed to.

Deleting an Alias 

To disable or delete an alias is quite straightforward. We can set the shortcut alias command to empty and the Shell environment will not consider it as a valid command anymore.

doskey [shortcut command] =

Thus, we were able to delete or disable an alias in a batch script by setting the alias to an empty value.

Options/Parameters in Batch Aliases

We can use certain parameters and options in the alias to integrate certain features that can be more used to make the alias more flexible. 

There are several parameters as follows:

parameter/option description
/history to get the history of the current batch environment (commands executed in CMD/ Powershell)
/exename to execute the macro with an executable in the system path
/macrofile to include a file that contains the macro to be used

There are a lot of options and parameters we can use to enhance the alias in our batch script

Note: The entire list of options/parameters can be found in the official Microsoft docs.

We can even use command line arguments in the alias as we do with the normal batch script. This can be a really powerful tool to have especially for more complex batch tasks and scripts. Let’s say we want to create an alias that creates a folder and changes our current directory into the created folder. We can use the positional parameter specified as the folder name and create and cd into the folder.

doskey md = mkdir $1 &t cd $1

We can use the $1 to indicate the first parameter passed to the alias and $t to separate the commands in the alias.

Thus, we can now dynamically use our alias in a batch script or CMD/Powershell environment. This can be further used as per the specification of the commands.

Windows CMD aliasing allows you to create shortcuts for longer commands, making them easier to use by defining your own commands with the `doskey` utility.

Here’s an example of how to create an alias:

doskey ll=dir /w /p

Understanding CMD Aliases

What is a CMD Alias?

A CMD alias is essentially a shortcut that allows you to execute commands with a simpler name or less typing effort. Think of an alias as a nickname for a command; it lets you use a shorter or more memorable term instead of the full command. For instance, instead of typing `ipconfig /all` to display your network configuration, you could create an alias that lets you just type `networkinfo`.

The primary distinction between an alias and a standard command is that aliases are customized commands made by the user, while standard commands are built into the Windows Command Prompt.

Benefits of Using Aliases

Using Windows CMD aliases comes with various advantages:

  • Time-Saving Advantages: By creating a concise command for frequently used commands, you reduce the time spent typing long commands repeatedly.
  • Enhanced Productivity: Short commands lead to increased efficiency, allowing you to perform tasks faster without fiddling with lengthy syntax.
  • Customization: You have the freedom to define commands that suit your workflow, tailoring them to your everyday tasks.

Windows Cmd Clear: Refresh Your Command Line Effortlessly

Windows Cmd Clear: Refresh Your Command Line Effortlessly

Creating CMD Aliases

How to Create an Alias in Windows CMD

Creating an alias in Windows CMD can be done easily using the `doskey` command. Here’s a straightforward process on how to do that:

  1. Open Command Prompt.
  2. Use the `doskey` command to create an alias. The basic syntax is:
doskey aliasName=command

For example, to create an alias that lists directory contents, you can do:

doskey ls=dir

Now whenever you type `ls`, it will run the `dir` command.

Permanent vs Temporary Aliases

Defining Temporary Aliases

Temporary aliases exist only for the duration of the Command Prompt session. They are removed once you close the CMD window. To create a temporary alias, follow the steps mentioned previously. For example:

doskey cls=clear

This alias will work fine during that session but will be lost once you close CMD.

Defining Permanent Aliases

In contrast, a permanent alias remains stored even after you close CMD. To create permanent aliases, you need to set them up in your CMD configuration using `doskey`. Here’s how:

  1. Open a Command Prompt and type:
doskey /macrofile=yourAliasFile.txt
  1. In `yourAliasFile.txt`, you can define your aliases in the same way:
ls=dir
cls=clear

Every time you open CMD, simply run the above command to load your macro file, and your aliases will be ready for use.

Mastering Windows Cmd Disk Management in Minutes

Mastering Windows Cmd Disk Management in Minutes

Managing Aliases

Listing Existing Aliases

To see all the aliases currently available, you can list them by executing:

doskey /macros

This command will display all the active aliases, helping you keep track of what you have created.

Editing Aliases

If there is a need to modify an alias, you can simply redefine it using the `doskey` command. For instance, if you want to update the `ls` alias to include a specific option like detailed view, you can do:

doskey ls=dir /w

Deleting Aliases

To remove an alias you no longer need, use the `doskey /delete` command followed by the alias name:

doskey /delete ls

This command will remove the `ls` alias from your current CMD session.

Mastering Windows Cmd Arp for Networking Success

Mastering Windows Cmd Arp for Networking Success

Advanced CMD Alias Features

Using Parameters in Aliases

Creating aliases that accept parameters can make your commands more flexible. For example, if you want to create an alias that opens a specific FTP server, you can do:

doskey ftp=start ftp $*

Here, typing `ftp myserver` will execute `start ftp myserver`, allowing for dynamic usage based on user input.

Using Batch Files with Aliases

You can also create batch files and use them as aliases for commands that require multiple steps. For instance, if you have a batch file named `runpython.bat`, you can define an alias for it:

doskey runpython=python C:\scripts\script.py

Now, typing `runpython` in the command line will execute the specified Python script.

Unlocking The Power Of Windows Cmd Whois

Unlocking The Power Of Windows Cmd Whois

Troubleshooting Common Alias Issues

Common Problems with Aliases

As with any feature, users may encounter common issues when using Windows CMD aliases. Some include conflicts with existing commands or aliases not working as expected.

One solution is to ensure that your alias names do not conflict with existing CMD commands or reserved keywords.

Checking Compatibility

Not all versions of Windows support CMD aliases in the same way. Ensure that you are using a compatible version of Windows. If you experience issues, confirm whether your Windows version has limitations related to CMD features.

Windows Cmd Repair: Quick Fixes You Need to Know

Windows Cmd Repair: Quick Fixes You Need to Know

Best Practices for Creating and Managing Aliases

To maximize the benefits of CMD aliases:

  • Effective Naming Conventions: Keep your alias names intuitive and descriptive to minimize confusion.
  • Organization: Group similar aliases together within a file if utilizing them in bulk.
  • Documentation: Maintain a list of your aliases and their functions, updating them as necessary.

This practice will not only help you remember what each alias does but also assist others if they need to use your system.

Mastering Windows Cmd Switches: A Concise Guide

Mastering Windows Cmd Switches: A Concise Guide

Conclusion

In summary, using Windows CMD aliases can significantly enhance your command-line efficiency. They streamline repetitive tasks, increase productivity, and allow for tailored experiences in your command-line interactions. Embrace the power of aliases and start customizing your CMD environment today.

Mastering Windows Cmd Version: Essential Tips and Tricks

Mastering Windows Cmd Version: Essential Tips and Tricks

Call to Action

What are some of your favorite CMD aliases? Share them with us! Also, consider signing up for our newsletter to receive more tips and tricks related to Windows CMD usage.

In this article, I would like to share with you a useful way to define aliases for the command prompt. The method described in this article works in all modern Windows versions including Windows 10, Windows 8.1, Windows 8 and Windows 7. By following the steps below, you will be able to define any desired alias to extend the functionality of the default command processor (cmd.exe) and save your time.

There is a doskey command available in the command prompt. Using doskey, it is possible to define an alias for a new or existing console command.
For example, almost all users are familiar with the cd command which is used to change the current directory in the command prompt. If the desired directory is located on another drive, you need to use the «/D» switch with the cd command or enter the drive letter in the command prompt explicitly.
For example:

d:
cd documents

or

cd /d d:\documents

Using DOSKEY, it is possible to save your time and define an alias which will allow you to omit the requirement to enter the drive letter and the /D switch. For example:

doskey cd=cd /D $*

The following are some special codes in Doskey macro definitions:
$T Command separator. Allows multiple commands in a macro.
$1-$9 Batch parameters. Equivalent to %1-%9 in batch programs.
$* Symbol replaced by everything following the macro name on the command line. We used it in our alias.
Now, we can compare the results without the alias and with the alias.
Without the alias, the cd command will not change the active drive:

cd without alias

With the alias created with DOSKEY, the command prompt will change the active drive and the current folder automatically:

cd with alias

This is very useful.

Using DOSKEY, it is possible to define your own aliases. For example, you can create the LS alias for the DIR command to use a common command for directory listing in Windows and Linux. As you may or may not be knowing, LS is a default file listing command in the Linux operating system.

doskey ls=dir

ls alias

Or something like this:

ls alias 2

Define global aliases in the Windows command prompt

The problem with aliases is that they work only for the command prompt instance where you have defined them. To avoid this issue, you can create a new shortcut to cmd.exe or even modify the default one. You need to add the following parameters after the cmd.exe part:

cmd.exe /k c:\apps\cmd\aliases.cmd

Here the file c:\apps\cmd\aliases.cmd is a regular batch file which contains the appropriate DOSKEY calls.

command prompt define alias

command prompt load aliases shortcut

command prompt loaded aliases

Besides the command prompt, Windows allows defining your own aliases for the Run dialog as well. Refer to the following article: Launch your favorite apps with useful aliases from the Run dialog

That’s it. This is a very useful way to define your own command or change the behavior of default commands. I am using these aliases since a very long time. What about you? Are you using aliases in the command prompt or were you not aware of this feature?

Support us

Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:

If you like this article, please share it using the buttons below. It won’t take a lot from you, but it will help us grow. Thanks for your support!

Windows¶

you may make the alias(es) persistent with the following steps:

  1. Create a .bat or .cmd file with your DOSKEY commands.
  2. Run regedit and go to HKEY_CURRENT_USER\Software\Microsoft\Command Processor
  3. Add String Value entry with the name AutoRun and the full path of your .bat/.cmd file.

For example, %USERPROFILE%\alias.cmd, replacing the initial segment of the path with %USERPROFILE% is useful for syncing among multiple machines.

This way, every time cmd is run, the aliases are loaded.

For completeness, here is a template to illustrate the kind of aliases one may find useful.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@echo off

:: Temporary system path at cmd startup

:: set PATH=%PATH%;"C:\Program Files\Sublime Text 2\"

:: Add to path by command

:: DOSKEY add_python26=set PATH=%PATH%;"C:\Python26\"
:: DOSKEY add_python33=set PATH=%PATH%;"C:\Python33\"

:: Commands

DOSKEY ls=dir /B
DOSKEY notes = cd Documents/Notes/notes
:: DOSKEY sublime=sublime_text $*
:: sublime_text.exe is name of the executable. By adding a temporary entry to system path, we don't have to write the whole directory anymore.
:: DOSKEY gsp="C:\Program Files (x86)\Sketchpad5\GSP505en.exe"
:: DOSKEY alias=notepad %USERPROFILE%\Dropbox\alias.cmd

:: Common directories

::DOSKEY dropbox=cd "%USERPROFILE%\Dropbox\$*"
::DOSKEY research=cd %USERPROFILE%\Dropbox\Research\

On MAC¶

On mac the process is similar to a Linux system, I will add more step later for now i will leave the alias and configuration i want to do

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
export PATH=$PATH:/usr/local/bin/pip3
export PATH=$PATH:/usr/local/bin/pip3.7
export PATH=$PATH:/Users/name_of_the_user/Library/Python/3.7/bin
export PATH=$PATH:/usr/local/bin/
alias ls='ls -GFh'
export PS1="\w [\u]➤ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias Documents='cd ~/Documents'
alias notes='cd ~/Documents/001.My_notes'
# ---------------------------------------------------------------------------
# Git Aliases
#----------------------------------------------------------------------------

alias gall='git add .'
alias gc='git commit -m'
alias gpush='git push origin master'
alias gpull='git pull origin master'

If you are like me and use mac and Linux at home and use windows at work. Then the following will be regular errors.

‘ls’ is not recognized as an internal or external command, operable program or batch file.

I always use ls, clear, cat etc. in my CMD shell and eventually get the following errors.

This has happened a lot of time for me to do something about it. Fortunately, there’s an easy way to setup alias in the cmd windows and never see this nasty ‘not recognised’ error. First create a text file like this.

You can use any other commands that you use regularly. Next, open the cmd windows and type the following command

Doskey /macrofile=path/to/alias/file/filename

As shown in the next figure. That’s it.

Here’s how it works, no more nag error messages

Don’t want to type this message every time, create a windows shortcut like this  

That’s it!!

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Windows распределенная файловая система
  • Windows 10 mkv audio
  • Canon pixma ip2000 драйвер windows 10
  • Как поставить на заставку видео на windows 10
  • Самая маленькая windows vista