wolfgang ziegler


„make stuff and blog about it“

Red Background for Administrator PowerShell

October 21, 2013

User Account Control (UAC) which was introduced with Windows Vista is a great thing. I am serious, I absolutely appreciate this feature and its contribution to making Windows the safe and solid operating system it is today.

But as always, security has its price and and comes to the disadvantage of usability. You might have noticed that, when you e.g. needed to edit a file in your Windows or Program Files directories and just wouldn’t be allowed to so.

For these kinds of administrative operations, I usually keep an instance of PowerShell open, which runs under administrative credentials.

I am a very visually oriented person and I like this administrative instance of PowerShell to really stick out compared to the other Windows on my desktop. So I gave all administrative instances of PowerShell a screaming red background.

redpowershell

This prevents me from performing stupid and harmful operations inside this PowerShell instance because the red background permanently reminds me of the great responsibility that comes with the great power of administrative credentials.

Show Me The Code!

So how’s this done? It’s implemented in a few lines of code in your PowerShell Profile (which you probably do not have yet, so we will created one). Here’s the steps:

Check if you have a PowerShell profile:

PS C:\> test-path $profile
False

OK, so let’s create one, and check for its existence again:

PS C:\> new-item $profile -force -type file
PS C:\> test-path $profile
True

Awesome! Let’s open the profile with it’s default program using the invoke-item (short: ii) PowerShell command:

PS C:\> ii $profile

That opens an empty notepad document on my PC. Now, copy these lines into the file and we are done:

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

The next time you open an administrative PowerShell instance it will have a red background.

Works on my machine!

worksonmymachine