How to get beautiful PowerShell with Git Branches

How to get beautiful PowerShell with Git Branches

No more boring PowerShell instead use Windows Terminal(WT), posh-git and oh-my-posh

Introduction

  • Why should you customize PowerShell?

As developers we tend to use Git, to be more specific Git CLI. Natively PowerShell doesn't show much information e.g. which branch we are working on. Using some modules and a terminal we can get different themes for our PowerShell.

  • What is Windows Terminal (WT)

Windows Terminal is a terminal application with shells like Command Prompt, PowerShell, and WSL all in one application. Its main features include multiple tabs, panes, custom themes, styles, and configurations.

Tools / Module Used

  • Windows Terminal

  • oh-my-posh (for PowerShell theme)

  • posh-git (for git autocompletion, branch name,...)

  • Powerline Fonts (Nerd Font Recommended or Cascadia PL)

  • git-bash (assumed to be already installed)

Steps at a glance

  1. Install Windows Terminal

  2. Install Powerline Fonts

  3. Install Required Modules

  4. Add modules and desired theme to profile

  5. Configure PowerShell to run scripts

  6. Add font family in JSON file of terminal

Detailed Steps

Step 1: Install Windows Terminal

  • Go to Microsoft Store and install Windows Terminal.

    wt.png

  • After installing it will appear in the start menu as well.

TIP: Windows Terminal can be opened from a run using wt like cmd

Step 2: Install Fonts

  • Any Nerd Font will work with the standard themes of Oh My Posh, but recommended is MesloLGM Font.

  • Get MesloLGM Fonts here

Cascadia Font is also compatible but some glyphs are not supported yet you need to patch with custom Unicode manually.

  • Install MesloLGM Windows Compatible fonts

fonts.png

NOTE: Install font for all users by right-clicking and install for all users

Step 3: Install Modules

  • Using PowerShell, install oh-my-posh and posh-git.

You may install these using Terminal itself

Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser
  • You need to press A in PowerShell while installation of both modules to install from the repository. See the image below 👇

    image.png

Step 4: Configure PowerShell Profile

  • Enter the below-shown command in PowerShell.
notepad $PROFILE
  • If there is no file profile file, then a dialog box will be prompted to create one, hit yes.

  • Now inside PowerShell_profile.ps1 file which is currently opened, add the following lines👇

Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt -Theme paradox
  • Save the file and close the notepad.

You can change the theme later on as you want, the cover photo is using aliens theme.

Changing themes will be covered in another blog.🙂

Step 5: Configure PowerShell to run scripts

  • We need to change the execution policy so that we can run our profile script(the one created in the above step).

  • To change execution policy open new PowerShell window as administrator.

    image.png

There will be some errors when you open a new PowerShell window, that is due to restricted execution policy but after executing the below step it won't appear next time.

  • Enter the below-shown command(to change policy to unrestricted)
Set-ExecutionPolicy Unrestricted

Step 6: Add Font Family in JSON file of Terminal

  • Open Settings of Terminal. From the Dropdown menu adjacent to the + button.

  • Or Use ctrl + , as a shortcut to open settings of terminal.

  • Now click on the bottom left button labeled as Open JSON File.

  • On clicking the button JSON file would be opened in the desired editor, if VS Code is available then it would be opened in that else another editor would be chosen.

  • Now add Font Family by Adding the below code in the default section of the JSON file.👇

"fontFace": "MesloLGM NF"
  • Note: You must add the above code in the default section only. Don't alter the other code written, we will discuss customizing WT using JSON later in detail, in another blog.

    image.png

Congrats🎉, You have customized the Terminal.

Now change to a git initialized directory / any cloned repository, you will be seeing an extra-label indicating on what branch you are working on.

image.png

Extra Themes

  • To check all available themes head over to Oh-My-Posh | Themes

  • You may check-in PowerShell as well using :

Get-PoshThemes
  • For changing theme open the profile file using notepad $profile and change the theme by changing the old theme to the new one.

You can also try theme by temporarily changing theme in PowerShell Using Set-PoshPrompt -Theme <themename> for e.g. Set-PoshPrompt -Theme mt

React 🤟, Comment and Share

Follow Me, More cool stuff on its way.