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.
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%
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With