PoshCode Logo PowerShell Code Repository

Set-Prompt by Joel Bennett 4 years ago
View followups from Joel Bennett | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1834"></script>download | new post

My command Prompt

  1. # This should go OUTSIDE the prompt function, it doesn't need re-evaluation
  2. # We're going to calculate a prefix for the window title
  3. # Our basic title is "PoSh - C:\Your\Path\Here" showing the current path
  4. if(!$global:WindowTitlePrefix) {
  5.    # But if you're running "elevated" on vista, we want to show that ...
  6.    if( ([System.Environment]::OSVersion.Version.Major -gt 5) -and ( # Vista and ...
  7.          new-object Security.Principal.WindowsPrincipal (
  8.             [Security.Principal.WindowsIdentity]::GetCurrent()) # current user is admin
  9.             ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) )
  10.    {
  11.       $global:WindowTitlePrefix = "PoSh (ADMIN)"
  12.    } else {
  13.       $global:WindowTitlePrefix = "PoSh"
  14.    }
  15. }
  16.  
  17. function prompt {
  18.    # FIRST, make a note if there was an error in the previous command
  19.    $err = !$?
  20.  
  21.    # Make sure Windows and .Net know where we are (they can only handle the FileSystem)
  22.    [Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
  23.    # Also, put the path in the title ... (don't restrict this to the FileSystem
  24.    $Host.UI.RawUI.WindowTitle = "{0} - {1} ({2})" -f $global:WindowTitlePrefix,$pwd.Path,$pwd.Provider.Name
  25.    
  26.    # Determine what nesting level we are at (if any)
  27.    $Nesting = "$([char]0xB7)" * $NestedPromptLevel
  28.  
  29.    # Generate PUSHD(push-location) Stack level string
  30.    $Stack = "+" * (Get-Location -Stack).count
  31.    
  32.    # my New-Script and Get-PerformanceHistory functions use history IDs
  33.    # So, put the ID of the command in, so we can get/invoke-history easier
  34.    # eg: "r 4" will re-run the command that has [4]: in the prompt
  35.    $global:lastCommandId = (Get-History -count 1).Id
  36.    $global:nextCommandId = $global:lastCommandId + 1
  37.    # Output prompt string
  38.    # If there's an error, set the prompt foreground to "Red", otherwise, "Yellow"
  39.    if($err) { $fg = "Red" } else { $fg = "Yellow" }
  40.    # Notice: no angle brackets, makes it easy to paste my buffer to the web
  41.    Write-Host "[${Nesting}${nextCommandId}${Stack}]:" -NoNewLine -Fore $fg  
  42.    return " "
  43. }
  44. # SIG # Begin signature block
  45. # MIIRDAYJKoZIhvcNAQcCoIIQ/TCCEPkCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
  46. # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
  47. # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUEBrAGqg9LWArXQEDCZQGec1A
  48. # e5aggg5CMIIHBjCCBO6gAwIBAgIBFTANBgkqhkiG9w0BAQUFADB9MQswCQYDVQQG
  49. # EwJJTDEWMBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMiU2VjdXJlIERp
  50. # Z2l0YWwgQ2VydGlmaWNhdGUgU2lnbmluZzEpMCcGA1UEAxMgU3RhcnRDb20gQ2Vy
  51. # dGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDcxMDI0MjIwMTQ1WhcNMTIxMDI0MjIw
  52. # MTQ1WjCBjDELMAkGA1UEBhMCSUwxFjAUBgNVBAoTDVN0YXJ0Q29tIEx0ZC4xKzAp
  53. # BgNVBAsTIlNlY3VyZSBEaWdpdGFsIENlcnRpZmljYXRlIFNpZ25pbmcxODA2BgNV
  54. # BAMTL1N0YXJ0Q29tIENsYXNzIDIgUHJpbWFyeSBJbnRlcm1lZGlhdGUgT2JqZWN0
  55. # IENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyiOLIjUemqAbPJ1J
  56. # 0D8MlzgWKbr4fYlbRVjvhHDtfhFN6RQxq0PjTQxRgWzwFQNKJCdU5ftKoM5N4YSj
  57. # Id6ZNavcSa6/McVnhDAQm+8H3HWoD030NVOxbjgD/Ih3HaV3/z9159nnvyxQEckR
  58. # ZfpJB2Kfk6aHqW3J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89. # aWF0ZSBPYmplY3QgQ0EwHhcNMDkxMTExMDAwMDAxWhcNMTExMTExMDYyODQzWjCB
  90. # qDELMAkGA1UEBhMCVVMxETAPBgNVBAgTCE5ldyBZb3JrMRcwFQYDVQQHEw5XZXN0
  91. # IEhlbnJpZXR0YTEtMCsGA1UECxMkU3RhcnRDb20gVmVyaWZpZWQgQ2VydGlmaWNh
  92. # dGUgTWVtYmVyMRUwEwYDVQQDEwxKb2VsIEJlbm5ldHQxJzAlBgkqhkiG9w0BCQEW
  93. # GEpheWt1bEBIdWRkbGVkTWFzc2VzLm9yZzCCASIwDQYJKoZIhvcNAQEBBQADggEP
  94. # ADCCAQoCggEBAMfjItJjMWVaQTECvnV/swHQP0FTYUvRizKzUubGNDNaj7v2dAWC
  95. # rAA+XE0lt9JBNFtCCcweDzphbWU/AAY0sEPuKobV5UGOLJvW/DcHAWdNB/wRrrUD
  96. # dpcsapQ0IxxKqpRTrbu5UGt442+6hJReGTnHzQbX8FoGMjt7sLrHc3a4wTH3nMc0
  97. # U/TznE13azfdtPOfrGzhyBFJw2H1g5Ag2cmWkwsQrOBU+kFbD4UjxIyus/Z9UQT2
  98. # R7bI2R4L/vWM3UiNj4M8LIuN6UaIrh5SA8q/UvDumvMzjkxGHNpPZsAPaOS+RNmU
  99. # Go6X83jijjbL39PJtMX+doCjS/lnclws5lUCAwEAAaOCA4EwggN9MAkGA1UdEwQC
  100. # MAAwDgYDVR0PAQH/BAQDAgeAMDoGA1UdJQEB/wQwMC4GCCsGAQUFBwMDBgorBgEE
  101. # AYI3AgEVBgorBgEEAYI3AgEWBgorBgEEAYI3CgMNMB0GA1UdDgQWBBR5tWPGCLNQ
  102. # yCXI5fY5ViayKj6xATCBqAYDVR0jBIGgMIGdgBTQTg9AmWy4SxlvOyi44OOIBzSq
  103. # t6GBgaR/MH0xCzAJBgNVBAYTAklMMRYwFAYDVQQKEw1TdGFydENvbSBMdGQuMSsw
  104. # KQYDVQQLEyJTZWN1cmUgRGlnaXRhbCBDZXJ0aWZpY2F0ZSBTaWduaW5nMSkwJwYD
  105. # VQQDEyBTdGFydENvbSBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eYIBFTCCAUIGA1Ud
  106. # IASCATkwggE1MIIBMQYLKwYBBAGBtTcBAgEwggEgMC4GCCsGAQUFBwIBFiJodHRw
  107. # Oi8vd3d3LnN0YXJ0c3NsLmNvbS9wb2xpY3kucGRmMDQGCCsGAQUFBwIBFihodHRw
  108. # Oi8vd3d3LnN0YXJ0c3NsLmNvbS9pbnRlcm1lZGlhdGUucGRmMIG3BggrBgEFBQcC
  109. # AjCBqjAUFg1TdGFydENvbSBMdGQuMAMCAQEagZFMaW1pdGVkIExpYWJpbGl0eSwg
  110. # c2VlIHNlY3Rpb24gKkxlZ2FsIExpbWl0YXRpb25zKiBvZiB0aGUgU3RhcnRDb20g
  111. # Q2VydGlmaWNhdGlvbiBBdXRob3JpdHkgUG9saWN5IGF2YWlsYWJsZSBhdCBodHRw
  112. # Oi8vd3d3LnN0YXJ0c3NsLmNvbS9wb2xpY3kucGRmMGMGA1UdHwRcMFowK6ApoCeG
  113. # JWh0dHA6Ly93d3cuc3RhcnRzc2wuY29tL2NydGMyLWNybC5jcmwwK6ApoCeGJWh0
  114. # dHA6Ly9jcmwuc3RhcnRzc2wuY29tL2NydGMyLWNybC5jcmwwgYkGCCsGAQUFBwEB
  115. # BH0wezA3BggrBgEFBQcwAYYraHR0cDovL29jc3Auc3RhcnRzc2wuY29tL3N1Yi9j
  116. # bGFzczIvY29kZS9jYTBABggrBgEFBQcwAoY0aHR0cDovL3d3dy5zdGFydHNzbC5j
  117. # b20vY2VydHMvc3ViLmNsYXNzMi5jb2RlLmNhLmNydDAjBgNVHRIEHDAahhhodHRw
  118. # Oi8vd3d3LnN0YXJ0c3NsLmNvbS8wDQYJKoZIhvcNAQEFBQADggEBACY+J88ZYr5A
  119. # 6lYz/L4OGILS7b6VQQYn2w9Wl0OEQEwlTq3bMYinNoExqCxXhFCHOi58X6r8wdHb
  120. # E6mU8h40vNYBI9KpvLjAn6Dy1nQEwfvAfYAL8WMwyZykPYIS/y2Dq3SB2XvzFy27
  121. # zpIdla8qIShuNlX22FQL6/FKBriy96jcdGEYF9rbsuWku04NqSLjNM47wCAzLs/n
  122. # FXpdcBL1R6QEK4MRhcEL9Ho4hGbVvmJES64IY+P3xlV2vlEJkk3etB/FpNDOQf8j
  123. # RTXrrBUYFvOCv20uHsRpc3kFduXt3HRV2QnAlRpG26YpZN4xvgqSGXUeqRceef7D
  124. # dm4iTdHK5tIxggI0MIICMAIBATCBkjCBjDELMAkGA1UEBhMCSUwxFjAUBgNVBAoT
  125. # DVN0YXJ0Q29tIEx0ZC4xKzApBgNVBAsTIlNlY3VyZSBEaWdpdGFsIENlcnRpZmlj
  126. # YXRlIFNpZ25pbmcxODA2BgNVBAMTL1N0YXJ0Q29tIENsYXNzIDIgUHJpbWFyeSBJ
  127. # bnRlcm1lZGlhdGUgT2JqZWN0IENBAgFRMAkGBSsOAwIaBQCgeDAYBgorBgEEAYI3
  128. # AgEMMQowCKACgAChAoAAMBkGCSqGSIb3DQEJAzEMBgorBgEEAYI3AgEEMBwGCisG
  129. # AQQBgjcCAQsxDjAMBgorBgEEAYI3AgEWMCMGCSqGSIb3DQEJBDEWBBSmV2E5YX+V
  130. # qydJ/ryGk/Ebemx1SjANBgkqhkiG9w0BAQEFAASCAQBZRaRaBXCuNx1DaUFrs0xU
  131. # SoKgF/l2THb/fzFys9L6bp7Ye33OSh7CxntqbCDW/MomOoUuQS/FBg/ilB8V0qs1
  132. # /vktC2ROttW+JzEGy1LRhy6vbh8vxW0Hjb6ndT8eIbuHJxBHZcB8w6dMVtLCUMJW
  133. # UdiFRBR73fAHKZYqMsjOhT4hBozWGLXETJvnD45umXTmjvCN2w7br1HIhnp80AA8
  134. # JPeAMDzG604hEqrw6mWBZv36dw70JbHeEuucInaD+qH8E3dA6WSLbM7tBp4xjQL5
  135. # ftweGUsLxdYIsHKh89GJCIF1ToMF91W0R2rAvy+NUx+TZge0RZvCb8MK2oLcdD0g
  136. # 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