Here's a simple way to tell if you're running as an administrator from within PowerShell:
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $windowsPrincipal = new-object 'System.Security.Principal.WindowsPrincipal' $windowsIdentity return $windowsPrincipal.IsInRole("Administrators")
I like to use this right at the top of my startup profile to turn my window background dark red:
if ((get-isadmin) -and (-not $psISE)) { $Host.UI.RawUI.BackgroundColor = "DarkRed" clear }
Since the raw UI can only use colors from the console, I also tweak my console's interpretation of dark red into (48,0,0) so that it's very dark, nearly black but still recognizably red. I also change my prompt color from green to yellow. It's very obvious looking at green on black vs. yellow on red that I'm in an admin prompt.
I check to make sure we're not running in the ISE, because the ISE runs a dark on light color scheme and doesn't paint the same way as the console version of PowerShell.
FWIW if you use PSCX you can just check $pscx:isadmin
Posted by: james | June 03, 2010 at 22:25
Pretty nice post.In any case I’ll be subscribing to your feed and I hope you write again soon! I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts.
http://www.laptop-battery-chargers.com/hp-pavilion-dv6500.html hp dv6500 battery
Posted by: charger | June 07, 2010 at 01:03