Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create dynamic alias for Powershell
$aliasFilePath = "<Alias file path>"
function New-CommandAlias {
param(
[parameter(Mandatory=$true)]$CommandName,
[parameter(Mandatory=$true)]$Command,
[parameter(Mandatory=$true)]$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
@rahulpnath

This comment has been minimized.

Copy link
Owner Author

rahulpnath commented Dec 6, 2019

Create dynamic alias commands to update https://gist.github.com/rahulpnath/8a6413dadf8759ffbc9778d018ab2039. 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
You can’t perform that action at this time.