← Back to list

Shortcut to Efficiency: Crafting Aliases in PowerShell and Linux for Beginners

See how aliases can quickly simplify commands on your computer, making work easier in PowerShell and Linux.

myTiros Lang · 2024-03-04 20:19 · 59 claps · 5.6 min read paywalled
#powershell #linux #alias #beginner #tutorial
Open on Medium ↗
Wiki topics: 🔓 · Open Source 🛠️ · Crafts & DIY 🥊 · Combat Sports

Aliases — Shortcuts in PowerShell and Linux

When my Windows Subsystem for Linux (WSL) failed to process my sshrequests after upgrading to Windows 11 [1], I had to turn to Windows PowerShell to connect to the server. Although the ssh command for server connection are the same across both platforms, the process of setting up new aliases differs significantly. That’s because, in a Linux system, the command-line interface applies text-based shell scripts, like bash, while PowerShell utilizes a scripting language with object-oriented features.

An alias, to put it simply, is a shortcut for executing commands or a series of commands. For example, in Linux, alias cd3='cd ../../..' allows you to move up three directory levels with cd3. What would normally take 11 keystrokes now takes only 3.

Without aliases, which save me hundreds of strokes per day, my keyboard would have worn out at least a year earlier. So if there’s one skill I’d like to learn in any system, it’s setting up aliases. Let’s get into how you can create your aliases!

Topics Covered

  • Adding aliases to Windows PowerShell
  • Adding aliases to Linux systems (bash shell)
  • Sourcing a configuration file in Linux
  • Understanding the order of configuration file detection when a Linux terminal session starts
  • Creating a new $PROFILE file in Windows PowerShell
  • Adding functions to the $PROFILE file in Windows PowerShell
  • Listing current aliases
  • Organizing and managing numerous aliases

Photo generated by the author with OpenAI’s DALL-E

Photo generated by the author with OpenAI’s DALL-E

Linux system (bash scripts)

Creating an alias in a Linux system using the bash shell is a straightforward process. Here’s how you can do it.

Steps

  1. Open the ~/.bashrc configuration file.
  2. Insert a new line following this format
alias my_alias_name='what I want my_alias_name to replace; use semicolon to separate multiple lines of commands'

For example, alias ll='ls -la' create a shortcut for listing files in detail.

  1. Save the configuration file.

  2. Apply the change. Make the Linux recognize the change you’ve just made by sourcing the configuration file (source ~/.bashrc or . ~/.bashrc). Alternatively, restart your terminal session by closing that terminal window and then reopening it.

Don’t add extra whitespaces

Bash scripts are sensitive to whitespace. In other programming languages, adding spaces around the equal symbol is a good practice. However, in bash scripts, this habit can lead to errors. It’s important to remember not to insert any whitespaces around the equal sign when creating an alias.

Which configuration file?

New Linux users might get overwhelmed when found a whole bunch of configuration files under their home directory.

.bash_history
.bash_logout
.bashrc
.bash_your_user_name
.bash_profile

But don’t panic, their relationship is simple. If you are new to Linux, just try to find or create a ~/.bashrc for yourself to put all settings in.

  1. ~/.bashrc: This is where most people put their aliases, which are sourced from ~/.bash_profile.

  2. `~/.bash_profile: Bash checks for this file first. If it exists, Bash reads and executes commands from it. Then, it doesn't look for ~/.bash_login or ~/.profile. In this file, you usually can find

  3. ~/.bash_login: If ~/.bash_profile does not exist, Bash looks for ~/.bash_login and reads and executes commands from it if it exists.

  4. ~/.profile: If neither ~/.bash_profile nor ~/.bash_login exist, Bash looks for ~/.profile, reads, and executes commands from it if it exists.

Don’t miss the final step

If you try the alias and the alias doesn’t work out. There’s a large chance that your change is not sourced correctly. In plain words, you saved your new alias to the configurationfile, but the system is not aware of the change. You need to ask the system to execute the configuration file, this is when the source command is involved. When you source ~/.bashrc, all the content in that configuration file is executed.

You may find other people edit their configuration file like this

vim ~/.bashrc
. !$

where . is short for source, and !$ is short for repeating the last part of the previous command. In other words, it’s a short way to write source ~/.bashrc.

It’s also possible that your new alias work well after source the configuration file, but a restart of terminal make it disappear. Then the problem would be largely depend on which configuration file you put your aliases in. And you might want to study the topic of previous section more carefully and try to make the new terminal session source your configuration file correctly.

Windows PowerShell

Adding aliases in Windows PowerShell includes four main steps:

  1. open the configuration file (create a new one if there was none)
  2. add a function to do what you want the alias to do
  3. use your alias to trigger the function.
  4. source your configuration file

Configuration file

The configuration file would be located in the $PROFILE environment variable. You can find the content of this profile by either print it in terminal Get-Content $PROFILE or open with some editor notepad $PROFILE .

If there is no profile file, an error may be raised.

Get-Content $PROFILE
Get-Content : Cannot find path 'C:\Users\lang\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1' because it does not exist.
At line:1 char:1
+ Get-Content $PROFILE
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\lang\...ell_profile.ps1:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Then you need to create a PowerShell profile first, before adding any content.

New-Item -Path $PROFILE -ItemType File -Force

With a sharp eye, you may notice in the previous error information that this new profile file has the path of


C:\Users\<your_user_name>\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Add a function

Make a function, which contains everything you want to run with that alias. The reason we need a further step in adding aliases to Windows PowerShell than Linux system is due to the limitation in Set-Alias command, which only accepts a single word, in other words, no white space is included in the part that the alias would replace.

For example, I want to connect to a server,

function Connect-MySSH { ssh my_user@my_server }

or a more general condition,

function your_function_name { 
  everything you want alias to do 
  even more than one line
}

Add the alias

Add the alias to trigger the function you added

Set-Alias -Name SD -Value Connect-MySSH

In a more general template

Set-Alias -Name alias_name -Value your_function_name

Source the configuration file

Different from the bash case, there’s no source command in PowerShell. But the dot command is still working.

. $PROFILE

will make the shell recognize of the new alias. Alternatively, you can always restart the PowerShell window to make the newly created alias work.

More to consider

Wise choice of your alias

Hold a second before going ahead and adding a bunch of aliases to your system. Make sure your alias has a well-chosen name, that it is either unused in any other applications or you are sure that you want to rename that alias to another feature.

It’s not wise to set cd to always go up two levels.

alias cd='cd ../..'

After the alias setting up, even a cd Document command would take you up two levels, instead of going into that Document directory.

But a renamed ls is quite normal and useful.

ls -a --color=auto

will list a more complete list of files, and add some colors to enhance the readability.

Organization your aliases

With more and more aliases set up in your system, you may forget some of them. Make sure you add comments to them. And the alias command would help you show a list of all your aliases in the current environment.

Be sure to review them now and then, and make sure the unused ones are eliminated, in case they conflict with some other commands someday. If you set the same alias name to different content by accident, the last alias definition read by the shell will take precedence.

With the dictionary of your aliases growing, you should pay more attention to their organization. You can put the alias list in a single file, like ~/.bash_aliases, or even a bunch of files to categorize them. Just don’t remember to source them correctly. Like add this part to your ~/.bash_profile.

if [ -f ~/my_custom_config.sh ]; then
    source ~/my_custom_config.sh
fi

Footnotes

[1] I can’t use ssh command in my Windows Linux subsystem after I moved to Windows 11. I heard about people who had this problem also using a VPN.

ssh: Could not resolve hostname my.poor.server: Temporary failure in name resolution

I’d thank this error which gave a chance to get familiar with Windows PowerShell. On the other hand, I’ll spend more time later try to fix this failure of resolve hostname problem.

[] Note: This article was polished by ChatGPT*


메타데이터
post_id
90f2bee652fd
slug
shortcut-to-efficiency-crafting-aliases-in-powershell-and-linux-for-beginners-90f2bee652fd
url
https://medium.com/@mytiros/shortcut-to-efficiency-crafting-aliases-in-powershell-and-linux-for-beginners-90f2bee652fd
canonical_url
https://medium.com/@mytiros/shortcut-to-efficiency-crafting-aliases-in-powershell-and-linux-for-beginners-90f2bee652fd
author_url
https://medium.com/@mytiros
status
ok
fetched_at
2026-07-24 07:41:03