Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 6 down vote accepted

Have a look at:

share|improve this answer
$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
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.

share|improve this answer
This only determines if the user running the script is an administrator on the machine -- and not if the script is currently being executed with administrative privileges. In other words, this will still return true even if the user did not use "run as administrator" to launch the command shell. –  Holistic Developer Oct 17 '14 at 19:34
@HolisticDeveloper, that is incorrect. If you aren't elevated, it will return false –  charleswj81 Jan 8 at 16:33

In Powershell 4.0 you can use requires at the top of your script:

#Requires -RunAsAdministrator


The script 'MyScript.ps1' cannot be run because it contains a "#requires" statement for running as Administrator. The current Windows PowerShell session is not running as Administrator. Start Windows PowerShell by using the Run as Administrator option, and then try running the script again.

share|improve this answer
Not exactly what I was looking for but still very useful. Thanks Eddie! –  Michael Kelley Mar 19 at 17:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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