in ,

WindowsWindows Loving ItLoving It

[WORKING] PowerShell Alias persist across all Sessions – 2020

Use the set-alias cmdet to create an alias for cmdlet, functions and custom functions. Use $profile to make the alias persist across all Sessions.

PowerShell set alias

PowerShell has several cmdlets that are very useful but often too long to type every time.  We can write our own functions that have meaningful names but also a short form for us to quickly type it out whenever necessary.  Thankfully there are several aliases for the in-built cmdlets.  But what about the ones that we create and also the ones that do not have an alias.

Jump to:

People Also Seach for:

In this post, we are going to see how to create an alias use the set-alias cmdlet and also see how we can persist the alias across different sessions.  Let us start off with how to create an alias.

Create a PowerShell Alias:

We will use the set-alias cmdlet to create an Alias.  The following example will show us this.  We will use the g in the terminal and see what happens.

error without alias
error without alias

As expected we get an error.  But let us try to get the list of child items here.  We will have to create an alias for this.

Set-Alias -Name g -Value Get-ChildItem

The above command will help users create an alias for get-childitem.  Thus we can easily get the child items of the directory.  We can do it with just one character.

get child item using one character
get child item using one character

Another advantage of this is, it works just like the original get-childitem cmdlet.  The below script will show it by using the directory flag.

g -di

The above command will give the output of the list of directories only in the current location.

g with directory
g with directory

The only problem with using the set-alias is that it is lost after closing the current session.  We can make this persist across all the sessions of the current type using PowerShell profiles.

Persist Custom Alias across Different Sessions:

We need to get to the Current User Current Host profile to be able to persist a custom alias across the current type of session.  There are 6 different types of profiles and the details on them are for another day. Right now we are interested only in the Current User Current Host profile only.

We need to navigate to the location where it is stored using the below script.

cd $(split-path $profile)

If we execute the get-childitem command in the current location, we will be able to see the profile PowerShell Scirpt.

gci

Now we need to edit the file using the PowerShell ise.   We use the below command to edit the file in PowerShell ISE.

ise .\profile.ps1

In the ISE we enter the script and save the file.

new-Alias g Get-ChildItem

creating a profile for alias
creating a profile for alias

We close and reopen the shell and enter g to see that the alias is working.

using the profile
using the profile

Verdict:

The Set-Alias is used to create a custom alias for existing cmdlets and functions.  We can use this for creating an alias for custom functions also.

If you want more such awesome posts, stay tuned to TecKangaroo.

powershell alias
  • Article Rating
  • Content Relevance
  • Content Clarity
  • Author Rating
4.6

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading…

0

Comments

0 comments