Microsoft.PowerShell_profile.ps1
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
# ==== PowerShell Profile Configuration ====================

$StartupTimer = [System.Diagnostics.Stopwatch]::StartNew()

# ==== Check for Minimum PowerShell Version ===============

if ($PSVersionTable.PSVersion.Major -lt 7) {
    Write-Warning "PowerShell version 7 of higher required for this configuration"
    Write-Host ""
    Write-Host " - This profile is located at $($PROFILE)"
    Write-Host " - PowerShell 7 can be installed via https://aka.ms/install-powershell"
    Write-Host ""
    return
}

# ==== Setup PSReadLine ====================================

if ($Host.Name -eq 'ConsoleHost') {

    Import-Module PSReadLine -ErrorAction SilentlyContinue

    if (Get-Module PSReadLine) {

        $PSReadlineConfigOptions = @{
            EditMode                      = 'Windows'
            HistoryNoDuplicates           = $true
            HistorySearchCursorMovesToEnd = $true
            PredictionSource              = 'HistoryAndPlugin'
            PredictionViewStyle           = 'ListView'
            ShowToolTips                  = $true
            BellStyle                     = 'None'
            MaximumHistoryCount           = 10000
            Colors                        = @{
                Command                = [ConsoleColor]::DarkMagenta
                Parameter              = [ConsoleColor]::Magenta      
                Operator               = [ConsoleColor]::DarkYellow 
                Variable               = [ConsoleColor]::Magenta      
                String                 = [ConsoleColor]::Green     
                Number                 = [ConsoleColor]::Cyan     
                Type                   = [ConsoleColor]::Blue      
                Comment                = [ConsoleColor]::DarkGray   
                Keyword                = [ConsoleColor]::Yellow   
                Error                  = [ConsoleColor]::Red      
                Emphasis               = [ConsoleColor]::Blue  
                Default                = [ConsoleColor]::White      
                InlinePrediction       = [ConsoleColor]::Blue
                ListPrediction         = [ConsoleColor]::Blue
                ListPredictionTooltip  = [ConsoleColor]::DarkGray
                ListPredictionSelected = "`e[48;2;56;58;72m"
                Selection              = "`e[48;2;56;58;72m"
            }
        }

        Set-PSReadLineOption @PSReadlineConfigOptions
        
        Set-PSReadLineOption -AddToHistoryHandler {
            param($line)
            $line -notmatch '(password|secret|token|apikey|connectionstring)'
        }

        Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
        Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
        Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
        Set-PSReadLineKeyHandler -Chord 'Ctrl+d' -Function DeleteChar
        Set-PSReadLineKeyHandler -Chord 'Ctrl+w' -Function BackwardDeleteWord
        Set-PSReadLineKeyHandler -Chord 'Alt+d' -Function DeleteWord
        Set-PSReadLineKeyHandler -Chord 'Ctrl+LeftArrow' -Function BackwardWord
        Set-PSReadLineKeyHandler -Chord 'Ctrl+RightArrow' -Function ForwardWord
        Set-PSReadLineKeyHandler -Chord 'Ctrl+z' -Function Undo
        Set-PSReadLineKeyHandler -Chord 'Ctrl+y' -Function Redo
    }
}

# ==== Load Environment Variables ==========================

$env:AWS_PROFILE = 'default'

# ==== Prompt Customization =================================

function prompt {
    $user = $env:USERNAME
    $host_ = $env:COMPUTERNAME
    $cwd = Split-Path -Leaf (Get-Location)

    $branch = ""
    if (Test-Path ".git" -ErrorAction SilentlyContinue) {
        $b = git rev-parse --abbrev-ref HEAD 2>$null
        if ($b) { $branch = " [$b]" }
    }

    $m = "`e[35m"   # Magenta
    $b = "`e[34m"   # Blue
    $y = "`e[33m"   # Yellow
    $g = "`e[32m"   # Green
    $w = "`e[37m"   # White
    $r = "`e[0m"    # Reset

    "`n${m}${user}${r} ${w}on${r} ${b}${host_}${r} ${w}at${r} ${y}${cwd}${r}${g}${branch}${r}`n${m}${r} "
}

# ==== Startup Banner =======================================

$StartupTimer.Stop()
$msg = " profile loaded · took $($StartupTimer.ElapsedMilliseconds)ms "
$width = $Host.UI.RawUI.WindowSize.Width
$remain = $width - $msg.Length - 4  # 2 for "── " prefix + 2 for " ▪" suffix

if ($remain -gt 0) {
    $line = "──$msg" + ('─' * $remain) + ' ▪'
    Write-Host $line -ForegroundColor DarkGray
}