Skip to content

Instantly share code, notes, and snippets.

@enab-dev
Last active August 29, 2015 13:58
Show Gist options
  • Save enab-dev/10023918 to your computer and use it in GitHub Desktop.
Save enab-dev/10023918 to your computer and use it in GitHub Desktop.
Windows Powershell custom profile file
set-alias np C:\Windows\notepad.exe
set-alias subl "C:\Program Files\Sublime Text 3\sublime_text.exe"
set-alias grep select-string
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent() )
$isAdmin = $currentPrincipal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator )
$host.ui.RawUI.BackgroundColor = [ConsoleColor]::Black
function prompt
{
$host.UI.RawUI.ForegroundColor = [ConsoleColor]::Green
$host.UI.RawUI.WindowTitle = (shorten-path (pwd).Path)
if ($isAdmin) { $host.UI.RawUI.WindowTitle = $host.UI.RawUI.WindowTitle + " (Admin)" }
# our theme
$cdelim = [ConsoleColor]::Gray
$chost = [ConsoleColor]::Blue
$cloc = [ConsoleColor]::DarkYellow
$red = [ConsoleColor]::Red
write-host ''
write-host '[' -n -f $cdelim
write-host ([net.dns]::GetHostName()) -n -f $chost
write-host '/' -n -f $cdelim
write-host $env:username -n -f $chost
write-host '] @ ' -n -f $cdelim
write-host (get-date -format "yyyy-MM-dd HH:mm:ss") -f $cloc
write-host '{' -n -f $cdelim
write-host (shorten-path (pwd).Path) -n -f $cloc
write-host '}' -f $cdelim
if ( $isAdmin )
{
write-host '[ADMIN] ' -n -f $red
}
write-host "$([char]0x0A7) " -n -f $cloc
return ' '
}
function shorten-path([string] $path) {
$loc = $path.Replace($HOME, '~')
# remove prefix for UNC paths
$loc = $loc -replace '^[^:]+::', ''
# make path shorter like tabs in Vim,
# handle paths starting with \\ and . correctly
return ($loc -replace '\\(\.?)([^\\])[^\\]*(?=\\)','\$1$2')
}
function Get-IisProcesses() {
gwmi -NS 'root\WebAdministration' -class 'WorkerProcess' | select AppPoolName,ProcessId
}
# Helper Function - convert WMI date to TimeDate object
function WMIDateStringToDate($Bootup) {
[System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup)
}
function Get-Uptime() {
$Computer = "." # adjust as needed
$computers = Get-WMIObject -class Win32_OperatingSystem -computer $computer
foreach ($system in $computers) {
$Bootup = $system.LastBootUpTime
$LastBootUpTime = WMIDateStringToDate($Bootup)
$now = Get-Date
$Uptime = $now - $lastBootUpTime
$d = $Uptime.Days
$h = $Uptime.Hours
$m = $uptime.Minutes
$ms= $uptime.Milliseconds
"System Up for: {0} days, {1} hours, {2}.{3} minutes" -f $d,$h,$m,$ms
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment