Created
February 22, 2011 22:46
-
-
Save chilversc/839599 to your computer and use it in GitHub Desktop.
PowerShell profile
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
function Get-HomeLocation { | |
# Using 'Run as administrator' does not set $Home, so try other variables as well | |
if ($variable:Home) { | |
$variable:Home | |
} elseif ($env:Home) { | |
$env:Home | |
} elseif ($env:UserProfile) { | |
$env:UserProfile | |
} else { | |
$null | |
} | |
} | |
function Get-HomeRelativeLocation | |
{ | |
$h = Get-HomeLocation | |
if ($h) { | |
$pwd = [string] (Get-Location) | |
if ($pwd.StartsWith($h)) { | |
'~' + $pwd.Substring($h.Length) | |
} else { | |
$pwd | |
} | |
} else { | |
[string] (Get-Location) | |
} | |
} | |
function Write-LabeledPrompt ($Label) | |
{ | |
if ($nestedpromptlevel -ge 1) { | |
'>>> ' | |
} else { | |
$pwd = Get-HomeRelativeLocation | |
if (Test-Administrator) { | |
Write-Host -NoNewline -ForegroundColor Red "[Admin] " | |
} | |
if (Test-Path variable:/PSDebugContext) { | |
Write-Host -NoNewline -ForegroundColor DarkGreen '[DBG]:' | |
} | |
Write-Host -NoNewline -ForegroundColor DarkGreen "$Label " | |
Write-Host -ForegroundColor DarkYellow ('[' + $pwd + ']') | |
'> ' | |
} | |
} | |
function prompt | |
{ | |
Write-LabeledPrompt 'PS' | |
} | |
function Test-Administrator | |
{ | |
$user = [Security.Principal.WindowsIdentity]::GetCurrent() | |
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) | |
} | |
function touch { | |
param([string] $filename) | |
New-Item -type file $filename | |
} | |
# If the file system has not had its home set (eg using run as administrator) try to set | |
# it using one of the other environmental variables so that commands like 'cd ~' work | |
if (-not (Get-PSProvider 'FileSystem').Home) { | |
$h = Get-HomeLocation | |
if ($h) { | |
(Get-PSProvider 'FileSystem').Home = $h | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Beautiful. I spent WAY more time on this (Run-as-admin doesn't set $HOME or ~) at home last night than I should have. Nice to know I'm not insane. Maybe I missed the env: prefix on $UserProfile.