Created
December 20, 2018 19:35
-
-
Save atbradley/256e83efb78b78d287e5f75794cfd431 to your computer and use it in GitHub Desktop.
Custom PowerShell prompt with current time and available drive space.
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 PrettySize([int64]$bytes) { | |
if ( $bytes -gt 1TB ) { $result = [string]::Concat([math]::Round($bytes/1TB, 2), " TB") } | |
elseif ( $bytes -gt 1GB ) { $result = [string]::Concat([math]::Round($bytes/1GB, 2), " GB") } | |
elseif ( $bytes -gt 1MB ) { $result = [string]::Concat([math]::Round($bytes/1MB, 2), " MB") } | |
elseif ( $bytes -gt 1KB ) { $result = [string]::Concat([int]($bytes/1KB), " KB") } | |
$result | |
} | |
Function prompt { | |
# .Description | |
# This custom version of the PowerShell prompt will present a colorized location value based on the current provider. It will also display the PS prefix in red if the current user is running as administrator, and list available space on drives C: and D:. Based on https://jdhitsolutions.com/blog/powershell/5958/friday-fun-perk-up-your-powershell-prompt/ | |
# .Link | |
# https://go.microsoft.com/fwlink/?LinkID=225750 | |
# .ExternalHelp System.Management.Automation.dll-help.xml | |
$user = [Security.Principal.WindowsIdentity]::GetCurrent() | |
if ( (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) { | |
$adminfg = "Red" | |
} | |
else { | |
$adminfg = $host.ui.rawui.ForegroundColor | |
} | |
$drives = Get-WMIObject -Class Win32_Volume | Select-Object -property DriveLetter,@{Label=’Size’;Expression={PrettySize($_.Capacity)}},@{Label=’FreeSpace’;Expression={PrettySize($_.FreeSpace)}},@{Label=’PctFree’;Expression={[string]::Concat([math]::round($_.FreeSpace / $_.Capacity * 100, 2), '%')}} | Sort-Object DriveLetter | Where-Object {$_.DriveLetter -eq 'C:' -or $_.DriveLetter -eq 'D:'} | |
foreach ( $drive in $drives ) { | |
$driveinfo += "$($drive.DriveLetter) $($drive.FreeSpace) ($($drive.PctFree)); " | |
} | |
Switch ((get-location).provider.name) { | |
"FileSystem" { $fg = "green"} | |
"Registry" { $fg = "magenta"} | |
"wsman" { $fg = "cyan"} | |
"Environment" { $fg = "yellow"} | |
"Certificate" { $fg = "darkcyan"} | |
"Function" { $fg = "gray"} | |
"alias" { $fg = "darkgray"} | |
"variable" { $fg = "darkgreen"} | |
Default { $fg = $host.ui.rawui.ForegroundColor} | |
} | |
Write-Host "[$((Get-Date).timeofday.tostring().substring(0,8))] " -NoNewline | |
Write-Host "PS " -nonewline -ForegroundColor $adminfg | |
Write-Host $driveinfo -NoNewline | |
Write-Host "$($executionContext.SessionState.Path.CurrentLocation)" -foregroundcolor $fg -nonewline | |
Write-Output "$('>' * ($nestedPromptLevel + 1)) " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment