Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save scriptingstudio/1cc9a68d7315dfd8c0b106e151b2c976 to your computer and use it in GitHub Desktop.

Select an option

Save scriptingstudio/1cc9a68d7315dfd8c0b106e151b2c976 to your computer and use it in GitHub Desktop.
My PowerShell profile
function prompt {
function Get-LastCmdTime {
$diffPromptTime = $null
$lastCmd = Get-History -Count 1
if ($lastCmd -ne $null) {
$diff = $lastCmd.EndExecutionTime - $lastCmd.StartExecutionTime
$msfractional = $diff.TotalMilliseconds - [Math]::Truncate($diff.TotalMilliseconds)
$ms = $msfractional + $diff.Milliseconds
if ($diff.Hours) {'{0}h:{1}m:{2}s:{3:N2}ms' -f $diff.Hours,$diff.Minutes,$diff.Seconds,$ms}
elseif ($diff.Minutes) {'{0}m:{1}s:{2:N2}ms' -f $diff.Minutes,$diff.Seconds,$ms}
elseif ($diff.Seconds) {'{0}s:{1:N2}ms' -f $diff.Seconds,$ms}
else {'{0:N2}ms' -f $diff.TotalMilliseconds}
}
}
$admin = ( ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator') )
$shellprompt = if ($admin) {'PS$>'} else {'PS#>'}
$lastCmd = Get-LastCmdTime
$lastCmd = if ($lastCmd) {"[$lastCmd] "} else {''}
write-host "`n$lastCmd$($executionContext.SessionState.Path.CurrentLocation)" -ForegroundColor DarkCyan
write-host $shellprompt -NoNewline
return " "
}
Set-PSReadLineOption -AddToHistoryHandler {
param([string]$line)
return $line.Length -gt 3 -and $line[0] -ne ' ' -and $line[-1] -ne ';'
}
if ($psVersionTable.PSVersion.Major -eq 5) {
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment