Check if a user has administrator rights via PowerShell



  • Recluse
  • 24.01.2021
  • 695
  • 0
  • 1
  • 1
  • 0
Check if a user has administrator rights via PowerShell

  • The content of the article

Checking if a user is in the Administrators group

If, for some reason, you need to check whether a user has administrator rights through PowerShell, then the easiest way to do this is by checking his presence in the BUILTIN Administrators group. The reason why this is the simplest, and probably the correct option, is simple – in the case of using other methods (access to the file / registry key, etc.), elevation confirmation via UAC is required. But checking for the presence of a user in a group is just executed in the background and does not require any interaction from the user.

You can make such a check with the following script:

if (whoami /groups /fo csv | convertfrom-csv | where-object { $_.SID -eq "S-1-5-32-544" })
{
    # Ваш код для пользователей с правами администраторов
}
else
{
    # Ваш код для пользователей без прав администратора
}

A trivial example with the launch of Notepad and Calculator:

if (whoami /groups /fo csv | convertfrom-csv | where-object { $_.SID -eq "S-1-5-32-544" })
{
    # Запускаем блокнот для пользователей с правами администратора
    Start-Process 'C:windowssystem32notepad.exe'
}
else
{
    # Запускаем калькулятор для пользователей без прав администратора
    Start-Process 'C:windowssystem32calc.exe'
}

Leave a Reply

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