Created
August 9, 2019 15:57
-
-
Save BoredComputerguy/9e83326c5a16ec2b980bb11b210a87e0 to your computer and use it in GitHub Desktop.
Powershell profile prompt functions
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
$PromptENV = 'Prod' | |
$Role = "User" | |
$IsAdmin = $False | |
function Test-Admin{ | |
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) | |
$currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
} | |
function Write-ENVPrompt{ | |
Write-Host "[" -NoNewline | |
if($PromptENV -eq 'Prod'){ | |
$Color = 'Red' | |
}elseif($PromptENV -eq 'Dev'){ | |
$Color = 'Yellow' | |
}else{ | |
$Color = 'Gray' | |
} | |
Write-Host -Object $PromptENV -NoNewline -ForegroundColor $Color | |
Write-Host "]" -NoNewline | |
Return "[$PromptENV]" | |
} | |
function Write-PSDrivePrompt{ | |
$DriveName = $ExecutionContext.SessionState.Path.CurrentLocation.Drive.Name | |
Write-host -Object $DriveName -ForegroundColor Cyan -NoNewline | |
Write-host -Object $ExecutionContext.SessionState.Path.CurrentLocation.Path.Replace($DriveName,'') -NoNewline | |
Return $ExecutionContext.SessionState.Path.CurrentLocation.Path | |
} | |
function Write-NetworkDrivePrompt{ | |
$PromptServerpath = ($ExecutionContext.SessionState.Path.CurrentLocation.ProviderPath -split "\\") | |
Write-Host "\\" -NoNewline | |
Write-Host $PromptServerPath[2] -NoNewline -ForegroundColor Cyan | |
$PromptPath = $PromptServerpath |Select -Skip 3 | |
Write-Host "\$($PromptPath -join "\")" -NoNewline | |
Return $ExecutionContext.SessionState.Path.CurrentLocation.ProviderPath | |
} | |
function Write-RolePrompt{ | |
Write-Host "[" -NoNewline | |
if($IsAdmin){ | |
Write-Host -Object $Role -NoNewline -ForegroundColor Red | |
}else{ | |
Write-Host -Object $Role -NoNewline -ForegroundColor Green | |
} | |
Write-Host "]" -NoNewline | |
Return "[$Role]" | |
} | |
if(Test-Admin){ | |
$Role = "Admin" | |
$IsAdmin = $True | |
} | |
Write-Host -ForegroundColor $ForeGroundColor "************** Welcome BoredComputerGuy **************" | |
function prompt{ | |
$RoleTitle = Write-RolePrompt | |
$ENVTitle = Write-ENVPrompt | |
Write-Host -Object "PS " -NoNewline | |
$PromptCurrentLocation = $ExecutionContext.SessionState.Path.CurrentLocation | |
if($Null -eq $PromptCurrentLocation.Drive -and $PromptCurrentLocation.Path -like "Microsoft.PowerShell.Core\FileSystem::\\*"){ | |
$PathTitle = Write-NetworkDrivePrompt | |
}elseif($PromptCurrentLocation.Drive.Name.Length -gt 1){ | |
$PathTitle = Write-PSDrivePrompt | |
}else{ | |
$PathTitle = "$($ExecutionContext.SessionState.Path.CurrentLocation)" | |
Write-Host -Object $PathTitle -NoNewline | |
} | |
"$('>' * ($nestedPromptLevel + 1)) " | |
$host.ui.RawUI.WindowTitle = "$RoleTitle $ENVTitle PS $PathTitle" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment