Powershell Customization

PARAB
2 min readSep 4, 2023

--

Hey Guys !!

In this article, I will show you how to customize your PowerShell terminal so that its prompt will show git branch along with other important things in different colors.

After this change, your prompt will look like this.

First you have to make config file. For that run the below command

New-Item -path $profile -type file –force
  • It will create a profile config file in below directly
  • this file is named as — Microsoft.PowerShell_profile`

C:\Users\USER_NAME\Documents\WindowsPowerShell

Then add following code snipped

function Write-BranchName () {
try {
$branch = git rev-parse --abbrev-ref HEAD

if ($branch -eq "HEAD") {
# we're probably in detached HEAD state, so print the SHA
$branch = git rev-parse --short HEAD
Write-Host " ($branch)" -ForegroundColor "red" -NoNewline
}
else {
# we're on an actual branch, so print it
Write-Host " ($branch)" -ForegroundColor "blue" -NoNewline
}
} catch {
# we'll end up here if we're in a newly initiated git repo
Write-Host " (no branches yet)" -ForegroundColor "yellow" -NoNewline
}
}

function prompt {
$base = "PS "
$path = "$($executionContext.SessionState.Path.CurrentLocation)"
$userPrompt = "$('>' * ($nestedPromptLevel + 1)) "

Write-Host "`n$base" -NoNewline

if (Test-Path .git) {
Write-Host $path -NoNewline -ForegroundColor "green"
Write-BranchName
}
else {
# we're not in a repo so don't bother displaying branch name/sha
Write-Host $path -ForegroundColor "green" -NoNewline
}

return $userPrompt
}

Now your prompt will look like with some colors, I hope you will like it

PS C:\PATH (GIT_BRANCH)>

NOTE: I have taken reference from an answer on stackoverflow https://stackoverflow.com/a/44411205

--

--

PARAB
PARAB

Written by PARAB

Cybersecurity Enthusiast || AWS || GCP || Full Stack Developer || Docker

Responses (1)