ADDING COMMAND ALIASES TO POWERSHELL

RMAG news

HOW MUCH TIME COULD YOU SAVE BY SHORTENING COMMON COMMANDS AND PARAMETERS USING POWERSHELL ALIASES? THE ANSWER IS A LOT.
If you’re like me, there are certain commands that get run repeatedly throughout your day. Between “git checkout”, “docker {whatever}” and navigating to frequent paths with “cd”, I’ve been wondering how much time I could save by shortening these commands and parameters.

THIS SEAT’S TAKEN

PowerShell has built in commands that we don’t want to step on. I won’t list them all (there are MANY), but if you get something wildly unexpected from your aliases then you can Google them.

SO SHOW ME ALREADY

Okay, okay. So first, let’s create a function in a “.ps1” file.

function goGoGadgetGitStatus {
git status
}

The function above simply calls “git status”. You can then setup an alias to call that function.

Set-Alias gs goGoGadgetGitStatus

Then from your PowerShell console you can type “gs” and it will run “git status”.

That’s a very simplistic example but gives you an idea of what’s possible.

WHAT ABOUT PARAMETERS

Great question! We can define parameters in the function and then pass them in.

function goGoGadgetGit {
Param(
[Parameter(Mandatory = $true, Position = 0)]
[String]
$Cmd,

[Parameter(Mandatory = $false, ValueFromRemainingArguments = $true)]
[String[]]
$Params
)

Switch ($Cmd)
{
#status
‘s’ { git status $Params }
#init
‘i’ {git init $Params }
#add
‘a’ {git add $Params }
#branch
‘b’ {git branch $Params }
#checkout
‘ch’ {git checkout $Params }
#clean
‘cl’ {git clean $Params }
#clone
‘cln’ {git clone $Params }
#commit
‘cm’ {git commit $Params }
#config
‘cf’ {git config $Params }
#log
‘l’ {git log $Params }
#merge
‘m’ {git merge $Params }
#pull
‘pl’ {git pull $Params }
#push
‘pu’ {git push $Params }
#remote
‘re’ {git remote $Params }
}
}
Set-Alias g goGoGadgetGit

Now in our console we can call “g s” to get “git status”, “g i” to execute “git init”, “g a” to get “git add”, “g b” to get “git branch”, “g ch” to get “git checkout”, “g cl” to get “git clean”, “g cln” to get “git clone”, “g cm” to get “git commit”, “g cf” to get “git config”, “g l” to get “git log”, “g m” to get “git merge”, “g pl” to get “git pull”, “g pu” to get “git push”, “g re” to get “git remote”. Also according to you, you can change the all of this shortcut.
And I’m not going to go into detail on how to write PowerShell functions or options for parameters. There are plenty of resources out there for the two. But hopefully this showed you a rudimentary way to set up command aliases for PowerShell. Just add the code to your PowerShell profile.ps1 and you’re off to the races.

POWERSHELL CONFIG

I’ve actually created a repository at https://github.com/dalalsoham/My_Powershell that has the PowerShell script that I include in my profile that gives many aliases for things like Git and also this is my PowerShell configuration. Feel free to fork & clone it and use this config in your system.

If you have any other suggestions or any corrections Leave a comment below. :)❤️

Leave a Reply

Your email address will not be published. Required fields are marked *