Sunday, August 06, 2006

PowerShell Prompt Part 2

In PowerShell prompt is a function
$dir function:

CommandType Name Definition
----------- ---- ----------
Function prompt $
Function TabExpansion ...
Function Clear-Host $spaceType
Function more param([string[]]$paths);
Function help param([string]$help);
Function man param([string]$help);
Function mkdir param([string[]]$paths);
Function md param([string[]]$paths);

Simple prompt in PowerShell

function prompt { "$"}

CMD equivalent: PROMPT $$

Bash equivalent: PS1="$"

Most common prompt which displays your current working directory

function prompt { "$(get-location)> " }

CMD : PROMPT $P$G

Bash : PS1='\w$' or PS1='`pwd`'

Another prompt to display current working directory ( not the full path )

function prompt { "$(split-path $(get-locaiton) -leaf)> "

CMD : ????

Bash : PS1='\W $'

Prompt with linefeed character

function prompt { "$(get-location)`n> "}

CMD : $P$_$G

Bash : PS1='\w\n>'

Prompt with DateTime

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

CMD : $T$G

Bash : PS1='\t>'

Bash has four special codes for representing datetime prompt string

\t : 17:05:05
\T : 05:05:05
\@ : 05:05 PM
\d : Sun Aug, 06

Let's do samething in powershell

"$($(get-date).tostring('HH:MM:s'))" ==> 17:05:05

"$($(get-date).tostring('hh:MM:s'))" ==> 05:05:05

"$($(get-date).tostring('hh:MM tt'))" ==> 05:05 PM

"$($(get-date).tostring('ddd MMM, dd'))" ==> Sun Aug, 06

Prompt with Hostname

function prompt { "$env:computername > "}

CMD: prompt %computername%$G

Bash : PS1='\h>'

NOTE: Bash also offers \H which will give you the complete hostname in the domain, you can mimic the same thing in powershell using following prompt

function prompt { "$([System.Net.Dns]::GetHostByName([System.Net.Dns]::GetHostName()).hostname) >" }


Prompt with Username

function prompt { "$env:username > "}

CMD: prompt %username%$G

Bash : PS1='\u>'

0 comments: