Windows powershell: command line change

Using Windows powershell, how to change the command line? For example, the default prompt tells

PS C:\Documents and Settings\govendes\My Documents> 

I want to configure this line.

thank

+49
windows powershell
Apr 20 2018-11-11T00:
source share
5 answers

Just enter the prompt function in your Powershell profile ( notepad $PROFILE ), for example:

 function prompt {"PS: $(get-date)>"} 

or color:

 function prompt { Write-Host ("PS " + $(get-date) +">") -nonewline -foregroundcolor White return " " } 
+72
Apr 20 2018-11-11T00:
source share

In connection with the comment above for Windows Server 2012 and Win7, the following is required:

 new-item -itemtype file -path $profile -force notepad $PROFILE 

I suggest the following as an invitation if you run multiple usernames (for example, on your own + login):

 function Global:prompt {"PS [$Env:username]$PWD`n>"} 

(credit to David I. MacIntosh for this)

+14
Jan 06 '14 at
source share

If you want to do it yourself, then Ocaso Protal answer is the way to go. But if you're lazy like me and just want to do something for you, I highly recommend the Luke Sampson Pshazz package .

Just to show you how lazy you are, I will give a quick tutorial.

  • Install Pshazz with Scoop ( scoop install pshazz )
  • Use a nice predefined theme ( pshazz use msys )
  • Drinking (root) beer

Pshazz also allows you to create your own themes, which is as simple as setting up a JSON file. Look mine to see how easy it is!

+3
Dec 21 '16 at 15:37
source share

On the command line, I like the current timestamp and the allowed drive letters for network drives. To make it more readable, I put it in two lines and played a little with the colors.

With CMD, I ended up with

  PROMPT=$E[33m$D$T$H$H$H$S$E[37m$M$_$E[1m$P$G 

For PS, I got the same result:

 function prompt { $dateTime = get-date -Format "dd.MM.yyyy HH:mm:ss" $currentDirectory = $(Get-Location) $UncRoot = $currentDirectory.Drive.DisplayRoot write-host "$dateTime" -NoNewline -ForegroundColor White write-host " $UncRoot" -ForegroundColor Gray # Convert-Path needed for pure UNC-locations write-host "PS $(Convert-Path $currentDirectory)>" -NoNewline -ForegroundColor Yellow return " " } 

This is a bit readable :-)

BTW:

  • I prefer powershell_ise.exe $PROFILE over (dumb) notepad
  • If you like to debug tooltip () with breakpoints, you should rename the request function to something else (or try in another file). Otherwise, you may end up in a loop: when you stop debugging, prompt () is called again, and you stop at the breakpoint again. Very annoying at first ...
0
Aug 31 '17 at 8:10
source share

In this version of the answer, Warren Stevens avoids the noisy "Microsoft.PowerShell.Core \ FileSystem" in the way if you Set-Location to network resources.

 function prompt {"PS [$Env:username@$Env:computername]$($PWD.ProviderPath)`n> "} 
0
Oct 28 '17 at 15:39
source share



All Articles