I am a developer with a background in .NET. I work mostly on Windows (sometimes I work on MAC but not often). I have used MAC as the main development environment for the last six months and honestly, I like working on MAC much more than Windows and one of the reasons is that MACs terminal is really powerful and it is even cooler when you use ZSH (I will talk about this in another post).

Now I’m using Windows to write this post, just because I’m working on a project that uses .NET technology. (Unfortunately, I don’t use Visual Studio for MAC at all.)

Problem

When using a MAC I have some alias (or functions) to speed my development process.

For example, when using GIT I usually update master branch then merge code from the master branch back to a feature branch. In MAC I only type 1 command:

gup master && git merge master
# help me update master branch then switch back
 

In the code block below the gup command is a function that was added in ~/.bash_profile and it looks like below:


function gup () {
  c=$(git rev-parse --abbrev-ref HEAD)
  git checkout "$1"
  git pull
  git checkout "$c"
}

When switching back to Windows development I felt uncomfortable like missing my pocket knife.

Solutions

Fortunately, PowerShell has a feature like ~/.bash_profile which is called “PowerShell Profile”. You can read more about “PowerShell Profile” here.

The PowerShell profile basically is a .ps1 file which will be loaded every time you open a PowerShell engine. There are serval profile files and based on your needs you can choose what profile file you want to add to your utility scripts.

DescriptionPath
All Users, All Hosts $PsHome\Profile.ps1
All Users, Current Host $PsHome\Microsoft.PowerShell_profile.ps1
Current User, All Hosts $Home\[My ]Documents\PowerShell\Profile.ps1
Current user, Current Host $Home\[My ]Documents\PowerShell

Host is the interface that the Windows PowerShell engine uses to communicate with the user. For example, the host specifies how prompts are handled between Windows PowerShell and the use.

more

For example, PowerShell.exe in a host and PowerShell in the Visual Studio Code (VSCode) is a different one.

If you encounter an issue that you cannot access some functions in VSCode while you can access it in the PowerShell.exe, you might add the functions in PowerShell.exe host.

Bonus

Below are some utility functions that I’m using:


# GIT: publish local branch to remote
function gpo() {
    git push origin --set-upstream $(git rev-parse --abbrev-ref HEAD)
}

# GIT: update a branch then switch back to the current branch
function gup () {

    $c=$(git rev-parse --abbrev-ref HEAD)
  
    git checkout "$1"
  
    git pull
  
    git checkout "$c"
}
  
# GIT: quick command to help your code and push to remote
function gsave() {
    git add .

    $msg=$args[0]
    
    if ([string]::IsNullOrEmpty($msg)) {
        $msg = "save code"
    }
  
    git commit -m "$msg"
  
    gpo
}

# GIT: git fetch from origin with prune flag
function gfo() {
    git fetch origin --prune
}

# GIT: Alias for git commit -m
function gcom() {
    git commit -m $args[0]
}

# GIT: Create directory and cd into it
function mkd {
    New-Item -Path $args[0] -ItemType Directory -Force -Out
    Set-Location $args[0]   
}

Conclusion

I’m a lazy developer so I need to reduce some repeated task and PowerShell profile is one of the most useful tools.

If you have used PowerShell profile or PowerShell script file to speed up your development process, please share your utility functions in the comment below.

P.s: the banner of this post I borrowed from https://medium.com/@jsrice7391/using-vsts-for-your-companys-private-powershell-library-e333b15d58c8 😉

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.