Table of Contents
Using WindowsPrincipal
and WindowsIdentity
Classes
Use the WindowsPrincipal
and WindowsIdentity
Classes to check if PowerShell runs as an admin.
1 2 3 4 5 6 7 8 9 10 11 |
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent() ) $isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if ($isAdmin) { Write-Host "PowerShell is running as admin." } else { Write-Host "PowerShell is not running as admin." } |
1 2 3 |
PowerShell is running as admin. |
The above code returned PowerShell is running as admin
if the PowerShell is running as an administrator; otherwise, it returned PowerShell is not running as admin
.
Let’s break down the code to understand it.
First, we used the New-Object
cmdlet to create a new WindowsPrincipal
object based on the current Windows identity, representing the user running the PowerShell session; we stored the reference of this object in the $currentPrincipal
variable.
Then, we invoked the IsInRole()
method on the $currentPrincipal
object (created in the previous step) and passed the WindowsBuiltInRole.Administrator
enumeration value as an argument to determine if the current user belongs to the Administrators Group.
If the IsInRole()
method returned True
, the current user ran PowerShell as an administrator. On the other hand, if it returned False
, the current user was not running PowerShell as an administrator.
We used the WindowsPrincipal
and WindowsIdentity
classes from the System.Security.Principal
namespace for the above code snippet to check if the current user has admin privileges based on group membership of a user’s Windows identity.
We can also use the WindowsIdentity
class of the System.Security
namespace with the -match
operator to check if PowerShell runs with admin privileges (a.k.a. "elevated rights").
1 2 3 4 5 6 7 8 |
$isAdmin = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544") if ($isAdmin) { Write-Host "PowerShell is running as admin." } else { Write-Host "PowerShell is not running as admin." } |
1 2 3 |
PowerShell is running as admin. |
In the above code example, [bool]
cast the result to a bool
value. Then, the GetCurrent()
method of the WindowsIdentity
class retrieved the Windows identity for the currently running user.
Next, the .groups
was used to access the group
property of identity to determine what user groups the identity is a member of. Finally, the -match "S-1-5-32-544"
assess if groups
have the Well Known SID of the Administrators group, the identity will only contain it if "run as administrator"
option was used.
Using net user
Command
Use the net user
command to check if PowerShell is running as an admin.
1 2 3 |
net user mehvish |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
User name Mehvish Full Name Comment User's comment Country/region code 000 (System Default) ... ... ... ... ... ... Local Group Memberships *Administrators Global Group memberships *None The command was completed successfully. |
We used the net user
command to check if the current user is in the Administrative Group. Don’t forget to replace mehvish
with your username while using the net user
command in PowerShell. Using this command, we will get many details from which we need to locate the Local Group Memberships
section. For example, if it includes "Administrators"
, then it means that the current user is a member of the Administrators group and PowerShell is running with admin privileges.
You can also check admin privileges using the
Get-ExecutionPolicy
command. If the execution policy is other than"Restricted"
, then PowerShell is running as an admin. Note the"Restricted"
execution policy denotes a non-administrative context.
Using Requires
Statement
If you are using PowerShell version 4.0 or above, you can use the Requires
statement at the top of your script to prevent the script from running as a regular user. See the following example.
1 2 3 |
#Requires -RunAsAdministrator |
Now, run two PowerShell processes, first, with admin privileges (elevated) and second, without admin privileges (non-elevated). In both processes, move to the directory where your script file is and run it.
In our case, we have the testPS.ps1
file containing the #Requires -RunAsAdministrator
statement only, and it is in the E:\Test\Script Files directory. So, we will run our script file as ./testPS.ps1
where the dot means current directory because we navigated to the E:\Test\Script Files directory.
We will not get any error when running the testPS.ps1
file from the elevated PowerShell process, but we got the following error when running the testPS.ps1
file from a non-elevated PowerShell process.
1 2 3 4 5 6 7 8 9 10 |
./testPS.ps1 : The script 'testPS.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 using the Run as Administrator option, then try rerunning the script. At line:1 char:1 + ./testPS.ps1 + ~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (testPS.ps1:String) [], ScriptRequiresException + FullyQualifiedErrorId : ScriptRequiresElevation |
That’s all about how to check if PowerShell is running as Admin.