63

How can I tell in my scripts if PowerShell is running with administrator privileges?

I need to know because I'm trying to run a program that requires the ability to open protected ports.

1

5 Answers 5

60
[bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")

Breaking apart what this does:

  • [bool] - Cast the end result to a bool.
  • [System.Security.Principal.WindowsIdentity]::GetCurrent() - Retrieves the WindowsIdentity for the currently running user.
  • (...).groups - Access the groups property of the identity to find out what user groups the identity is a member of.
  • -match "S-1-5-32-544" checks to see if groups contains the Well Known SID of the Administrators group, the identity will only contain it if "run as administrator" was used.
6
  • 2
    Instead of just posting a line of code, can you please explain what it does? This helps future visitors in understanding and adapting it, if necessary.
    – slhck
    May 3, 2014 at 15:53
  • BOOO. Give this man more upvotes Oct 24, 2017 at 18:58
  • 11
    I prefer the answer by @Bill_Stewart below since it is free of magic strings.
    – 8DH
    Apr 20, 2018 at 6:49
  • 3
    Instead of using -match and typecasting: [Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5-32-544' Oct 20, 2019 at 4:23
  • @MaximilianBurszley Nice one! Definitely an improvement over all those other multi-line solutions!
    – not2qubit
    Oct 14, 2020 at 22:55
101
([Security.Principal.WindowsPrincipal] `
  [Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

This retrieves the current Windows identity and returns $true if the current identity has the Administrator role (i.e., is running elevated).

1
  • 18
    While the accepted answer is correct, this answer is much more clear, especially to someone who may read your script six months from now. May 20, 2014 at 19:52
60

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

#Requires -RunAsAdministrator

Outputs:

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.

6
  • 2
    what if you want a function that exits if not ran by admin? Oct 24, 2017 at 18:58
  • 1
    @KolobCanyon - There's no such thing as running only a PowerShell function elevated; the entire PowerShell process is either elevated or not. Nov 5, 2017 at 23:27
  • 1
    @Bill_Stewart yes, but you can return if the user is not admin :) Nov 6, 2017 at 21:39
  • 2
    @KolobCanyon - you can only elevate the PowerShell process; you cannot elevate a single function. That's why the #Requires -RunAsAdministrator is useful: It prevents the entire script from running if you're not elevated. Nov 6, 2017 at 21:45
  • 1
    The requires link isn't working for me. Perhaps about_Requires is the new URL?
    – Kevinoid
    Jul 15, 2020 at 17:57
0

Your code :


invoke-command -computername cavl-ghwwsc3 -command { ([Security.Principal.WindowsPrincipal] 
  [Security.Principal.WindowsIdentity]::GetCurrent()
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)}

is working fine but how to launch it remotely in current user session (not in powershell elevate admin rights because it return my admin isadmin value to remote computer, not current log user if this user isadmin.

0

one more way:

${env:=::} -eq $null

the environment variable =:: is presented only you are NOT running the program as administrator.

This should very fast check as it is only variable value comparison. Also it's not so easy to set variable with a name starting with an = due to the syntax rules ,so this is also reliable.

2
  • 1
    This returns true for none admin instances of Powershell on Windows Terminal
    – GazB
    May 24, 2022 at 9:11
  • @GazB - what's the version of windows that you are using?
    – npocmaka
    May 24, 2022 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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