Home » PowerShell » PowerShell Alias Function

PowerShell Alias Function

PowerShell alias features provide an easy way to use an alternate name or nickname for a function, command, script, or file. Alias attribute on Function creates an alternate name for the function.

Using the [Alias (“Alias name”)] attribute, you can create an Alias for function in PowerShell.

In this article, we will discuss how to use the Alias attribute to create a function alias in PowerShell

Alias for Function in PowerShell

To create a shortcut name or set alias for function, use the Alias attribute and provide the alias name to it.

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

function Read-File
{
    [CmdletBinding()]
    [Alias("RF")]

    PARAM(
        [Parameter (Mandatory=$true)]
        [String]$FilePath
    )
    
    Write-Output "Reading $FilePath file"
}

In the above PowerShell script, we have created a Read-File function that read the file. In the function body, it uses the [Alias (“RF”)] attribute to set an alias for function in PowerShell.

If you want to execute the above function to read the file, use the following command

RF -FilePath "D:\PS\Alias.txt"

In the above PowerShell script, we use RF as an alias name for the function Read-File and pass the mandatory parameter FilePath. The function prints the message to the terminal as below

Reading D:\PS\Alias.txt file

Conclusion

I hope the above article on how to set PowerShell Alias for the function is helpful to you.

Using the Alias attribute, you can easily set an alias for a function. It’s very easy to use and execute the function by its alias name.

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

New-Alias

Get-Alias

Set-Alias

Remove-Alias

Export-Alias

PowerShell Set Alias Permanent

PowerShell Parameter Alias

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