How to Check If a PowerShell Script Is Run As Administrator?

If you want to run a PowerShell script with the administrator privileges, you possibly can verify if the present powershell.exe course of has the elevated permissions proper in your PS code.

The following PowerShell code can be utilized to verify if the present script is operating within the “Run as Administrator” mode:

Write-Host "Checking for elevated permissions..."
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInPosition(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))

PowerShell script to Check for Elevated Admin Rights

Save the PowerShell code to the check_process_elevation.ps1 file and run it within the console with out the administrator privileges:


As you possibly can see, the message appeared that you don’t have any administrator privileges, so the PowerShell script has been stopped.

not elevated powerhell session

Now run the script within the elevated PowerShell session . As you possibly can see, the script has detected that this PowerShell session is run as administrator.

check if a powershell process is running as administrator (elevated)

Also you possibly can request elevation proper from the PowerShell script. To do it, as an alternative of the string:

Write-Warning "Insufficient permissions…”

use the next code:

Start-Process Powershell -ArgumentList $PSCommandPath -Verb RunAs

When operating the script with out the administrator privileges, it’ll rerun within the new elevated PowerShell session and you will notice an UAC elevation immediate. If you settle for it, your PS1 script will likely be run as administrator. (The path to the present file of the PowerShell script is transferred utilizing the $PSCommandPath surroundings variable.)

uac promt to elevate powershell script

In PowerShell or newer, it’s even simpler to verify in case your script operating with the administrator privileges. To do it, use the –RunAsAdministrator directive.

#requires -version
#requires –RunAsAdministrator
Write-Host "PowerShell is run as administrator" -ForegroundColor Green

If the script just isn’t run beneath the administrator, the next error will seem:

The script ‘check_process_elevation.ps1’ can't be run as a result of it incorporates a “#requires” assertion for operating as Administrator. The present Windows PowerShell session just isn't operating as Administrator. Start Windows PowerShell through the use of the Run as Administrator possibility, after which strive operating the script once more.
At line:1 char:1
+ C:PScheck_process_elevation.ps1
+ ~~~~~~~~~~~~~~~~~~~~~
+ ClassInfo : PermissionDenied: (check_process_elevation.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresElevation

powershell ScriptRequiresElevation

If you run the script on a pc with PowerShell v2, the next error message will seem:

Cannot course of the “#requires” assertion at line 2 as a result of it isn't within the appropriate format.
The “#requires” assertion have to be in one of many following codecs:
“#requires -shellid ”
“#requires -version ”
“#requires -pssnapin  [-version ]”

To handle Active Directory, chances are you’ll want one other process: to verify if the present person has the area admin privileges from a PowerShell script. Use the next code:

If(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInPosition("Domain Admins"))


Check Also

How to Use Native SSH Client in Windows 10?

The built-in SSH shopper appeared in Windows 10 and Windows Server 2019. Ssh.exe can be …

Leave a Reply

Your email address will not be published. Required fields are marked *