Set-Alias cmdlet in PowerShell is used to create or change the alias name for a PowerShell command in the current PowerShell session. Alias set by Set-Alias cmdlet is not permanent and is only available for the current PowerShell session.
A cmdlet in the PowerShell can have multiple alias name. Set-Alias cmdlet is used to change or reassign an existing alias name to another PowerShell cmdlet.
In this article, we will discuss how to set alias in PowerShell using the Set-Alias cmdlet, reassign an existing alias name to different cmdlet.
Set-Alias PowerShell
PowerShell Set-Alias
creates or change a PowerShell command alias. The Set-Alias cmdlet in PowerShell doesn’t set permanent alias for a cmdlet.
Syntax
Set-Alias
[-Name] <string>
[-Value] <string>
[-Description <string>]
[-Option <ScopedItemOptions>]
[-PassThru]
[-Scope <string>]
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Parameters
-Name: Specify the name of the new alias. It accepts only alphanumeric characters for an alias name.
-Description: Specify the description of the alias.
-Scope: Specify the scope for the new alias. It can take Global, Local, or Script value. Local is the default value.
-Value: Specify the cmdlet name that is being used for aliased.
How to Set Alias for a cmdlet in PowerShell
Use the Set-Alias
to create an alias name or to set an alias name for a cmdlet in PowerShell current session.
Set-Alias -Name C -Value Get-Command
In the above PowerShell script, the Set-Alias
uses the Name parameter to specify the alias name for the PowerShell command Get-Command.
Set-Alias cmdlet creates a PowerShell alias for a cmdlet in the current session.
To get the alias name for PowerShell cmdlet, use the Get-Alias command as below
Get-Alias -Name C
The output of the above PowerShell set alias command is:
Cool Tip: How to import alias in PowerShell!
How to Reassign an existing alias to a cmdlet using Set-Alias
Using the Set-Alias
cmdlet, you can reassign an existing alias to a different PowerShell cmdlet.
In the above example, we have assigned an alias name C to the Get-Command cmdlet. To reassign an existing alias C to a different command, use the following script.
PS C:\>Get-Alias -Name C CommandType Name Version Source ----------- ---- ------- ------ Alias C -> Get-Command PS C:\> Set-Alias -Name C -Value Get-Content PS C:\> Get-Alias -Name C CommandType Name Version Source ----------- ---- ------- ------ Alias C -> Get-Content
In the above PowerShell script, earlier we assigned an alias name C to the Get-Command cmdlet.
The Get-Alias
cmdlet uses the Name parameter to specify the alias name and retrieve the command name as Get-Command.
The Set-Alias
command uses the Name parameter to specify the alias name C to a different PowerShell command Get-Content. It reassigns an existing alias name to a different PowerShell cmdlet.
The Get-Alias command uses the Name parameter to specify the alias name C to find the alias for the name in the current PowerShell session.
Cool Tip: How to remove PowerShell alias!
Conclusion
I hope the above article on how to set an alias in PowerShell using the Set-Alias
cmdlet is helpful to you.
PowerShell set alias for command is available only for the current session or until you close the PowerShell. The Set-Alias cmdlet in PowerShell doesn’t set permanent alias for a cmdlet.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.