Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check to see if the currently logged-in user is in a specific group?

I am attempting to create a batch script that changes whether or not a user is in a specific group on Active Directory. I know how to get a list of groups the user is in with this line:

whoami \groups

What I don't know is how to query that list and run a command based on whether or not a certain group is in that list. I'm looking for something along the lines of:

set %groups% whoami \groups
if foo in %groups% echo run

This doesn't run (obviously), but I expect the output to be "run" if a group named foo is in the list of groups obtained by whoami \groups.

like image 738
Brandon Olson Avatar asked Oct 23 '25 23:10

Brandon Olson


1 Answers

Try like this:

@echo off
set "groupToCheck=BUILTIN\Administrators"

whoami /groups | findstr /B "%groupToCheck:\=\\%\>" >nul 2>&1 && (
  echo I'm in %groupToCheck%
) || (
  echo I'm NOT in %groupToCheck%
)
like image 165
npocmaka Avatar answered Oct 27 '25 02:10

npocmaka



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!