Skip to content

Instantly share code, notes, and snippets.

@foxpy
Last active May 5, 2020 01:33
Show Gist options
  • Save foxpy/487cae016ee3bd4ca4275068ece3fdb5 to your computer and use it in GitHub Desktop.
Save foxpy/487cae016ee3bd4ca4275068ece3fdb5 to your computer and use it in GitHub Desktop.
My custom PowerShell profile. Run with ` -nologo` command line argument. WARNING: Save in Windows-1252!
# Popular Unix shell command `fortune`
# Download `fortune.txt` from https://www.bgreco.net/fortune.txt
# and place it in PowerShell profile folder
function fortune() {
[System.IO.File]::ReadAllText((Split-Path $profile)+'\fortune.txt') -replace "`r`n", "`n" -split "`n%`n" | Get-Random
}
# CPU temperature taken from MSACPI (run command as admin)
# Thanks https://gist.github.com/jeffa00/9577816
function Get-Temperature() {
$currentTemp = Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi"
$returnTemp = @()
foreach ($temp in $currentTemp.CurrentTemperature) {
$currentTempCelsius = [convert]::ToInt32($temp / 10 - 273.15)
$returnTemp += $currentTempCelsius.toString() + " °C"
}
return $returnTemp
}
# Determine if PowerShell launched with admin priveleges
# Thanks https://stackoverflow.com/questions/9999963/powershell-test-admin-rights-within-powershell-script#10000292
function Test-IsAdmin() {
try {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
} catch {
throw "Failed to determine if the current user has elevated privileges. The error was: '{0}'." -f $_
}
}
# Ovverides default `prompt` function which is called
# every time you see PowerShell command line invitation
function prompt() {
Write-Host -NoNewLine -ForegroundColor Cyan (Get-Location)
foreach ($entry in (Get-Location -Stack)) {
Write-Host -NoNewLine -ForegroundColor Green '+'
}
if (Test-IsAdmin) {
Write-Host -NoNewLine -ForegroundColor Red '#'; ' '
}
else {
Write-Host -NoNewLine -ForegroundColor Cyan '>'; ' '
}
}
# Custom PowerShell greeting and window title
$ui = (Get-Host).UI.RawUI
If (Test-IsAdmin) {
$ui.WindowTitle = "Administrator: M$ PowerShell"
} else {
$ui.WindowTitle = "M$ PowerShell"
}
Write-Host 'PowerShell' $PsVersionTable.PSVersion '-' (Get-Date)
Write-Host ''
Write-Host (fortune)
Write-Host ''
Write-Host ''
# Remove useless ugly beep
Set-PSReadlineOption -BellStyle None
# Close terminal on EOF
Set-PSReadlineKeyHandler -Chord 'Ctrl+D' -ScriptBlock { Stop-Process -Id $PID }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment