Created
September 16, 2016 10:57
-
-
Save felixfbecker/bd45d816317a9d9e04bdca7f0564c309 to your computer and use it in GitHub Desktop.
My customized PowerShell prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fix UTF8 Output | |
# [Console]::OutputEncoding = [Text.Encoding]::UTF8 | |
$OutputEncoding = [Text.Encoding]::UTF8 | |
$env:COMPOSER_DISABLE_XDEBUG_WARN = 1 | |
$env:NPM_CONFIG_UNICOE = 'true' | |
# A helper function to switch between multiple versions of PHP, NodeJS, ... | |
function Use-Version($program, $version, $arch) { | |
if (!$arch) { | |
if (Test-Path "C:\Program Files (x86)\$program\$version") { | |
$arch = '32'; | |
} elseif (Test-Path "C:\Program Files\$program\$version") { | |
$arch = '64'; | |
} else { | |
throw "$program $version is not installed"; | |
} | |
} | |
if ($arch -eq '64' -or ${env:ProgramFiles(x86)} -eq $null) { | |
$programDir = $env:ProgramFiles; | |
} elseif ($arch -eq '32') { | |
$programDir = ${env:ProgramFiles(x86)}; | |
} else { | |
throw "Invalid architecture $arch, expected 64 or 32"; | |
} | |
if (!(Test-Path "$programDir\$program\$version")) { | |
throw "$program $version is not installed in $programDir\$program\$version" | |
} | |
# remove any other PATH entries for this version | |
$env:PATH = $env:PATH -ireplace "C:\\Program Files[^\\]*\\$program\\[^;]+[;$]",$null | |
$env:PATH = "$programDir\$program\$version;$env:PATH" | |
} | |
# Import the awesome https://github.com/dahlbyk/posh-git | |
# If module is installed in a default location ($env:PSModulePath), | |
# use this instead (see about_Modules for more information): | |
Import-Module $PSScriptRoot\Modules\posh-git | |
# Set up a bash-like prompt with user, machine, pwd and git info | |
# Example: | |
# | |
# felix@FELIX-PC ~\git\Projekte\whatever [master ≡ +0 ~2 -0 !] | |
# $ | |
# | |
function prompt { | |
$realLASTEXITCODE = $LASTEXITCODE | |
Write-Host '' | |
# Print user@machine | |
Write-Host ($env:USERNAME + '@' + [System.Environment]::MachineName) -NoNewline -ForegroundColor Green | |
Write-Host ' ' -NoNewline | |
# Print PWD with user folder replaced by ~ | |
$path = $pwd.path.replace($env:USERPROFILE, '~') | |
Write-Host($path) -NoNewline #-ForegroundColor Cyan | |
Write-VcsStatus | |
Write-Host '' | |
# Write-Host '$' -NoNewline # -ForegroundColor Green | |
# Write path to tile | |
# Write [Administrator] to the tile if run as admin | |
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
if ($isAdmin) { | |
$host.UI.RawUI.WindowTitle = '[Administrator] ' + $path | |
} else { | |
$host.UI.RawUi.WindowTitle = $path | |
} | |
$global:LASTEXITCODE = $realLASTEXITCODE | |
return "$ " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment