1

I require a PowerShell command or script which will check whether my console application is running as administrator or not. If it is not running as administrator, I will stop it and run again using the script.

If PowerShell command is not available, is there any other way to check this?

Thanks in advance.

Giorgi Chakhidze
  • 3,351
  • 3
  • 22
  • 19
Shivani G
  • 71
  • 2
  • 13
  • Does this answer your question? [Check if logged on user is an administrator when non-elevated](https://stackoverflow.com/questions/29129787/check-if-logged-on-user-is-an-administrator-when-non-elevated) – iRon Dec 10 '21 at 10:56
  • See also [Running a command as Administrator using PowerShell?](https://stackoverflow.com/a/57035712/1701026) – iRon Dec 10 '21 at 11:10
  • 1
    Not exactly. I will be running the script as admin only. So no need to check log on user. I just wanted to check about application, whether running as elevated or not – Shivani G Dec 10 '21 at 11:16
  • Is https://scriptimus.wordpress.com/2015/07/19/powershell-test-isadmin/ useful? – Jeff Zeitlin Dec 10 '21 at 11:45
  • 1
    @jeff Zeitlin I want to check for particular application. – Shivani G Dec 10 '21 at 12:14
  • Checking whether or not a process is running elevated is not the same as checking whether or not an user is a member of Administrators role. These two are quite different operations. – Giorgi Chakhidze Dec 11 '21 at 11:13

1 Answers1

2

You can use my Test-ProcessElevated cmdlet (It is too long, I've posted to GitHub gist).

For example:

# from pipeline:
Get-Process notepad | Test-ProcessElevated

# from parameter:
Test-ProcessElevated $(Get-Process notepad)

# it returns boolean
if (ps notepad | Test-ProcessElevated)
{
    Write-Host 'notepad is running elevated.'
}

# dwm.exe is running in a different session:
ps dwm | Test-ProcessElevated

I've tested it in both Windows PowerShell 5 and PowerShell 7.

Giorgi Chakhidze
  • 3,351
  • 3
  • 22
  • 19
  • 1
    Hey, it is working and giving expected results. Thank you – Shivani G Dec 13 '21 at 05:14
  • I'm getting one exception. Can you please help me out here... Exception calling "IsProcessElevated" with "1" argument (s): "OperProcessToken failed" At line:161 char:16 return [SecurityUtils.Win32]::IsProcessElevated($Process) : NotSpecified: (:) , MethodInvocationException + FullyQualifiedErrorId: Win32Exception + Category Info notepad is not running elevated. – Shivani G Dec 13 '21 at 10:59
  • I just noticed that, when my application is running as elevated it is throwing this exception. And in if condition it is still giving process is not running as elevated. – Shivani G Dec 13 '21 at 12:10
  • Turns out opening a process that is running in a different session is not a simple task , even if you are an Administrator. Additional privileges are required. I'll look into this. – Giorgi Chakhidze Dec 13 '21 at 15:57
  • @ShivaniG I've solved the problem. See the updated script: https://gist.github.com/0xfeeddeadbeef/ce341bbca071099fd6952abab896aaac – Giorgi Chakhidze Dec 13 '21 at 22:10
  • Here's what needed to be changed: https://gist.github.com/0xfeeddeadbeef/ce341bbca071099fd6952abab896aaac/revisions – Giorgi Chakhidze Dec 13 '21 at 22:14