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.
thanks, Robert
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.
thanks, Robert
"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
> Could you show the prompt as you would like it to appear in the shell?
set prompt=[%computername%]$s[%username%]$s$d$s$t$h$h$h$_$m$p$_$+$g
produces this prompt if C: is a local drive:
[ROBERT] [Bob] 16.08.2006 19:17:16
C:\WINDOWS\system32
++>
produces this prompt if X: is a network drive:
[ROBERT] [Bob] 16.08.2006 19:17:16
\\SERVER\C$ X:\WINDOWS\system32
++>
The ++ indicates that I'm two levels deep in pushd.
Thanks for your infos so far.
regards, Robert
function prompt
{
# display Computer name, username and the date time
write-host -NoNewLine "[$($env:ComputerName)] [$($env:UserName)]
$(get-date)`n"
# Add a logic for displaying Network path for Network drive
# not implemented...
# display current working directory
write-host -NoNewLine "$(pwd)`n"
# display Stack level
Write-Host -NoNewLine ("+" * ((Get-Location -Stack).count))
">"
}
Uhm, I am not sure how to resolve a network path for network drive...
but once I figure out how to retrieve a network drive path, it won't be hard
to add that functionality.
Anyways, above should be close to what you wanted(you can change "get-date"
to produce other date format if you want to...
====================
Sung M Kim
Please don''t bother me with spam...
"dance2die" <dance2die@discussions.microsoft.com> wrote in message
news:A7EAA3A1-2C45-47E1-9FF5-938B14F09C50@microsoft.com...
>
> function prompt
> {
> # display Computer name, username and the date time
> write-host -NoNewLine "[$($env:ComputerName)] [$($env:UserName)]
> $(get-date)`n"
> # Add a logic for displaying Network path for Network drive
> # not implemented...
>
> # display current working directory
> write-host -NoNewLine "$(pwd)`n"
>
> # display Stack level
> Write-Host -NoNewLine ("+" * ((Get-Location -Stack).count))
> ">"
> }
I think the idea of the prompt function is to return a string that
PowerShell then writes to the host. So you might modify this to:
function prompt
{
$prompt = "[$($env:ComputerName)] [$($env:UserName)] $(get-date)`n"
$result = net use "$($pwd.drive):" 2> $null
if ($?) {
$result[1] -match "Remote name\s+(?<name>.*)" > $null
$matches.name + " "
}
$prompt + $(pwd)`n
$prompt += "+" * ((Get-Location -Stack).count
$prompt += "> "
$prompt
}
--
Keith
"Keith Hill [MVP]" wrote:
> I think the idea of the prompt function is to return a string that PowerShell then writes to the host
that certainly is a better of a practice since it reduces the calls to
write-host dramatically, and also improving "prompt" function efficiency(mine
sometimes takes a while to refresh ;p)
Thank you for the "Best Practices" there, I appreciate it.
I really want more over these Best Practice cases... and apply them to
PowerShell...
====================
Sung M Kim
Please don''t bother me with spam...
| Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Change to C:\ Prompt from C:\Users\username> Prompt | cyberbiker | General Discussion | 2 | 27 Nov 2009 |
| cmd prompt | Fuzzynutts | General Discussion | 3 | 30 Jul 2009 |
| Uac prompt | hdbg62 | General Discussion | 0 | 04 Nov 2008 |
| Re: Where is the dos prompt? | philo | Vista General | 4 | 24 Oct 2008 |
| DOS Prompt Here | Doit2it | Vista General | 12 | 09 Mar 2008 |