Shorten PowerShell Prompt

If you open power-shell it will by default show you the full directory path on the prompt and sometimes it take up a whole lot of screen real estate. Following picture shows when I use powershell for git commands. Notice the long directory path in prompt before the command:

I find it very annoying when I am deep inside directory structure and in this post I will share with you how you can shorten it easily.

Step-1: Powershell profile

You have to customize the prompt function in your PowerShell profile (%userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1); it may be blank or even not exist if you have never modified it before.

Open PowerShell and type >> $profile and hit enter

this will show you the path for your powershell profile. In my case this path and file was not present on my computer. So I create the folder and an empty powershell file as follows:

If you already have this file present on your machine, you can skip this profile creation step.

Step-2: Modify the Powershell profile

Add the following to your powwershell profile:

function prompt {
  $p = Split-Path -leaf -path (Get-Location)
  "$p> "
}

Save the profile and Restart Powershell.

Optional

If you get a message that says you are not allowed to run scripts, then you need to copy/paste this line in PowerShell:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

and restart.

Result

Once you have done above mentioned steps. Your powershell promopt will be signficantly shorten (in this case only shows the current directory in the prompt).

References


Discover more from Hex Quote

Subscribe to get the latest posts sent to your email.

Discover more from Hex Quote

Subscribe now to keep reading and get access to the full archive.

Continue reading