My PowerShell profile

Your PowerShell profile gives you the power to customize the console at run time. This article explores my customization that make my PowerShell sessions feel like home.

Requirements

There are four requirements to make my setup work:

  • [module] PSReadline – is included by default in PowerShell 5.0+.
  • [module] AdvancedHistory – this is a module I wrote that adds keyboard navigable history to the console.
  • [module] Posh-Git – adds the ability to show Git status as part of your prompt and adds tab-complete to Git commands and parameters. (requires some tweaking, see below)
  • [font] DejaVu Sans Mono – any Powerline font will work but I personally use DejaVu Sans Mono.

My Colour Scheme

Unfortunately setting the colours in PowerShell is tedious and it’s not easy to export or share the settings. To set these colours you will need to open PowerShell, click the logo in the top right corner of the window, select Properties, and then Colors. Click each colour going from left-to-right and use these values:

  • Black: 35, 51, 68
  • DarkBlue: 50, 117, 255
  • DarkGreen: 68, 190, 136
  • DarkCyan: 20, 133, 156
  • DarkRed: 170, 59, 43
  • DarkMagenta: 136, 68, 190
  • DarkYellow: 185, 191, 81
  • Gray: 232, 238, 251
  • DarkGray: 139, 142, 150
  • Blue: 102, 151, 255
  • Green: 85, 238, 170
  • Cyan: 29, 191, 224
  • Red: 213, 74, 54
  • Magenta: 170, 85, 238
  • Yellow: 232, 239, 102
  • White: 255, 255, 255 (default)

Before clicking “OK” make sure you have the “Screen Background” radio selected and click the furthest colour on the left (“Black.”)

On Windows the colour settings are stored in the actual shortcut used to launch the current session and can be copied between locations.

  • Start menu shortcuts: AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell
  • Taskbar shortcuts: AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar

My Profile

Editing the Profile

There are multiple profiles for PowerShell and PowerShell ISE but the one we’re concerned with is the PowerShell console profile specific to the current user. The location of the user profile is assigned to the variable $PROFILE and can be created and opened from the command-line:

PS C:\Users\omniomi> Test-Path $PROFILE
False

PS C:\Users\omniomi> New-Item -path $PROFILE -type file –force

    Directory: C:\Users\omniomi\Documents\WindowsPowerShell

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 6/14/2018 9:21 AM 0 Microsoft.PowerShell_profile.ps1

PS C:\Users\omniomi> code $PROFILE
  • If the Test-Path command comes back True you already have a profile and can skip the New-Item command.
  • You can substitute “code” with “ise” or any editor of your choice.

My Profile

Before starting:make sure to set the font within PowerShell to DejaVu or whichever Powerline font you selected.

Set-Location c:\ ; cls

# Prompt
Import-Module Posh-Git
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { $Script:IsAdmin = $true }

function prompt {
    if (((Get-Item $pwd).parent.parent.name)) {
        $Path = '..\' + (Get-Item $pwd).parent.name + '\' + (Split-Path $pwd -Leaf)
    } else {
        $Path = $pwd.path
    }

    if($Script:IsAdmin) {
        Write-Host " $([char]0xE0A2)" -ForegroundColor Black -BackgroundColor Green -NoNewline
        Write-Host "$([char]0xE0B0)$([char]0xE0B1)" -ForegroundColor Green -BackgroundColor DarkBlue -NoNewline
    }

    Write-Host " $($MyInvocation.HistoryId)" -ForegroundColor white -BackgroundColor DarkBlue -NoNewline
    Write-Host "$([char]0xE0B0)$([char]0xE0B1) " -ForegroundColor DarkBlue -BackgroundColor Cyan -NoNewline
    Write-Host ($path).ToLower().TrimEnd('\') -ForegroundColor White -BackgroundColor Cyan -NoNewline
    if ((Write-VcsStatus *>&1).Length -gt 0) {
        Write-Host "$([char]0xE0B0)$([char]0xE0B1)" -ForegroundColor Cyan -BackgroundColor DarkGray -NoNewline
        Write-Host (Write-VcsStatus) -NoNewline -BackgroundColor DarkGray
        Write-Host "$([char]0xE0B0)$("$([char]0xE0B1)" * $NestedPromptLevel)" -ForegroundColor DarkGray -NoNewline
    } else {
        Write-Host "$([char]0xE0B0)$("$([char]0xE0B1)" * $NestedPromptLevel)" -ForegroundColor Cyan -NoNewline
    }
    ' '
}

# Aliases
function ll {
    Get-ChildItem -Force $args
}

# Readline options
## Tab completion
Set-PSReadlineKeyHandler -Key Tab -Function Complete
Set-PSReadlineOption -ShowToolTips

## Colours
Set-PSReadlineOption -TokenKind Command -ForegroundColor Blue
Set-PSReadlineOption -TokenKind Parameter -ForegroundColor DarkBlue
Set-PSReadlineOption -TokenKind Comment -ForegroundColor Green
Set-PSReadlineOption -TokenKind Operator -ForegroundColor Gray
Set-PSReadlineOption -TokenKind Variable -ForegroundColor Magenta
Set-PSReadlineOption -TokenKind Keyword -ForegroundColor Magenta
Set-PSReadlineOption -TokenKind String -ForegroundColor DarkGray
Set-PSReadlineOption -TokenKind Type -ForegroundColor DarkCyan

# AdvancedHistory
Import-Module AdvancedHistory
Enable-AdvancedHistory -Unique

Tweaking Posh-Git

Finally, in order to make Posh-Git fit into the prompt naturally we need to make changes to GitPrompt.ps1 in the Posh-Git installation directory (C:\Program Files\WindowsPowerShell\Modules\posh-git\0.7.3  if installed for all user on Windows.)

Replace lines 5-86 with this:

    DefaultForegroundColor                      = $null

    BeforeText                                  = " $([char]0xE0A0) "
    BeforeForegroundColor                       = [ConsoleColor]::Yellow
    BeforeBackgroundColor                       = [ConsoleColor]::DarkGray

    DelimText                                   = ' |'
    DelimForegroundColor                        = [ConsoleColor]::Yellow
    DelimBackgroundColor                        = $null

    AfterText                                   = ''
    AfterForegroundColor                        = [ConsoleColor]::Yellow
    AfterBackgroundColor                        = [ConsoleColor]::DarkGray

    FileAddedText                               = '+'
    FileModifiedText                            = '~'
    FileRemovedText                             = '-'
    FileConflictedText                          = '!'

    LocalDefaultStatusSymbol                    = $null
    LocalDefaultStatusForegroundColor           = [ConsoleColor]::White
    LocalDefaultStatusForegroundBrightColor     = [ConsoleColor]::White
    LocalDefaultStatusBackgroundColor           = [ConsoleColor]::DarkGray

    LocalWorkingStatusSymbol                    = '!'
    LocalWorkingStatusForegroundColor           = [ConsoleColor]::DarkRed
    LocalWorkingStatusForegroundBrightColor     = [ConsoleColor]::DarkRed
    LocalWorkingStatusBackgroundColor           = [ConsoleColor]::DarkGray

    LocalStagedStatusSymbol                     = '~'
    LocalStagedStatusForegroundColor            = [ConsoleColor]::White
    LocalStagedStatusBackgroundColor            = [ConsoleColor]::DarkGray

    BranchUntrackedSymbol                       = $null
    BranchForegroundColor                       = [ConsoleColor]::White
    BranchBackgroundColor                       = [ConsoleColor]::DarkGray

    BranchGoneStatusSymbol                      = [char]0x00D7 # Multiplication sign
    BranchGoneStatusForegroundColor             = [ConsoleColor]::DarkGray
    BranchGoneStatusBackgroundColor             = [ConsoleColor]::DarkGray

    BranchIdenticalStatusToSymbol               = [char]0x2261 # Three horizontal lines
    BranchIdenticalStatusToForegroundColor      = [ConsoleColor]::Yellow
    BranchIdenticalStatusToBackgroundColor      = [ConsoleColor]::DarkGray

    BranchAheadStatusSymbol                     = [char]0x2191 # Up arrow
    BranchAheadStatusForegroundColor            = [ConsoleColor]::White
    BranchAheadStatusBackgroundColor            = [ConsoleColor]::DarkGray

    BranchBehindStatusSymbol                    = [char]0x2193 # Down arrow
    BranchBehindStatusForegroundColor           = [ConsoleColor]::DarkRed
    BranchBehindStatusBackgroundColor           = [ConsoleColor]::DarkGray

    BranchBehindAndAheadStatusSymbol            = [char]0x2195 # Up & Down arrow
    BranchBehindAndAheadStatusForegroundColor   = [ConsoleColor]::Yellow
    BranchBehindAndAheadStatusBackgroundColor   = [ConsoleColor]::DarkGray

    BeforeIndexText                             = ""
    BeforeIndexForegroundColor                  = [ConsoleColor]::White
    BeforeIndexForegroundBrightColor            = [ConsoleColor]::White
    BeforeIndexBackgroundColor                  = [ConsoleColor]::DarkGray

    IndexForegroundColor                        = [ConsoleColor]::White
    IndexForegroundBrightColor                  = [ConsoleColor]::White
    IndexBackgroundColor                        = [ConsoleColor]::DarkGray

    WorkingForegroundColor                      = [ConsoleColor]::DarkRed
    WorkingForegroundBrightColor                = [ConsoleColor]::DarkRed
    WorkingBackgroundColor                      = [ConsoleColor]::DarkGray

    EnableStashStatus                           = $false
    BeforeStashText                             = ' ('
    BeforeStashBackgroundColor                  = [ConsoleColor]::DarkGray
    BeforeStashForegroundColor                  = [ConsoleColor]::DarkRed
    AfterStashText                              = ')'
    AfterStashBackgroundColor                   = [ConsoleColor]::DarkGray
    AfterStashForegroundColor                   = [ConsoleColor]::DarkRed
    StashBackgroundColor                        = [ConsoleColor]::DarkGray
    StashForegroundColor                        = [ConsoleColor]::DarkRed

    ErrorForegroundColor                        = [ConsoleColor]::DarkRed
    ErrorBackgroundColor                        = [ConsoleColor]::DarkGray