In a PowerShell script, how can I check if I'm running with administrator privlieges?

link|improve this question
feedback

2 Answers

up vote 4 down vote accepted

Have a look at:

http://serverfault.com/questions/11320/command-line-safety-tricks/29261#29261

link|improve this answer
2  
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent() ) &{ if ($currentPrincipal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator )) { (get-host).UI.RawUI.Backgroundcolor="DarkRed" clear-host write-host "Warning: PowerShell is running as an Administrator.`n" } – davey Dec 17 '09 at 20:16
thanks that worked! – Michael Kelley Dec 17 '09 at 20:30
feedback
function Test-Administrator  
{  
    $user = [Security.Principal.WindowsIdentity]::GetCurrent();
    (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)  
}

Execute the above function. IF the a result is True, the user has admin privileges.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.