batch-filecmd

How can I echo each user group on a seperate line in batch?


When I use whoami /groups it gives lots of clutter such as Type, SID, Attributes and formatting lines to split up the content into sections.

I don't want any of this I just want to be able to extract the individual group names for example BUILTIN\Users and NT AUTHORITY\INTERACTIVE and then echo them on individual lines.

Pretty new to this so help is much appreciated.


Solution

  • You can define the output format with the /FO switch (see whoami /?).
    Ask for csv and you have a "nice"* output separating the columns with a comma.
    You can then easily split your desired column into tokens, where token 1 Is your desired output:

    for /f "delims=," %a in ('whoami /groups /fo csv') do @echo %~a
    

    The ~ removes the quotes from the csv format.

    For usage in a batchfile, replace each % with %%

    You can remove the header line with skip=1 in the for command or /nh in the whoami command.

    *) nice for a computer, not to the human eye