|
Monday, May 01, 2006
Perfect Prompt for Windows PowerShell
Windows PowerShell have a default prompt in one color (usually gray) that tells you your current working directory. This is OK, but you can do much more with the prompt.
1. All sorts of information can be displayed (machine name, host name, user name, time & date ...)
2. The prompt can use colors
3. You can also manipulate the windows title to dispaly more information.
4. Other RawUI operation is allowed (move cursor)
Why Bother?
Beyond looking cool, it's often useful to keep track of system information.
1. Get your current working directory (how many files in current directory)
2. Current system time, how many process is running
2. If you use my su.msh, you would like to know you current windows identity
3. If you use my powershell remoting, different color helps distingish between local host and remote host.
4. Colorizing your prompt is the ability to quickly spot the prompt when you use scroll console.
First step: Profile and prompt function
Windows PowerShell use Profile to customize user environment. For more detail, about profile
The Story Behind the Naming and Location of PowerShell Profiles:
http://www.leeholmes.com/blog/TheStoryBehindTheNamingAndLocationOfPowerShellProfiles.aspx
1. You can have different prompt for different shell (Maybe you use makeshell.exe generated you own shell)
2. If you have different prompt function defined in multiple profile, the last one excuted take effect
Remember the excution order is
Edit 09-29-2006
- "All users" profile is loaded from "<Installation Directory>\profile.ps1"
- "All users," host-specific profile is loaded from "<Installation Directory>\Microsoft.PowerShell_profile.ps1"
- Current user profile is loaded from "<My Documents>\WindowsPowerShell\profile.ps1"
- Current User, host-specific profile is loaded from "<My Documents>\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
Windows PowerShell RC2 changed profile location to
<My documents>\WindowsPowerShell
Now you can customize you prompt in you profile (say <my document>\psconfigurtion\profile.ps1)
this is typical prompt function looks like:
function promptYou can do whatever you want in prompt function. But remember
{
"PS " + $(get-location) + "> "
}
1) Always return a [string], otherwise Windows PowerShell will use default "PS> " prompt.
2) Try to limit your prompt in one (short) line
3) Host will evaluate prompt frequently, so don't do crazy stuff to slow down your work.
Colorized prompt
function promptRandom color
{
Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor Magenta
return " "
}
function promptCursor Movement
{
$random = new-object random
$color=[System.ConsoleColor]$random.next(1,16)
Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor $color
return " "
}
Display current time at the end of prompt line (this will mess up you console buffer)
function promptUse Window Title
{
$oldposition = $host.ui.rawui.CursorPosition
$Endline = $oldposition
$Endline.X+=60
$host.ui.rawui.CursorPosition = $Endline
Write-Host $(get-date).Tostring("yyyy-MM-dd HH:mm:ss")
$host.ui.rawui.CursorPosition = $oldposition
Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor Magenta
return " "
}
Show current user, host, current line number
$global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()Make some noise
function prompt
{
$host.ui.rawui.WindowTitle = $CurrentUser.Name + " " + $Host.Name + " " + $Host.Version + " Line: " + $host.UI.RawUI.CursorPosition.Y
Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor Magenta
return " "
}
if your command take very long time to run, beep when it is done.
function promptMore information
{
Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor Magenta
return "`a "
}
Count number of files(or items) in current path and number of process running
function promptSomething for readers:
{
$host.ui.rawui.WindowTitle = "Files: " + (get-childitem).count + " Process: " + (get-process).count
Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor Magenta
return " "
}
1. laptop battery is low ! (Edit 2006-07-21: Already done by Musings of a PC)
2. You got new mail !
3. LAN cable disconnected !
Have Fun
Reference:
http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/
Tags: msh monad powershell
Comments:
<< Home
Thanks. I created this variation on your use Windows Title.
$Global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$UserType = "User"
$CurrentUser.Groups | foreach { if($_.value -eq "S-1-5-32-544") {$UserType = "Admin"} }
function prompt {
$host.UI.RawUI.WindowTitle = $CurrentUser.Name + " as " + $UserType
"$(get-location)>"
}
$Global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$UserType = "User"
$CurrentUser.Groups | foreach { if($_.value -eq "S-1-5-32-544") {$UserType = "Admin"} }
function prompt {
$host.UI.RawUI.WindowTitle = $CurrentUser.Name + " as " + $UserType
"$(get-location)>"
}
Nice.
But you can also do it like this:
$principal = new-object System.Security.principal.windowsprincipal($curr
entuser)
$principal.isinrole("Administrator")
But you can also do it like this:
$principal = new-object System.Security.principal.windowsprincipal($curr
entuser)
$principal.isinrole("Administrator")
To get ">" also in your color:
function prompt
{
Write-Host ("PS " + $(get-location) + ">") -nonewline -foregroundcolor Magenta
return " "
}
function prompt
{
Write-Host ("PS " + $(get-location) + ">") -nonewline -foregroundcolor Magenta
return " "
}
PSMDTAG:SHELL: Lot's of cool PROMPT scripts.
PSMDTAG:Environment: Prompt - how to set up
PSMDTAG:FAQ: PROMPT - How can I include my current location?
PSMDTAG:FAQ: PROMPT - How can I change the color?
PSMDTAG:FAQ: PROMPT - How do I include the current datetime?
PSMDTAG:FAQ: PROMPT - How do I include the current user?
Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
PSMDTAG:Environment: Prompt - how to set up
PSMDTAG:FAQ: PROMPT - How can I include my current location?
PSMDTAG:FAQ: PROMPT - How can I change the color?
PSMDTAG:FAQ: PROMPT - How do I include the current datetime?
PSMDTAG:FAQ: PROMPT - How do I include the current user?
Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Rather than calculating if the user is part of Administrators group on every prompt display , you should put following line in your global PowerShell profile and then just use $Admin variable in your prompt string.
$Global:Admin="$"
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = new-object System.Security.principal.windowsprincipal($CurrentUser)
if ($principal.IsInRole("Administrators"))
{
$Admin="#"
}
Function prompt
{
"PS $(get-location) $Admin "
}
$Global:Admin="$"
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = new-object System.Security.principal.windowsprincipal($CurrentUser)
if ($principal.IsInRole("Administrators"))
{
$Admin="#"
}
Function prompt
{
"PS $(get-location) $Admin "
}
What's the idea of all those links in the comments? One is surely enough, no?
this site | auto profit launcher bonuses | howies apprentice 5 bonuses
this site | auto profit launcher bonuses | howies apprentice 5 bonuses
Hey,
I am so lucky that I found your blog and great articles. I will come to your blog often for finding new great articles from your blog. I am adding your RSS feed in my reader Thank you…
Assignment Writing
I am so lucky that I found your blog and great articles. I will come to your blog often for finding new great articles from your blog. I am adding your RSS feed in my reader Thank you…
Assignment Writing
Hi,
Thank you a lot for this information, and looking forward to reading more in the future, as I have bookmarked your site, this post is really very informative. Thanks
Buy Essay
Thank you a lot for this information, and looking forward to reading more in the future, as I have bookmarked your site, this post is really very informative. Thanks
Buy Essay
Your blog came up in my search and I’m stricken by what you have written on this subject.
I am currently branching out my search and thus cannot contribute further, still, I have bookmarked your website and will be returning to keep up with any upcoming updates.
I am currently branching out my search and thus cannot contribute further, still, I have bookmarked your website and will be returning to keep up with any upcoming updates.
<< Home
Post a Comment