Skip to main content
Home
badllama.com
  • Search
  • Log In

Customize your Powershell prompt

cpeters's picture

Wed, 05/25/2016 - 23:46 by cpeters

This jaunty little powershell profile will give you a multicolor prompt, and will change the username red when your running in administrator context.

$Shell = $Host.UI.RawUI

$shell.BackgroundColor = “black”
$shell.ForegroundColor = “cyan”

prompt
Clear-Host


function prompt
{
    $myusername = $env:username
    $myhostname = $env:computername
    $myhostname = $myhostname.tolower()

    $elements = $pwd.Path | %{ $_.split("\") }
    $mypwd = $elements[$elements.Length-1]
    $mycustomprompt = echo `[$myusername`@$myhostname`:$mypwd`]
    If (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
        [Security.Principal.WindowsBuiltInRole] “Administrator”))
    {
        write-host -NoNewLine -ForegroundColor Red  "`[$myusername"
    }
    else
    {
        write-host -NoNewLine -ForegroundColor Green "`[$myusername"
    }
    write-host -NoNewLine -ForegroundColor Yellow "`@$myhostname`:"
    write-host -NoNewLine -ForegroundColor Cyan "$mypwd"
     If (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
         [Security.Principal.WindowsBuiltInRole] “Administrator”))
    {
        write-host -NoNewLine -ForegroundColor Red  "`]`#"
    }
    else
    {
        write-host -NoNewLine -ForegroundColor Green  "`]`$"
    }
    " "
}

Tags: 
customize windows
Powered by Backdrop CMS