If you are like me and live in PowerShell, then you spend a great deal of your day looking at your PowerShell prompt. That little indicator in the console and ISE that usually shows where you are. That little part of your PowerShell world is defined by a built-in function called Prompt. You can easily see the function like this:
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
PS C:\> $function:prompt "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " # .Link # http://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml PS C:\>
This prompt is from PowerShell v4 but I'm pretty sure it is the same function that was used in v3. PowerShell v2 has a different function.
PS C:\> $function:prompt $(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' + $(Get-Location) + $(if ($nestedpromptlev el -ge 1) { '>>' }) + '> ' PS C:\>
Did you notice that the newer function has a help link? Try it:
help prompt -online
You'll get the online version of the about_prompts help topic. The great thing about the prompt function is that you can change it. I've posted a variety of prompts over the years. But here are 4 more for you to try out. These prompts should work in v3 and later. Most of the functions are simple additions to the standard prompt and should work for both the console and ISE. To try out the prompt you can paste the function into your PowerShell session. To make it "permanent", insert it into your PowerShell profile script.
Include PowerShell Version
Function Prompt { "PS $($psversiontable.psversion.major) $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " # .Link # http://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml }
This prompt inserts the PowerShell major version into your prompt.
Include Admin
Function Prompt { $default = "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = [Security.Principal.WindowsPrincipal] $identity if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Host "[ADMIN] " -NoNewline -ForegroundColor Red $default } else { $default } # .Link # http://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml }
This prompt will test if you are running as Admin and if so, it inserts [ADMIN] in red text.
Include Computername
Function Prompt { "[$($env:computername)] PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " # .Link # http://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml }
Do you like how a remoting session shows you the computer you are connected to? Why not have that all the time? All I've done is insert the local computername from the Computername environmental variable.
Auto Export Command History
Function Prompt { "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " #define a log file with the host name, a time stamp and process ID # WindowsPowerShellISEHost_20140715_7388.log # the log file will go in the Windows PowerShell directory $log = "{0}_{1:yyyyMMdd}_{2}.log" -f $host.name.Replace(" ",""),(get-date),$PID $logfile = Join-Path -path $home\Documents\WindowsPowerShell -childpath $log #only insert history if it is different than the last command ran $mycmd = (Get-History -Count 1).CommandLine if ($mycmd -ne (Get-Content -Path $logfile -Tail 1)) { $mycmd | Out-File -FilePath $logfile -Encoding ascii -Append } # .Link # http://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml }
This last version serves up a twist on transcription. When you run a transcript you get the command and results. But maybe all you want is a record of all the commands you ran. Sure, you could export command history at the end of your session, but you have to remember to do so and if you exceed your maximum history count, you'll miss commands. In this prompt, everytime you hit enter, it gets the last command you ran and appends it to a log file. The log file is created in your PowerShell directory and uses the naming format of the PowerShell host, without spaces, a time stamp (YearMonthDay) and the process ID of the current PowerShell session. This allows you to keep multiple PowerShell sessions with separate logs. The log file will only record the command if it is different than the last one you ran. This also allows you to hit Enter without doing anything and not fill up your log.
If you temporarily paste in one of these Prompt functions, but don't like it, you can simply restart PowerShell to get your original prompt. Or you can use this function to restore it.
Function Restore-Prompt { #reset the original prompt or whatever you want to use as your default Function global:Prompt { "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " # .Link # http://go.microsoft.com/fwlink/?LinkID=225750 # .ExternalHelp System.Management.Automation.dll-help.xml } #original prompt }
This is handy to put into your PowerShell profile if you are experimenting with prompts. The Restore-Prompt simply defines a new Prompt function in the global scope. I'm using the default PowerShell prompt but you change it to whatever you wanted.
If you are doing something cool with your prompt, I hope you'll share.
Very cool, and thanks for sharing… Where did you get the click to expand control?
I use the Crayon WordPress plugin to format my code samples. It is very configurable and one of the settings is to collapse code which I do by default to speed up page loads.