Set alias command in powershell to make your life easier

Frankie
2 min readDec 2, 2020

--

Photo by Ilya Pavlov on Unsplash

When we are using VS Code for development, it also comes with powershell as the default terminal.

Sometimes we have to run commands in the terminal, and the commands maybe long and easily make typo, for example:

kubectl

kudectl

You have to spend some amount of time to figure it out.

Also, setting alias command can save your time from typing a long command to just a few characters.

Create permanent PowerShell Aliases

First, open VS Code and type below command on the terminal.

Get-Variable profile | Format-List

Value shows the path of your powershell profile. If you open fire explorer and follow the path, you may find out that the folder is not exist. If that is the case, we need to create the folder WindowsPowerShell and the file Microsoft.PowerShell_profile.ps1 .

After that, it’s time for us to set some alias commands, use editor (e.g. notepad) to open Microsoft.PowerShell_profile.ps1

Create new alias command by inputting Set-Alias <name> <value>.

For example: Set-Alias k kubectl.

Then save the file and reopen VS Code. Input k on the terminal.

It is equal to the command kubectl.

However, Set-Alias are just that — aliases for command names — not command names plus arguments. For example, we cannot set alias like this:

kubectl describe pod [pod name]

Set-Alias kdp kubectl describe pod

In this case, we need to use function :

function <alias name>{
<command plus arguments> $args
}

open Microsoft.PowerShell_profile.ps1 and add:

Set-Alias k kubectlfunction kdp {
k describe pod $args
}

Save and reopen VS Code.

Now, when we input kdp posts it is equivalence to

kubectl describe pod posts

You should always make your own list of alias commands to save time and get rid of typo.

--

--

Frankie
Frankie

Written by Frankie

Hi, I am interested in IT field including front-end, back-end, database, networking.

No responses yet