|
Monday, May 01, 2006
Perfect Prompt for Windows PowerShell
What Can Tweaking Your Prompt Do For You?
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
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:
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
Display current time at the end of prompt line (this will mess up you console buffer)
Show current user, host, current line number
if your command take very long time to run, beep when it is done.
Count number of files(or items) in current path and number of process running
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/
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 "
}
dessicant air dryerpediatric asthmaasthma specialistasthma children specialistcarpet cleaning dallas txcarpet cleaners dallascarpet cleaning dallas
vero beach vacationvero beach vacationsbeach vacation homes veroms beach vacationsms beach vacationms beach condosmaui beach vacationmaui beach vacationsmaui beach clubbeach vacationsyour beach vacationscheap beach vacations
bob hairstylebob haircutsbob layeredpob hairstylebobbedclassic bobCare for Curly HairTips for Curly Haircurly hair12r 22.5 best pricetires truck bustires 12r 22.5
washington new housenew house houstonnew house san antonionew house ventura
vero beach vacationvero beach vacationsbeach vacation homes veroms beach vacationsms beach vacationms beach condosmaui beach vacationmaui beach vacationsmaui beach clubbeach vacationsyour beach vacationscheap beach vacations
bob hairstylebob haircutsbob layeredpob hairstylebobbedclassic bobCare for Curly HairTips for Curly Haircurly hair12r 22.5 best pricetires truck bustires 12r 22.5
washington new housenew house houstonnew house san antonionew house ventura
new houston house houston house txstains removal dyestains removal clothesstains removalteeth whiteningteeth whiteningbright teeth
jennifer grey nosejennifer nose jobscalebrities nose jobsWomen with Big NosesWomen hairstylesBig Nose Women, hairstyles
jennifer grey nosejennifer nose jobscalebrities nose jobsWomen with Big NosesWomen hairstylesBig Nose Women, hairstyles
black mold exposureblack mold symptoms of exposurewrought iron garden gatesiron garden gates find them herefine thin hair hairstylessearch hair styles for fine thin hairnight vision binocularsbuy night vision binocularslipitor reactionslipitor allergic reactionsluxury beach resort in the philippines
afordable beach resorts in the philippineshomeopathy for eczema.baby eczema.save big with great mineral makeup bargainsmineral makeup wholesalersprodam iphone Apple prodam iphone prahacect iphone manualmanual for P 168 iphonefero 52 binocularsnight vision Fero 52 binocularsThe best night vision binoculars here
night vision binoculars bargainsfree photo albums computer programsfree software to make photo albumsfree tax formsprintable tax forms for free craftmatic air bedcraftmatic air bed adjustable info hereboyd air bedboyd night air bed lowest price
afordable beach resorts in the philippineshomeopathy for eczema.baby eczema.save big with great mineral makeup bargainsmineral makeup wholesalersprodam iphone Apple prodam iphone prahacect iphone manualmanual for P 168 iphonefero 52 binocularsnight vision Fero 52 binocularsThe best night vision binoculars here
night vision binoculars bargainsfree photo albums computer programsfree software to make photo albumsfree tax formsprintable tax forms for free craftmatic air bedcraftmatic air bed adjustable info hereboyd air bedboyd night air bed lowest price
find air beds in wisconsinbest air beds in wisconsincloud air beds
best cloud inflatable air bedssealy air beds portableportables air bedsrv luggage racksaluminum made rv luggage racksair bed raisedbest form raised air bedsbed air informercialsbest informercials bed airmattress sized air beds
bestair bed mattress antique doorknobsantique doorknob identification tipsdvd player troubleshootingtroubleshooting with the dvd playerflat panel television lcd vs plasmaflat panel lcd television versus plasma pic the bestadjustable bed air foam The best bed air foam
hoof prints antique equestrian printsantique hoof prints equestrian printsBuy air bedadjustablebuy the best adjustable air bedsair beds canadian storesCanadian stores for air beds
migraine causemigraine treatments floridaflorida headache clinicdrying dessicantair drying dessicant
best cloud inflatable air bedssealy air beds portableportables air bedsrv luggage racksaluminum made rv luggage racksair bed raisedbest form raised air bedsbed air informercialsbest informercials bed airmattress sized air beds
bestair bed mattress antique doorknobsantique doorknob identification tipsdvd player troubleshootingtroubleshooting with the dvd playerflat panel television lcd vs plasmaflat panel lcd television versus plasma pic the bestadjustable bed air foam The best bed air foam
hoof prints antique equestrian printsantique hoof prints equestrian printsBuy air bedadjustablebuy the best adjustable air bedsair beds canadian storesCanadian stores for air beds
migraine causemigraine treatments floridaflorida headache clinicdrying dessicantair drying dessicant
Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa pages. Thank you So much.
Oes Tsetnoc | Semangat Mengembalikan Jati Diri Bangsa
Oes Tsetnoc | Semangat Mengembalikan Jati Diri Bangsa
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,
Nice post! You have worked hard on jotting down the essential information. Keep sharing the good work in future too.
Coursework Writing
Nice post! You have worked hard on jotting down the essential information. Keep sharing the good work in future too.
Coursework 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