Here’s a great piece of profile customization: Make your PowerShell session have a dark red background each time it starts a session running as an administrator.

To do this, I look the built in $host variable, which has several properties in it, such as the foreground color and the background color of the shell.

Create Your PowerShell Profile

If you haven’t already created your PowerShell profile, you’ll need to do that before you can edit it.

Once you’ve created your profile you are ready to add this profile trick.

Add This Code To Your Profile

if ($host.UI.RawUI.WindowTitle -match “Administrator”) {$host.UI.RawUI.BackgroundColor = “DarkRed”; $Host.UI.RawUI.ForegroundColor = “White”}

This little gem makes it so easy to know if you’re running as an administrator or not, and makes it so easy to select an active shell that is already running as an administrator if you need to switch over to one.

If you’re just getting into PowerShell, here is what’s happening:

  • It references the “host” variable, which is built in.  You don’t have to create this variable, it’s created when PowerShell starts.  (You can see the object by typing in $host at the PowerShell prompt.)
  • It checks the “WindowTitle” property, which is actually a property of the “RawUI” property. RawUI is actually a property of the “UI” property.
  • These nested properties can be shown and set easily enough:   “$host.ui.rawui.windowtitle”  shows you the current window title.  Where $host.ui.rawui.windowtitle = “Oh yeah, baby! You got it now!!! https://www.ilovepowershell.com” makes your window title look better.
  • We check the UI.RawUI.WindowTitle property to see if the title contains “Administrator” (which it will if it’s in Admin mode).  If it finds it, we change the $host.ui.rawui.backgroundcolor property and the $host.ui.rawui.foregroundcolor property.

Relating To Random-UI

This customization goes hand-in-hand with the random background color profile customization.

This one can be used on its own, if you’re not into setting the background color randomly.  If you ARE the kind of person that has multiple PowerShell windows open, and you like the background colors randomized, then you’re better off implementing them all at once so you don’t set the background and foreground colors more than once.

This is what it looks like to have them both together as part of the same function:

if (!(Get-PSSnapin | ? {$_.name -eq “quest.activeroles.admanagement”})) {Add-PSSnapin quest.activeroles.admanagement}
function random-ui {
if ($host.UI.RawUI.WindowTitle -match “Administrator”) {
$host.UI.RawUI.BackgroundColor = “DarkRed”;
$Host.UI.RawUI.ForegroundColor = “White”
}
else {
$random = New-Object System.Random
switch ($random.Next(5)) {
0 {$host.ui.RawUI.BackgroundColor = “DarkMagenta”; $host.ui.RawUI.ForegroundColor = “White”}
1 {$host.ui.RawUI.BackgroundColor = “Black”; $host.ui.RawUI.ForegroundColor = “Green”}
2 {$host.ui.RawUI.BackgroundColor = “Gray”; $host.ui.RawUI.ForegroundColor = “DarkBlue”}
3 {$host.ui.RawUI.BackgroundColor = “DarkCyan”; $host.ui.RawUI.ForegroundColor = “DarkYellow”}
4 {$host.ui.RawUI.BackgroundColor = “DarkGray”; $host.ui.RawUI.ForegroundColor = “DarkRed”}
}   #end switch
}  #end if-else
}  #end random-ui
random-ui
cls

Party on, Garth!