Wednesday, September 24, 2014

#27 : Changing Powershell Prompt

When you open Powershell, you get a prompt like - PS C:\Users\Administrator>. The prompt is very good, but after few days of working you feel to have a change. But the good old command 'prompt' does not work in Powershell.

For those who don't know how to change prompt in Command Prompt, you can try below command Prompt -
prompt $$
-or-
prompt $p$g

The same command does not work in Powershell. I tried it very early days and skipped when it did not work. So, the question is - How to change Prompt in Powershell ?

You need to create function named prompt to change the prompt easily. Follow the below steps -
1. Open Command-Prompt of Powershell.
2. Run the below -
function prompt { "Posh >" ; return " " }

The above command you need to run every time you run Powershell. So, this will be a cumbersome approach. So, let's try another approach - Change your profile which is a Powershell script that runs every time.
1. Find the location of $profile using below command -
echo $profile
2. You might get a path like -
C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
3. This path does not exists usually. You can goto location Documents and create a folder named WindwosPowershell and file mentioned above. Create it and move to next step.
4. Run the command -
notepad $profile
5. Copy and paste below script in the notepad and save it -


  1. function prompt
  2. {
  3. ${GLOBAL:CNT_OF_PRMT}++
  4. Write-Host ( "Posh (" + $("000000000${GLOBAL:CNT_OF_PRMT} > ").tostring().substring($("000000000${GLOBAL:CNT_OF_PRMT}").length-4 , 4) + ") > ") -foregroundcolor white -nonewline
  5. return " "
  6. }
You might see a command prompt with a line number -
Posh (0064) >
Posh (0065) >

Every time we press enter a value is increased. You can change the foregroundcolor of your choice.
Isn't this glamorous! I like it!

No comments:

Post a Comment

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...