Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create dynamic alias for Powershell
$aliasFilePath = "<Alias file path>"
function New-CommandAlias {
$functionFormat = "function $commandName { & $command $args }
New-Alias -Name $commandAlias -Value $commandName -Force -Option AllScope"
$newLine = [Environment]::NewLine
Add-Content -Path $aliasFilePath -Value "$newLine$functionFormat"
Write-Host "Successfully created new alias"
. $aliasFilePath
Copy link

rahulpnath commented Dec 6, 2019

Create dynamic alias commands to update This allows to add more commands to the file dynamically from PowerShell itself

Sample Usage:

New-CommandAlias -CommandName "Get-GitStatus" -Command "git status -sb" -CommandAlias "s"
New-CommandAlias -CommandName "Move-ToWorkFolder" -Command "cd C:\Work\" -CommandAlias "mwf"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment