Ein besserer Powershell Prompt

Der Prompt von Powershell kann deutlich besser aussehen. Hier ist ein Beispiel. Nach jeder Ausführung wird der Inhalt von $dash_line, ein Zeitstempel, der Pfad, bei Netzwerklaufwerken, der dazugehörige UNC-Pfad und die Anzahl der Elemente im aktuellen Verzeichnis angezeigt. Zusätzlich wird der Titel des Fensters so abgeändert, dass er den verwendeten Benutzer anzeigt und in PS/ISE und mit/ohne Elevation unterscheidet.

Einfach die folgenden Zeilen ins Powershell-Profil kopieren.

$global:dash_line = "──────────────────────────────────────────────────────────────────"

function global:Prompt {

    $unc = (gwmi Win32_LogicalDisk -filter "DeviceID = '$((Get-Location).Drive.Name):'").ProviderName
    
    If ($Host.Name -like "*ISE*")
        {
        $windowtitle_host = "ISE"
        }
    else
        {
        $windowtitle_host = "PS"
        }

    $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
    if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
        {
        $window_elevation = "#"
        }
    else
        {
        $window_elevation = "$"
        }

    write-host $dash_line
    
    # brackets
    write-host "[" -nonewline -foregroundcolor white
    
    # Print the current time:
    write-host (Get-Date -format u) -nonewline -foregroundcolor green
    
    # brackets
    write-host ("] - [") -nonewline -foregroundcolor white
    
    # Print the working directory:
    write-host $PWD -nonewline -foregroundcolor Red
    write-host "|" -nonewline -foregroundcolor white
    write-host $unc -NoNewline -ForegroundColor Red
    write-host "|" -nonewline -foregroundcolor white
    
    # Print the number of objects inside the current directory:
    write-host (Get-Childitem $PWD -Force).Length -nonewline -foregroundcolor Red
    
    write-host "]" -foregroundcolor white
    
    # Print the prompt symbol:
    write-host (" $window_elevation") -nonewline -foregroundcolor white

    # Check for Principal and Set Window Title

    $Host.UI.RawUI.WindowTitle = $windowtitle_host + " " + $window_elevation + " - " + $env:USERNAME + "@" + $env:COMPUTERNAME + " - " + $PWD
    return " ";
}