Last active
November 30, 2016 14:09
-
-
Save thephilip/c499e960060b6e3edeebf1a6e7e79dc4 to your computer and use it in GitHub Desktop.
Custom 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
# ----------------------------- | |
# Custom PowerShell Profile | |
# By: Philip Smith | |
# ----------------------------- | |
# Requires -Version 3 | |
# ----------------------------- | |
# Customizable Variables | |
# ----------------------------- | |
$allow_auto_update = $false # allow this sript to update itself | |
$use_custom_prompt = $true # allow custom prompt | |
#$prompt_symbol = "%" # trailing character in prompt (uncomment to change) | |
$accent_1 = "DarkCyan" # accent/misc color | |
$accent_2 = "Gray" # accent/misc color | |
$accent_3 = "DarkGreen" # accent/misc color | |
$window_title = "{-_-}" # default: "Windows PowerShell" | |
$buffer_height = 2000 # default: 3000 | |
$buffer_width = 100 # default: 120 | |
$start_dir = $home # $env:userprofile | |
$shell_bg = "Black" # main background | |
$shell_fg = "White" # main foreground | |
$verbose_bg = "DarkGray" # verbose backgroun | |
$verbose_fg = "Blue" # verbose foreground | |
$warn_bg = "Yellow" # warning background | |
$warn_fg = "Black" # warning foreground | |
$err_bg = "Red" # error background | |
$err_fg = "Black" # error foreground | |
# ----------------------------- | |
# Reusable objects | |
# ----------------------------- | |
$remote_path = "" #remote file path | |
$local_path = "$env:USERPROFILE\documents\windowspowershell" | |
$shell = $Host.UI.RawUI | |
$buffer = $shell.BufferSize | |
$colors = $host.PrivateData | |
$me = ([Environment]::UserDomainName + "\" + [Environment]::UserName) | |
# ----------------------------- | |
# RawUI Modifications | |
# ----------------------------- | |
if ($shell_fg) { $shell.ForegroundColor = $shell_fg } | |
if ($shell_bg) { $shell.BackgroundColor = $shell_bg } | |
if ($window_title) { $shell.WindowTitle=$window_title } | |
if ($buffer_height) { $buffer.Height = $buffer_height } | |
if ($buffer_width) { $buffer.Width = $buffer_width } | |
$shell.BufferSize = $buffer | |
# ----------------------------- | |
# PrivateData Modifications | |
# ----------------------------- | |
if ($verbose_fg) { $colors.VerboseForegroundColor = $verbose_fg } | |
if ($verbose_bg) { $colors.VerboseBackgroundColor = $verbose_bg } | |
if ($warn_fg) { $colors.WarningForegroundColor = $warn_fg } | |
if ($warn_bg) { $colors.WarningBackgroundColor = $warn_bg } | |
if ($err_fg) { $colors.ErrorForegroundColor = $err_fg } | |
if ($err_bg) { $colors.ErrorBackgroundColor = $err_bg } | |
# ----------------------------- | |
# Override Default Prompt | |
# ----------------------------- | |
if ($use_custom_prompt) { | |
function Prompt { | |
if (-not $prompt_symbol) { | |
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(` | |
[Security.Principal.WindowsBuiltInRole] "Administrator")) { | |
$prompt_symbol = "#" | |
} else { | |
$prompt_symbol = "$" | |
} | |
} | |
# add current time | |
Write-Host ("[") -NoNewLine -ForegroundColor $accent_1 | |
Write-Host (Get-Date -format HH:mm:ss) -NoNewLine -ForegroundColor $accent_2 | |
Write-Host ("][") -NoNewLine -ForegroundColor $accent_1 | |
# add working directory | |
Write-Host ($PWD) -NoNewLine -ForegroundColor $accent_2 | |
Write-Host ("|") -NoNewLine -ForegroundColor $accent_1 | |
# add number of objects in current directory | |
Write-Host (Get-Childitem $PWD -Force).Length -NoNewLine -ForegroundColor $accent_2 | |
Write-Host ("]") -NoNewLine -ForegroundColor $accent_1 | |
# set the prompt symbol | |
Write-Host ($prompt_symbol) -NoNewLine -ForegroundColor $accent_3 | |
return " "; | |
} | |
} | |
# ----------------------------- | |
# Custom Helper Functions | |
# ----------------------------- | |
function Get-Colors { | |
param([Switch] $Names) | |
if ($Names) { | |
[enum]::GetValues([System.ConsoleColor]) | Foreach-Object { Write-Host $_ -ForegroundColor $_ } | |
break | |
} | |
for ($bg = 0; $bg -lt 0x10; $bg++) { | |
for ($fg = 0; $fg -lt 0x10; $fg++) { | |
Write-Host -NoNewLine -Background $bg -Foreground $fg ` | |
(" {0:X}{1:X} " -f $bg,$fg) | |
} | |
} | |
} | |
function Get-Uptime { | |
param([String] $ComputerName = $env:COMPUTERNAME) | |
$os = Get-WmiObject -ComputerName $ComputerName -Class Win32_OperatingSystem -ErrorAction SilentlyContinue | |
$uptime = (Get-Date) - $os.ConvertToDateTime($os.LastBootUpTime) | |
Write-Host "" | |
Write-Host ("Booted:") -NoNewLine -Foreground $warn_fg -Background $accent_1 | |
Write-Host (" " + $os.ConvertToDateTime($os.LastBootUpTime)) -Foreground $accent_3 | |
Write-Host ("Uptime:") -NoNewLine -Foreground $warn_fg -Background $accent_1 | |
Write-Host (" " + $uptime.Days + "d " + $uptime.Hours + "h " + $uptime.Minutes + "m") -Foreground $accent_3 | |
Write-Host "" | |
} | |
function Install-Chocolatey { | |
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) | |
} | |
function Reboot-PC { | |
param( | |
[String] $File, | |
[String] $ComputerName = $env:COMPUTERNAME | |
) | |
if ($File) { | |
Get-Content $File | Foreach-Object { | |
Get-WMIObject Win32_OperatingSystem -comp $_ | |
Foreach-Object { $_.Reboot() } | |
} | |
break | |
} | |
Get-WMIObject Win32_OperatingSystem -comp $ComputerName | Where-Object { $_.Reboot() } | |
} | |
function Get-UpdateStatus { | |
Write-Host ("RemoteUpdated: " + $(Get-Item $remote_path\profile.ps1).LastWriteTime) | |
Write-Host ("LocalUpdated: " + $(Get-Item $local_path\profile.ps1).LastWriteTime) | |
if ($(Get-Item $remote_path\profile.ps1).LastWriteTime -lt $(Get-Item $local_path\profile.ps1).LastWriteTime) { | |
return $true | |
} | |
return $false | |
} | |
# ----------------------------- | |
# Custom Command Aliases | |
# ----------------------------- | |
Set-Alias -Name uptime -Value Get-Uptime | |
Set-Alias -Name sudo -Value Get-PrivilegeEscalation | |
Set-Alias -Name Get-IPAddress -Value Get-NetIPAddress | |
# ----------------------------- | |
# Check for Updates | |
# ----------------------------- | |
$current = Get-UpdateStatus | |
Write-Host ("Software Current? " + $current) | |
if ($allow_auto_update) { | |
$backed_up = Test-Path $local_path\profile.backup | |
if ($current -ne $true) { | |
Write-Host "Installing available update!" | |
if ($backed_up) { | |
Remove-Item $local_path\profile.backup -Force | |
} | |
Copy-Item $local_path\profile.ps1 $local_path\profile.backup | |
Copy-Item $remote_path\profile.ps1 $local_path\profile.ps1 | |
$(Get-Item $local_path\profile.ps1).LastWriteTime = [DateTime]::UtcNow | |
$updated = $true | |
} | |
} | |
# ----------------------------- | |
# Console Startup Banner | |
# ----------------------------- | |
Clear-Host # comment for debug messages | |
Set-Location $start_dir | |
if ($updated) { | |
Write-Host ("`nCustom profile updated! Please restart") -Foreground $warn_bg | |
Write-Host ("PowerShell to apply and use new features.") -Foreground $warn_bg | |
} | |
Write-Host ("`n============================`n") -Foreground $accent_1 -Background $shell_bg | |
Write-Host ("WhoAmI:") -NoNewLine -Foreground $warn_fg -Background $accent_1 | |
Write-Host (" " + $me) -Foreground $accent_3 | |
Write-Host ("PCName:") -NoNewLine -Foreground $warn_fg -Background $accent_1 | |
Write-Host (" " + $env:COMPUTERNAME) -Foreground $accent_3 | |
Write-Host ("Domain:") -NoNewLine -Foreground $warn_fg -Background $accent_1 | |
Write-Host (" " + $env:USERDNSDOMAIN) -NoNewLine -Foreground $accent_3 | |
Get-Uptime | |
Write-Host ("============================`n") -Foreground $accent_1 -Background $shell_bg | |
# ----------------------------- | |
# End of Custom Profile | |
# ----------------------------- | |
# Chocolatey profile | |
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" | |
if (Test-Path($ChocolateyProfile)) { | |
Import-Module "$ChocolateyProfile" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment