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.
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.
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.
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.
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.
The above command will give the output of the list of directories only in the current location.
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.
If we execute the get-childitem command in the current location, we will be able to see the profile PowerShell Scirpt.
Now we need to edit the file using the PowerShell ise. We use the below command to edit the file in PowerShell ISE.
In the ISE we enter the script and save the file.
We close and reopen the shell and enter g to see that the alias is working.
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.
- Article Rating
- Content Relevance
- Content Clarity
- Author Rating