Are you tired of having to re-create your aliases every time you open a new PowerShell terminal? Don’t worry, there is a solution! In this blog, we will explore how to create permanent aliases in PowerShell, even after reloading the terminal.
What are Aliases?
Aliases in PowerShell allow you to create shorter versions of commands or cmdlets. For example, instead of typing out Get-ChildItem
, you could create an alias gci
to represent the same command. This can save time and make your command line experience more efficient.
How to Create Permanent Aliases
To create a permanent alias, you will need to edit your PowerShell profile. The profile is a script that runs every time you start a new PowerShell terminal.
- To find your profile, run the following command:
$profile
. This will return the path to your profile script. - If the profile does not exist, you can create it by running the following command:
New-Item -ItemType File -Path $profile -Force
. - Open the profile script in your preferred text editor and add your alias definition. For example:
Set-Alias gci Get-ChildItem
. - Save the profile and close the text editor.
Now, every time you start a new PowerShell terminal, your alias will be available to use.
Something You May Not Know
Did you know that you can also create aliases for your own scripts and functions? Simply define your script or function and then create an alias for it in your profile. For example:
function Get-User {
# function code here
}
Set-Alias gu Get-User
Now you can use the gu
alias to run your Get-User
function.
Example
This function will connect to the itvraag.nl
server as the user
user
, and then run the sudo apt update; sudo apt upgrade -y
command sequence on the remote machine.
Function SshFun {
ssh user@itvraag.nl 'sudo apt update; sudo apt upgrade -y'
}
Set-Alias SshAliasName SshFun
Example 2
The following fix was provided by HillSonMX
watching my YouTube video about this topic (down in the comments):
#Alias and Alias with Parameters ubu update
Function ubuup {ssh -t -p 22 <USERNAME>@<SERVERIPADRRESS> 'sudo apt update; sudo apt upgrade -y'}
Set-Alias -Name ubu -Value ubuup
5 Tips for Increased Productivity
- Create aliases for long or frequently used commands to save time and typing.
- Use aliases for your own scripts and functions to make them easier to run.
- Consider creating an alias for common parameter combinations. For example, you could create an alias for
Get-ChildItem -Directory
asgcd
. - Use descriptive aliases to help you remember what each one does.
- Don’t be afraid to experiment and create multiple aliases for the same command. Find what works best for you.
Challenge
Now it’s your turn to test your newfound knowledge on creating permanent aliases in PowerShell. Try creating an alias for a command or function of your choice and see how it improves your productivity.