|
Re: Prompt "Robert Fuchs" <me@privacy.net> wrote in message
news:%23pvdsLVwGHA.4872@TK2MSFTNGP02.phx.gbl...
> Hi,
>
> how can I set the prompt in PowerShell?
>
> I use [%computername%]$s[%username%]$s$d$s$t$h$h$h$_$m$p$_$+$g with cmd
> and would like something smilar in PS.
Could you show the prompt as you would like it to appear in the shell? In
general you just create a "prompt" function in your profile and have it
create whatever string you want. Here is what I use:
function prompt {
# Display the history ID number of the next command
$history = @(get-history)
if ($history.Count -gt 0) {
$lastItem = $history[$history.Count - 1]
$lastId = $lastItem.Id
}
$nextCommand = [int]$lastId + 1
# Does the current user had admin privileges
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = new-object
System.Security.Principal.WindowsPrincipal($currentUser)
$isAdmin =
$principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($isAdmin) { $sep = "#" } else { $sep = ">" }
# Determine what nesting level we are at (if any)
$nestingLevel = ''
if ($nestedpromptlevel -ge 1) {
$nestingLevel = "[${nestedpromptlevel}]"
}
# Output prompt string
"[$(get-location)]`n" +
"$nextCommand" + $nestingLevel + " $sep "
}
--
Keith |