I'm writing a batch script that will use a WMIC command to get a list of all groups on a Windows machine, get the group info by using net localgroup <groupname>
, and then write the info to an output file. Here is what I have:
for /f "skip=1" %%a in ('"wmic group get name"') do net localgroup %%a >> "%OUTPUTFILEPATH%" 2> nul && echo. >> "%OUTPUTFILEPATH%" && echo ^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^=^= >> "%OUTPUTFILEPATH%" && echo. >> "%OUTPUTFILEPATH%"
The issue I am having seems to be with getting quotes around the %%a
variable in do net localgroup %%a
because it outputs the info for groups like Administrators just fine but when it gets to a group name like Remote Desktop Users, it fails to return the info for the group.
In other words, this is what seems to be happening in the FOR loop:
net localgroup Administrators
net localgroup Remote Desktop Administrators
The first operation is successful. The second is not. Obviously, there need to be quotes around Remote Desktop Administrators in order for it to be seen as a single argument but I can't quite figure out how to get this to happen for my %%a
FOR loop variable.
I have tried putting quotes around "%%a"
and '%%a'
. This doesn't help.
Any input would be greatly appreciated.