Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

PowerShell Alias

Introduction to PowerShell Alias

Alias in Powershell allows us to define short names for commands so that it will be easy for us to remember and write it, we can also create Alias for any executable files.

When we write any command and use the Set-Alias along with the name which we wanted to use for shorter names for the commands than in memory it will hold the command full details inside the memory with the Alias name. At any time if we wanted to use the defined shorter name for the alias we can call it with Get-Alias. We can use Set-Alias and Get-Alias commands for setting and getting the actual command with Alias. We also have an available command for removing and any existing Alias. We can overwrite the existing Alias if we wanted to do any modification into the existing Alias.

How to Create PowerShell Alias?

To create an alias we need to use the Set-Alias command. This command allows us to set the alias name for the given function. Suppose we wanted to see the all list of available packages than we can use the command Get-Package but here we will create an alias for Get-Package. See the example below, here in this example we are setting an alias name for Get-Package with “pkg” name and in the next line, we are getting details of it by Get-Alias with “pkg” name.

We can use below syntax for setting an alias in powershell.

Syntax:

Set-Alias
[-Name]
[-Value]
[-Description ] [-Option ] [-PassThru

] [-Scope ] [-Force

Below is the example to set an alias in PowerShell.

Example:

Set-Alias -Name pkg -Value Get-Package
Get-Alias pkg

In the above example, we have set an Alias to the command Get-package, and with the help of Get-Alias on the defined alias name, we are able to get the alias details.

How to Access and Modify PowerShell Alias?

Here we discuss how to access and modify PowerShell alias.

Accessing Alias in PowerShell

To access Alias in PowerShell we can use a command called Get-Alias, it will allow us to get the name set by a command called Set-Alias. Below is an example to get the alias name. To access any defined Alias in PowerShell we can use below format,

Syntax:

Get-Alias
[[-Name] ] [-Exclude ] [-Scope ]

Example:

Set-Alias -Name TP -Value ./ranjan/ranjan1
Get-Alias TP

Output:

Modify Alias

In PowerShell, if we wanted to modify any alias then we can overwrite it again with another value and it will be modified, which will change the older name of the aliasIn general, there is not any available command for modification we need to override the existing alias value with the new one.

Setting an alias and getting it.

Example:

Set-Alias -Name TPF -Value ./ranjan/ranjan1
Get-Alias TPF

Output:

Modifying existing alias with a new value.

Example:

Set-Alias -Name TPF -Value ./ranjan/ranjan2
Get-Alias TPF

Output:

In the above example first, we set the value for the Alias “TPF” as the path “/ranjan/ranjan1” and in the next line we change it to “ranjan/ranjan2” by rewriting the same command with different values.

How to Remove PowerShell Alias?

We can remove any Alias with the PowerShell command called Remove-Alias. This command removes the Alias with the name which we passed for it. See the below format to remove Alias in PowerShell.

Syntax:

Remove-Alias
[-Name]
[-Scope ] [-Force

Set and get the alias,

Example:

Set-Alias -Name TPF -Value ./ranjan/ranjan1
Get-Alias TPF

Output:

Remove the alias defined above,

Example:

Remove-Alias -Name TPF //removing the the Alias “TPF”
Get-Alias TPF//will through an error as we have removed the Alias “TPF”

Output:

On running the command get-Alias it will be through an error as we have already removed the Alias “TPF” and we are trying to fetch its value.

Importance

Let’s understand one thing when we will be writing any big script at that time our code goes very long and maintenance is very hard in that case. Also, the readability of code will not be very good. For example, if we wanted to write some command related to all running processes than in PowerShell we will use “Get-process” but with the help of Alias concept in PowerShell, we can write the same command in more readable like “process-list” or “PL”.by writing “PL” instead of writing “Get-process” we are able to save few characters. Here the length of “Get-process” is 11 and the length of “PL” is two so we are able to save 9 letters here.

In general, we can define a few points for its importance they are given below.

  • Shorter the Code: Because it is in our hands what name we wanted to give to the PowerShell command for an alias, so we can give smaller names to them, which can make our code less than half of the code without an alias.
  • More Readable the Code: Many commands do not make very meaningful names according to the work they perform, so in such a situation we can define more readable Alias name for commands so that others and we also can easily be able to understand the code.
  • Easier to Maintenance: Because code is more readable and understandable, other people who are going to some work on an existing script they can easily understand each line and their meaning

Recommended Articles

This is a guide to PowerShell Alias. Here we discuss the Introduction and how to create PowerShell alias and how to access and modify PowerShell alias with examples. You may also look at the following articles to learn more –

  1. Introduction to PowerShell Import Module
  2. Examples to Implement PowerShell Get-Date
  3. Shortcuts for Get Help in PowerShell
  4. Explanation on cmdlets in PowerShell

The post PowerShell Alias appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

PowerShell Alias

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×