2

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
bahrep
  • 29,961
  • 12
  • 103
  • 150
  • https://stackoverflow.com/a/11440595/3829407 Tempting dupe but the question is different. That shows how to run elevated if not already. The detection algorithm in the linked answer is what you are asking. – Matt May 23 '18 at 16:35

1 Answers1

7

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)
    
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62