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
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:
- Open Command Prompt.
- 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:
- Open a Command Prompt and type:
doskey /macrofile=yourAliasFile.txt
- 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
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
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
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
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
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
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:
With the alias created with DOSKEY, the command prompt will change the active drive and the current folder automatically:
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
Or something like this:
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.
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!
Developers are always looking for ways to optimize their productivity. The latest addition that I did to improve mine was to create aliases for the most common commands that I use every day. I have entirely pivoted to using the new Windows Terminal for my day-to-day command line activities.
Windows Terminal is a modern, open-source application that aggregates multiple terminals and shells such as command prompt, PowerShell, and WSL and adds features such tab support, rich text, configuration, and visual themes to them. In this article, I will show you how you can improve your development workflow by reducing the number of keystrokes you punch in your terminal daily.
As developers, we often need to type the same commands repeatedly, which reduces productivity and creates distractions. To save some time, you can create aliases for some of the most common commands. The alias command is a text interface for your terminal or shell commands that map to lengthy and\or complex commands under the hood.
The process to create aliases is slightly different for Linux\MacOS\Windows WSL users from Windows users. For the Linux family of OS, use the alias command similar to the following to create aliases.
After executing the previous command, you can use the character “d” instead of the command “docker” in the same terminal session. You can create permanent aliases in Linux that work across sessions by persisting the aliases in shell configuration files such as ~/.bashrc for bash, and ~/.zshrc for ZSH.
In Windows, you can create aliases using the DOS command: doskey, which is useful for creating macros. You can read more about the doskey command here. Similar to the alias command, the doskey command stays in effect only during the session.
There is a way to put doskey instructions in a batch or command file and later use the file to initialize any terminal session by making a small change to the Windows registry. However, the process is prone to errors and has severe side effects. You can read about the drawbacks and a horror story of modifying the registry to execute an initialization script here.
Let’s discuss a safe way to ensure that command prompt executes the aliases script only for terminal sessions initiated by the user. This approach is not limited to aliases, and you can add other commands to the initialisation script too. Such commands may include the ones you want to execute before launching a terminal, such as changing to a particular directory and so on.
Create a command file named init.cmd (the name is irrelevant) and add the following commands to it.
@echo off
doskey k=kubectl $*
doskey d=docker $*
doskey gp=git pull
doskey gc=git commit -a -m "$*"
In the previous listing, we assigned aliases to some common commands that we use regularly. The command argument $* is one of the special characters that substitutes itself with the argument that you pass to the command. There are several others, and you can read about them here.
Next, place the file at a permanent location and now fire your Windows Terminal instance. Bring up the Windows Terminal settings by clicking on the gear icon in the menu or by using the macro key combination “Ctrl + ,” in the terminal window. Windows Terminal saves and reads settings from a JSON document named profiles.json. You can modify the many configuration options in this file to make the overall application, and each shell, act/look like you want. For now, search for the following setting in the JSON document.
The setting shown previously instructs the Windows Terminal process to launch the command interpreter, cmd.exe. Update this command to instruct the command interpreter to execute your command file on initialization and then continue. Remember to substitute the file path below with the path to the init.cmd file that you created previously.
"commandline": "cmd.exe /K C:\\InitCmd\\init.cmd"
The /K command parameter instructs the interpreter to carry out the command specified in the argument and continue execution. There are other parameters for the interpreter that you can read more about here. Now that your settings are in place, restart the terminal and enjoy the goodness of simpler commands. The following screenshot presents the output of some commands I executed on my terminal.
Did you enjoy reading this article? I can notify you the next time I publish on this blog… ✍
alias are easy way to do long command short, in Unix there are .bashrc file where we can set alias but in Windows I did not find such thing straight forward, so here is the approach to do so.
approach 1
usually we can open command prompt ( win key + R ) and type cmd and set alias using doskey
, for eg.
doskey ll=dir
and now when run ll
it will list all files of that folder.
which works good but problem in this approach is
- we have to set every alias separately
- these alias are temporary and only for the active session, as soon as we close the command prompt, alias are gone.
approach 2
To overcome both of the above mentioned problem, following are are steps to set permanent and multiple alias
- first run below command in your command prompt
echo %USERPROFILE%
, output of above is the base path , in my case it is C:/Users/User
-
now create a new file name alias on above location, you can name it on your ease, even with .txt extension also
-
now open this file in notepad or editor and add your daily use alias in it and save the file, mine are as below
cat=type $*
..=cd..
ls=dir $*
gs=git status
gp=git push
gb=git branch
nrs=npm run start
dev=cd /d D:\Developer
rct=cd /d D:\Developer\react-project
Enter fullscreen mode
Exit fullscreen mode
Note:
— you need to add /d if you create an alias to navigate to any directory.
— path separator is forward slash *\ .
— there is no space around = while creating alias.
now next part is to how to make it permanent so that it is available whenever you open the command prompt.
- now open run win key + R and type
regedit
; it will open a new dialog window which is registry editor. - navigate to Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor File
- right click to this file and select New >> String Value and rename it to Autorun and save it.
- now double click on this newly created Autorun key, a window open where you have to put below value in value data field (the address will be full path of your alias file)
DOSKEY /MACROFILE="C:\Users\User\alias"
Enter fullscreen mode
Exit fullscreen mode
- close the regedit and command prompt for once.
Now try your alias and thank me later.
let me know if it was helpful or something incorrect while you try, I would be happy to help
Cheers.