I have big problem trying to delete all Administrator accounts (beside 2 specyfic) using net localgroups. The problem is that there is no AND operator, so it must be done is some harder way.
for /F "tokens=*" %%G in ('net localgroup administrators') Do (
If %%G == Administrator (goto:ex)
If %%G == MWAdmin (goto:ex)
net localgroup administrators %%G /delete
:ex)
Your issue is the line:
:ex)
This makes no sense to batch, labels cannot be in a code block, as it breaks the block and batch gives up due to a syntax error.
You can solve this by checking the account name outside of the block with call
; as goto :eof
used after call
goes back to the call
, thus allowing for leaving and returning to where it left off in a for loop.
for /f "tokens=*" %%G in ('net localgroup administrators') do (
call :checkName "%%~G"
)
:: If name matches if, go back to for loop, else del.
:checkName
if "%~1" == "Administrator" goto :eof
if "%~1" == "MWAdmin" goto :eof
net localgroup administrators "%~1" /delete
goto :eof
Just in case, lets highlight:
call :label "%variable%"
Using this means that when it goes to :label
, you can use %1
to get %variable%
's value on pass, and ~
can be added with %~1
to remove quotes. This is mainly useful to take the value of %%X
outside of a for loop for easier processing.
Or use a neq
and nested neq
method in a for loop;
for /f "tokens=*" %%G in ('net localgroup administrators') do (
if "%%~G" neq "Administrator" (
if "%%~G" neq "MWAdmin" (
net localgroup administrators %%G /delete
)
)
)
Note i made %%G
into %%~G
, and added quotes around Administrator
and MWAdmin
, this is for stability, in-case a future name causes syntax errors.