The aliases of PowerShell cmdlets

I. Introduction

In this chapter, we'll learn how to list PowerShell aliases and create a custom alias. What are aliases, anyway? PowerShell supports aliases, i.e. alternative names for existing commands.

In other words, a PowerShell cmdlet can be called from several different names, and each alternative name corresponds to an alias. Thanks to an alias, we can shorten the name of certain cmdlets, which is useful when entering commands directly in the console. For example, the "Get-ChildItem" cmdlet, which lists the contents of a directory, can be shortened to "gci", or even "ls" and "dir", as it has several aliases.

PowerShell aliases

II. Listing PowerShell aliases

To obtain the list of PowerShell aliases present on your machine, execute the following command:

Get-Alias

In PowerShell 7, there are 140 native command aliases. Some aliases refer to MS-DOS commands, while others refer to Linux commands. The aim is to facilitate the use of the command line on Linux and Windows by creating a "bridge" between command names.

List of PowerShell aliases

For example, the "wget" command, used to download files under Linux, corresponds to a PowerShell alias of the "Invoke-WebRequest" cmdlet, which has the same function. By default, this alias is only available with Windows PowerShell.

We can check this because Get-Alias can display the aliases of a specific cmdlet:

Get-Alias -Definition Invoke-WebRequest

We can see that the Invoke-WebRequest cmdlet has three aliases: wget, iwr, curl. Using one of these four names, taking into account the cmdlet's original name, will produce the same result.

Conversely, we can see which cmdlet an alias refers to:

Get-Alias -Name wget

The information checks out:

In console mode, the most commonly used alias is probably "cd", which refers to the PowerShell cmdlet "Set-Location". It lets you move around the folder tree on the local machine, and this command is also compatible on Linux.

III. Creating an alias

PowerShell supports several cmdlets for manipulating command aliases, including "New-Alias" for creating an alias,"Set-Alias" for modifying an existing alias, and "Import-Alias" and "Export-Alias" for importing a list of aliases and exporting aliases to a file respectively.

To declare a new alias, proceed as follows:

New-Alias <nom de l'alias> <nom du cmdlet>

For example, we could create the aliases "restart" and "reboot" for the "Restart-Computer" cmdlet, which is used to restart the computer. Which gives:

New-Alias restart Restart-Computer
New-Alias reboot Restart-Computer

We can then check for the presence of these two aliases:

Create a PowerShell alias with New-Alias

Please note that these aliases will only be valid within the current PowerShell session. As soon as the PowerShell console is closed, these aliases will be lost, and you'll have to recreate them each time.

To create a persistent alias on a local machine, you need to integrate it into your PowerShell profile. In the next chapter, we'll look at what a PowerShell profile is and how it can be customized. This will be an opportunity to see how to integrate an alias into this profile.

IV. Conclusion

Although aliases are very useful, especially as they save time when you're working in a console, it's advisable not to use them in scripts. The use of aliases can be a source of errors and misunderstanding: think of the person who will be re-reading your code.