windowspowershellbatch-file

PowerShell command to check whether an application is running with elevated privileges


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?


Solution

  • 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.