0

I wrote a basic batch file to add couple of domain groups to the local admin account, validate the groups have been added, and change the color of the output based on the result. I'm sure there are much better ways to do this using VBS or other programming language but I wanted to know if there is a better way to do it using CMD only without any other third party tool.

I want to know if there is a way to display both outputs at the same time using different colors on the screen. For example, if one domain group was added successfully but the other one failed for whatever reason, are you able to display the output of the successful one in one color (green) and the output of the failed on in another color (red) without changing the color of the entire CMD window?

Below is my code:

@echo off
net localgroup administrators /add DOMAIN GROUP 1 >nul
if %errorlevel% == 0 (
color 0A
echo DOMAIN GROUP 1 group was added successfully!
) else (
color 0C
echo DOMAIN GROUP 1 group was not added!
)
echo.
pause
cls
net localgroup administrators /add DOMAIN GROUP 2 >nul
if %errorlevel% == 0 (
color 0A
echo DOMAIN GROUP 2 group was added successfully!
) else (
color 0C
echo DOMAIN GROUP 2 group was not added!
)
echo.
pause
Mark
  • 3,332
  • 1
  • 19
  • 33

1 Answers1

0

No native solution. See edit below.

If PowerShell is available on the machine :

@echo off
call :add "DOMAIN GROUP 1"
call :add "DOMAIN GROUP 2"
pause
exit

:add
set $group=%~1
net localgroup administrators /add %$group% >nul
if errorlevel 1 goto :error
powershell write-host -foregroundcolor green "%$group% was added successfully !"
exit /b

:error
powershell write-host -foregroundcolor red "%$group% was not added !"
exit /b

If there is no PowerShell, you will have to do something with an external executable/ansi lib. Discussed here : How to echo with different colors in the Windows command line

Edit

How to have multiple colors in a Windows batch file?

Community
  • 1
  • 1