Skip to content
Related Articles

Related Articles

Batch Script – Aliases

Improve Article
Save Article
Like Article
  • Last Updated : 14 Jan, 2022

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:

/historyto get the history of the current batch environment (commands executed in CMD/ Powershell)
/exenameto execute the macro with an executable in the system path
/macrofileto 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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!