![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
| | #5 (permalink) |
| 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 Specs![]() |
| | #6 (permalink) |
| 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 Specs![]() |
![]() |
| 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 | |||