PoshCode Logo PowerShell Code Repository

Set-Prompt by Joel Bennett 17 months ago (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2095"></script>download | new post

My command Prompt — I recently added a “Pastable” option so that I can safely copy-paste a commandline and have it run without having to manually edit out my prompt.

  1. param([Alias("copy","demo")][Switch]$Pasteable)
  2. # This should go OUTSIDE the prompt function, it doesn't need re-evaluation
  3. # We're going to calculate a prefix for the window title
  4. # Our basic title is "PoSh - C:\Your\Path\Here" showing the current path
  5. if(!$global:WindowTitlePrefix) {
  6.    # But if you're running "elevated" on vista, we want to show that ...
  7.    if( ([System.Environment]::OSVersion.Version.Major -gt 5) -and ( # Vista and ...
  8.          new-object Security.Principal.WindowsPrincipal (
  9.             [Security.Principal.WindowsIdentity]::GetCurrent()) # current user is admin
  10.             ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) )
  11.    {
  12.       $global:WindowTitlePrefix = "PoSh ${Env:UserName}@${Env:UserDomain} (ADMIN)"
  13.    } else {
  14.       $global:WindowTitlePrefix = "PoSh ${Env:UserName}@${Env:UserDomain}"
  15.    }
  16. }
  17.  
  18. if($Pasteable) {
  19.    function global:prompt {
  20.       Write-host "<#PS " -NoNewLine -fore gray
  21.       # FIRST, make a note if there was an error in the previous command
  22.       $err = !$?
  23.  
  24.       # Make sure Windows and .Net know where we are (they can only handle the FileSystem)
  25.       [Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
  26.       # Also, put the path in the title ... (don't restrict this to the FileSystem
  27.       $Host.UI.RawUI.WindowTitle = "{0} - {1} ({2})" -f $global:WindowTitlePrefix,$pwd.Path,$pwd.Provider.Name
  28.      
  29.       # Determine what nesting level we are at (if any)
  30.       $Nesting = "$([char]0xB7)" * $NestedPromptLevel
  31.  
  32.       # Generate PUSHD(push-location) Stack level string
  33.       $Stack = "+" * (Get-Location -Stack).count
  34.      
  35.       # my New-Script and Get-PerformanceHistory functions use history IDs
  36.       # So, put the ID of the command in, so we can get/invoke-history easier
  37.       # eg: "r 4" will re-run the command that has [4]: in the prompt
  38.       $global:lastCommandId = (Get-History -count 1).Id
  39.       $global:nextCommandId = $global:lastCommandId + 1
  40.       # Output prompt string
  41.       # If there's an error, set the prompt foreground to "Red", otherwise, "Yellow"
  42.       if($err) { $fg = "Red" } else { $fg = "Yellow" }
  43.       # Notice: no angle brackets, makes it easy to paste my buffer to the web
  44.       Write-Host "[${Nesting}${nextCommandId}${Stack}]" -NoNewLine -Fore $fg  
  45.       Write-host " #>" -NoNewLine -fore gray
  46.       return " "
  47.    }
  48. } else {
  49.    function global:prompt {
  50.       # FIRST, make a note if there was an error in the previous command
  51.       $err = !$?
  52.  
  53.       # Make sure Windows and .Net know where we are (they can only handle the FileSystem)
  54.       [Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
  55.       # Also, put the path in the title ... (don't restrict this to the FileSystem
  56.       $Host.UI.RawUI.WindowTitle = "{0} - {1} ({2})" -f $global:WindowTitlePrefix,$pwd.Path,$pwd.Provider.Name
  57.      
  58.       # Determine what nesting level we are at (if any)
  59.       $Nesting = "$([char]0xB7)" * $NestedPromptLevel
  60.  
  61.       # Generate PUSHD(push-location) Stack level string
  62.       $Stack = "+" * (Get-Location -Stack).count
  63.      
  64.       # my New-Script and Get-PerformanceHistory functions use history IDs
  65.       # So, put the ID of the command in, so we can get/invoke-history easier
  66.       # eg: "r 4" will re-run the command that has [4]: in the prompt
  67.       $global:lastCommandId = (Get-History -count 1).Id
  68.       $global:nextCommandId = $global:lastCommandId + 1
  69.       # Output prompt string
  70.       # If there's an error, set the prompt foreground to "Red", otherwise, "Yellow"
  71.       if($err) { $fg = "Red" } else { $fg = "Yellow" }
  72.       # Notice: no angle brackets, makes it easy to paste my buffer to the web
  73.       Write-Host "[${Nesting}${nextCommandId}${Stack}]:" -NoNewLine -Fore $fg  
  74.       return " "
  75.    }
  76. }
  77.  
  78. # SIG # Begin signature block
  79. # MIIID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QEwgaow
  110. # gZUxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJVVDEXMBUGA1UEBxMOU2FsdCBMYWtl
  111. # IENpdHkxHjAcBgNVBAoTFVRoZSBVU0VSVFJVU1QgTmV0d29yazEhMB8GA1UECxMY
  112. # aHR0cDovL3d3dy51c2VydHJ1c3QuY29tMR0wGwYDVQQDExRVVE4tVVNFUkZpcnN0
  113. # LU9iamVjdAIQKQm90jYWUDdv7EgFkuELajAJBgUrDgMCGgUAoHgwGAYKKwYBBAGC
  114. # NwIBDDEKMAigAoAAoQKAADAZBgkqhkiG9w0BCQMxDAYKKwYBBAGCNwIBBDAcBgor
  115. # BgEEAYI3AgELMQ4wDAYKKwYBBAGCNwIBFTAjBgkqhkiG9w0BCQQxFgQUAYxB1OGv
  116. # +gSNgLzV07nPJLNuFvQwDQYJKoZIhvcNAQEBBQAEggEAFM65fpcjQcDiRslwg9hl
  117. # Mf8sVPS5hOxSFkOH6SuOXOFggJTnpqH7Oiz9/67y7ndGhmb4h22WsW3sWIS29zGt
  118. # ZchjJk8mKcTzZB8foS0xJNLbYu8yGzDRdItUN3Hq1bvK+1B8RIcpY7NfTshQmsiD
  119. # ZWzhBM82vaGp03cXzzLrAhiKDIcFI43P2LQbjz5rtcl3aJHiK0y9pYq5v9Fk76jk
  120. # B5oNwvbWi9TLt8GEUYp48zRXLYP/Nnr3HJLcA4BHzfmAjopZGw54Oprr87LqXxv3
  121. # OrArwdsdjw6HOMk2NW0txZm1f0wR3O6Tt2KZjWzB0Ru7GjHkKj618OVebDvON755
  122. # lw==
  123. # SIG # End signature block

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me