PoshCode Logo PowerShell Code Repository

Short PS Prompt by Andy Levy 18 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3997"></script>download | new post

Dynamically adjusts the length of the path displayed in your prompt based upon the width of the window

  1. <#
  2. .Synopsis
  3.         Dynamically shortens the prompt based upon window size
  4. .Notes
  5.         I got really annoyed by having my PowerShell prompt extend across 2/3 of my window when in a deeply-nested directory structure.
  6.         This shortens the prompt to roughly 1/3 of the window width, at a minimum showing the first and last piece of the path (usually the PSPROVIDER & the current directory)
  7.         Additional detail is added, starting at the current directory's parent and working up from there.
  8.         The omitted portion of the path is represented with an ellipsis (...)
  9. #>
  10.  
  11. # Window title borrowed from Joel Bennett @ http://poshcode.org/1834
  12. # This should go OUTSIDE the prompt function, it doesn't need re-evaluation
  13. # We're going to calculate a prefix for the window title
  14. # Our basic title is "PoSh - C:\Your\Path\Here" showing the current path
  15. if(!$global:WindowTitlePrefix) {
  16.    # But if you're running "elevated" on vista, we want to show that ...
  17.    if( ([System.Environment]::OSVersion.Version.Major -gt 5) -and ( # Vista and ...
  18.          new-object Security.Principal.WindowsPrincipal (
  19.             [Security.Principal.WindowsIdentity]::GetCurrent()) # current user is admin
  20.             ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) )
  21.    {
  22.       $global:WindowTitlePrefix = "PoSh (ADMIN)"
  23.    } else {
  24.       $global:WindowTitlePrefix = "PoSh"
  25.    }
  26. }
  27.  
  28. function prompt {
  29. # Put the full path in the title bar for reference
  30.     $host.ui.rawui.windowtitle = $global:WindowTitlePrefix + " - " + $(get-location);
  31.        
  32. # Capture the maximum length of the prompt. If you want a longer prompt, adjust the math as necessary.
  33.         $winWidth = $host.UI.RawUI.WindowSize.Width;
  34.     $maxPromptPath = [Math]::Round($winWidth/3);
  35.        
  36. # In the PowerShell ISE (version 2.0 at least), $host.UI.RawUI.WindowSize.Widthis $null.
  37. # For now, I'm just going to leave the default prompt for this scenario, as I don't work in the ISE.
  38.     if (-not ($winWidth -eq $null)) {
  39.         $currPath = (get-location).path;
  40.         if ($currPath.length -ge $maxPromptPath){
  41.             $pathParts = $currPath.split([System.IO.Path]::DirectorySeparatorChar);
  42. # Absolute minimum path - PSPROVIDER and the current directory
  43.             $myPrompt = $pathParts[0] + [System.IO.Path]::DirectorySeparatorChar+ "..." + [System.IO.Path]::DirectorySeparatorChar + $pathParts[$pathParts.length - 1];
  44.             $counter = $pathParts.length - 2;
  45. # This builds up the prompt until it reaches the maximum length we set earlier.
  46. # Start at the current directory's parent and keep going up until the whole prompt reaches the previously-determined limit.
  47.             while( ($myPrompt.replace("...","..."+[System.IO.Path]::DirectorySeparatorChar+$pathParts[$counter]).length -lt $maxPromptPath) -and ($counter -ne 0)) {
  48.                 $myPrompt = $myPrompt.replace("...","..."+[System.IO.Path]::DirectorySeparatorChar+$pathParts[$counter]);
  49.                 $counter--;
  50.             }      
  51.             $($myPrompt) + ">";    
  52.         } else{
  53. # If there's enough room for the full prompt, use the Powershell default prompt
  54.             $(if (test-path variable:/PSDebugContext) { '[DBG]: ' } else { '' }) + 'PS ' + $(Get-Location) + $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '
  55.         }
  56.     }
  57. }

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