Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Prompt

Reply
 
Old 08-16-2006   #1 (permalink)
Robert Fuchs
Guest


 
 

Prompt

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


My System SpecsSystem Spec
Old 08-16-2006   #2 (permalink)
Keith Hill [MVP]
Guest


 
 

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


My System SpecsSystem Spec
Old 08-16-2006   #3 (permalink)
Robert Fuchs
Guest


 
 

Re: Prompt

> 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

My System SpecsSystem Spec
Old 08-16-2006   #4 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=
Guest


 
 

Re: Prompt


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...

My System SpecsSystem Spec
Old 08-16-2006   #5 (permalink)
Keith Hill [MVP]
Guest


 
 

Re: Prompt

"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


My System SpecsSystem Spec
Old 08-16-2006   #6 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=
Guest


 
 

Re: Prompt

"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...
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Prompt - [WP] PowerShell
Uac prompt General Discussion
Re: Where is the dos prompt? Vista General
Log in prompt Live Mail
Run As doesn't prompt Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46