Home » PowerShell » PowerShell Parameter Alias

PowerShell Parameter Alias

PowerShell alias provides an alternate name or shortcut name for the parameter instead of using the full name of the parameter or argument.

Using the Alias(), you can use the PowerShell alias with arguments. For example, Auth alias for argument Authentication.

In this article, we will discuss how to use Alias() to set PowerShell alias for arguments in the function. You can set an alias for multiple parameters in the function.

PowerShell Parameter Alias

To create PowerShell alias for parameters, use an alias() in the function. Alias name provides an easy way to remember the parameter name instead of writing their full name.

Let’s understand creating PowerShell parameter alias in function with an example, use the following code.

# Create a function to Read file
# Use Alias attribute to set alias for function

function Connect-SQL
{
    [CmdletBinding()]
    [Alias("CSQL")]

    PARAM(
        [Parameter (Mandatory=$true)]
       
        [Alias("Type")]
        [String]$ServerType,

        [Alias("Server")]
        [String]$ServerName,

        [Alias("Auth")]
        [String]$Authentication

    )
    
    Write-Output "Connecting to SQL with $ServerType,$ServerName and $Authentication mode"
}

In the above PowerShell script, we have created a function as Connect-SQL which has a function alias name CSQL. In the PARAM, the function has multiple parameters with their alias name specified in Alias().

To execute the above function, you can use function alias CSQL and use alias name while passing multiple arguments.

CSQL -Type "Database Engine" -Server "INCORP-EU-117" -Auth "Windows Authentication"

In the above PowerShell script, we have used PowerShell parameter alias Type, Server, and Auth instead of their full name as ServerType, ServerName, and Authentication.

The output of the above PowerShell script writes the output with the input provided to the function.

Connecting to SQL with Database Engine,INCORP-EU-117 and Windows Authentication mode

Conclusion

I hope the above article on how to create a PowerShell parameter alias in the function is helpful to you.

Use Alias() to create PowerShell alias for arguments and use it while calling the function name.

You can refer to the related articles on Alias in PowerShell.

New-Alias

Get-Alias

Set-Alias

Remove-Alias

PowerShell Set Alias Permanent

PowerShell Alias Function

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment