Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check elevated process status?

I would like to find a way to find out if a process is running as elevated or not using Powershell.

Use Case: Being able to run control panel tasks with elevated privilage as local domain user e.g. Add or Remove programs.

Any help will be appreciated.

#Start add or remove as admin
start-process appwiz.cpl -verb runas

#Check if path exists. Answer is Yes, so process is NOT elevated
get-wmiobject -class win32_process | select-object -properties name, path
like image 625
Sumeet Singh Avatar asked Jul 03 '26 19:07

Sumeet Singh


1 Answers

These are the two usual options:

  1. Use the #requires -RunAsAdministrator line in your script (requires PowerShell 3.0 or later). If you use this line at the top of your script, it will throw a terminating error and won't execute if the current process isn't elevated.

  2. Use code like the following to detect whether the current process is elevated:

    $IsElevated = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
    
like image 165
Bill_Stewart Avatar answered Jul 05 '26 10:07

Bill_Stewart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!