Skip to content

Instantly share code, notes, and snippets.

@rahulpnath
Last active December 6, 2019 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulpnath/0007a3bdd7da2efc7024bd92c002cf17 to your computer and use it in GitHub Desktop.
Save rahulpnath/0007a3bdd7da2efc7024bd92c002cf17 to your computer and use it in GitHub Desktop.
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
Copy link
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