I'm using the net group "Groupname" /domain
command which outputs a bunch of users in the group in the format of 3 columns. There are spaces between the columns.
How do I output the command so that there is one user per line?
Please attempt something before asking next time.
Assuming the output of the command is roughly:
bossMan Arescet Ice
superuser lazyWorker Dog
(etc...)
As i am unsure of the output of this file from where I'm writing this, I don't know if a prefix to the output is given that needs to be ignored.
:: First attempt with a single line;
for /f %%G in ('net group "Groupname" /domain') do (echo %%G)
Now i prefer using files by habit of backups and odd character handling so i would use this, although it is 3 lines and writes to a file;
:: Write the output of your command into a file:
net group "Groupname" /domain >userList.temp
:: Split the content by spaces, and write each space separated token.
:: Column 1 is %%~G, 2 is %%~H, 3 is %%~I.
for /f %%G in (userList.tmp) do (
echo %%~G
echo %%~H
echo %%~I
)
:: Cleanup the temp file;
del "userList.temp"
Of course >>output.ext
can be applied individually to %%~G %%~H %%~I
, or a call with that specified column of the current line.
Please use tokens=1,2,3 option
echo off
:: Write the output of your command into a file:
net group "Groupname" /domain >userList.temp
:: Split the content by spaces, and write each space separated token.
:: Column 1 is %%~G, 2 is %%~H, 3 is %%~I.
for /f "skip=8 tokens=1,2,3" %%G in (userList.temp) do (
echo %%G
echo %%H
echo %%I
)
:: Cleanup the temp file;
del "userList.temp"
pause
echo on
Thanks for all the heavy lifting creating this script, it worked very nicely.
However I wanted to evaluate a number of groups so I added in the ability to pass the group as an argument and write the output to a file. Very minor tweaks on the existing solution.
echo off
set GRP_NM=%1
:: Write the output of your command into a file:
net group "%GRP_NM%" /domain >userList.temp
:: Split the content by spaces, and write each space separated token.
:: Column 1 is %%~G, 2 is %%~H, 3 is %%~I.
echo UserName > %GRP_NM%_userlist.txt
for /f "skip=8 tokens=1,2,3" %%G in (userList.temp) do (
echo %%~G >> %GRP_NM%_userlist.txt
echo %%~H >> %GRP_NM%_userlist.txt
echo %%~I >> %GRP_NM%_userlist.txt
)
:: Cleanup the temp file;
del "userList.temp"
::pause
echo on
You can use the FOR 'command' option directly to do this without the temporary file.
The simple version extracting only usernames in a given group on the command line goes like this:
@ECHO off
CLS
::Get the group name from the command line
IF not "%1"=="" SET str_Grp-Nm=%1
IF not "%1"=="" ^> Given group name: '%str_Grp-Nm%'.
IF not DEFINED str_Grp-Nm SET str_Grp-Nm=Default_group
::Remove double quotes if any.
SET str_Grp-Nm=%str_Grp-Nm:"=%
::Default DOS-language: EN-US/ENU ^(Engels, USA^)
SET str_LastLn=The command completed
ECHO User names in group name;%str_Grp-Nm%>"%~dpn0 - %str_Grp-Nm%.csv"
::Split the content in three columns; column 1 is %%~g, 2 is %%~h and 3 is %%~i.
FOR /f "skip=8 tokens=1,2,3" %%g IN ('NET group "%str_Grp-Nm%" /domain') DO (
rem Skip the last line 'The command completed successfully.'. Note; this last line is language dependent, in Dutch the last line is: 'De opdracht is voltooid.'.
IF /i not "%%~g %%~h %%~i"=="%str_LastLn%" (
ECHO ^> Found username '%%~g':
ECHO %%~g>>"%~dpn0 - %str_Grp-Nm%.csv"
IF not '%%~h'=='' ECHO ^> Found username '%%~h':
IF not '%%~h'=='' ECHO %%~h>>"%~dpn0 - %str_Grp-Nm%.csv"
IF not '%%~i'=='' ECHO ^> Found username '%%~i':
IF not '%%~i'=='' ECHO %%~i>>"%~dpn0 - %str_Grp-Nm%.csv"
) ELSE (
ECHO * %%~g %%~h %%~i successfully!
)
)
IF exist "%~dpn0 - %str_Grp-Nm%.csv" START "This quoted line is needed for %OS%" "%~dpn0 - %str_Grp-Nm%.csv"
::Cleanup the environment...
SET str_Grp-Nm=
SET str_LastLn=
An expanded version, also extracting the full username and detecting the, in my case Dutch, DOS-language. Note: The DOS language could be different from your OS-language, especially in multilingual-systems.
@ECHO off
CLS
::Get the group name from the command line
IF not "%1"=="" SET str_Grp-Nm=%1
IF not "%1"=="" ECHO ^> Given group name: '%str_Grp-Nm%'.
IF not DEFINED str_Grp-Nm SET str_Grp-Nm=Default_group
::Remove double quotes if any.
SET str_Grp-Nm=%str_Grp-Nm:"=%
::Default DOS-language: EN-US/ENU ^(Engels, USA^)
SET str_Usr-Fn=Full Name
SET str_Usr-Un=UserName
SET str_LastLn=The command completed
::Detect DOS-language: NL-NL/NLD ^(Nederlands, Nederland^)
NET user %UserName% /domain | FIND /i "vOltoOid.">NUL &IF not ErrorLevel 1 (
ECHO ^> DOS-language: NL-NL/NLD ^(Nederlands, Nederland^) detected.
SET str_Usr-Fn=Volledige naam
SET str_Usr-Un=Gebruikersnaam
SET str_LastLn=De opdracht is
)
ECHO * Check for usernames in group: '%str_Grp-Nm%'
ECHO Press any key to continue, Ctrl+C to quit.
PAUSE > NUL
::Or continue afther... TimeOut /t 33
ECHO Group name;%str_Grp-Nm%>"%~dpn0 - %str_Grp-Nm%.csv"
ECHO %str_Usr-Un%;%str_Usr-Fn%>>"%~dpn0 - %str_Grp-Nm%.csv"
::Split the content in three columns; column 1 is %%~g, 2 is %%~h and 3 is %%~i.
FOR /f "skip=8 tokens=1,2,3" %%g IN ('NET group "%str_Grp-Nm%" /domain') DO (
rem Skip the last line 'The command completed successfully.'. Note; this last line is language dependent, in Dutch the last line is: 'De opdracht is voltooid.'.
IF /i not "%%~g %%~h %%~i"=="%str_LastLn%" (
ECHO ^> Check '%%~g':
REM Get the 'Full Name' from the UserName by using: 'NET user %%~g /domain | FIND /i "%str_Usr-Fn%"'. Use double qoutes to get the pipe-action in the FOR-command. Because there is a space in 'Full Name' a search for "name" does not give the correct result (2 ànd 3 columns).
FOR /f "tokens=2,*" %%u IN ('"NET user %%~g /domain | FIND /i "%str_Usr-Fn%""') DO ECHO %%~g;%%v>>"%~dpn0 - %str_Grp-Nm%.csv"
FOR /f "tokens=2,*" %%u IN ('"NET user %%~g /domain | FIND /i "%str_Usr-Fn%""') DO ECHO %str_Usr-Un%; '%%~g'; %str_Usr-Fn%; "%%v".
IF not '%%~h'=='' ECHO ^> Check '%%~h':
IF not '%%~h'=='' FOR /f "tokens=2,*" %%u IN ('"NET user %%~h /domain | FIND /i "%str_Usr-Fn%""') DO ECHO %%~h;%%v>>"%~dpn0 - %str_Grp-Nm%.csv"
IF not '%%~h'=='' FOR /f "tokens=2,*" %%u IN ('"NET user %%~h /domain | FIND /i "%str_Usr-Fn%""') DO ECHO %str_Usr-Un%; '%%~h'; %str_Usr-Fn%; "%%v".
IF not '%%~i'=='' ECHO ^> Check '%%~i':
IF not '%%~i'=='' FOR /f "tokens=2,*" %%u IN ('"NET user %%~i /domain | FIND /i "%str_Usr-Fn%""') DO ECHO %%~i;%%v>>"%~dpn0 - %str_Grp-Nm%.csv"
IF not '%%~i'=='' FOR /f "tokens=2,*" %%u IN ('"NET user %%~i /domain | FIND /i "%str_Usr-Fn%""') DO ECHO %str_Usr-Un%; '%%~i'; %str_Usr-Fn%; "%%v".
) ELSE (
ECHO * %%~g %%~h %%~i successfully!
)
)
IF exist "%~dpn0 - %str_Grp-Nm%.csv" START "This quoted line is needed for %OS%" "%~dpn0 - %str_Grp-Nm%.csv"
::Cleanup the environment...
SET str_Grp-Nm=
SET str_Usr-Fn=
SET str_Usr-Un=
SET str_LastLn=
Note: You can extract more information from the combination NET user and FIND, basically following the syntax below here.
NET user <username> /domain | FIND /i "<search string>"
And there is much more possible by using Visual Basic. But those are other storys ;o)
Good luck!