Speaking of learning something new every day… we all know that command
line tools and spaces don’t get along… but the way to make them behave
is to surround values containing spaces in quotes, right?
So the other day I’m writing this query to extract all users in my
active directory (easy enough: dsquery user -name * -limit 50000 | sort
> users.txt).
But all the names had spaces in them and my next query, to list all the
groups each user was in (for /f “delims=~” %%i in (users.txt) do echo
%%i >> foreachuser-showgroupmembership.txt && cscript //nologo
EnumGroup.vbs %%i >> foreachuser-showgroupmembership.txt) kept bombing
out on the spaces.
So after banging my head against the wall for a couple hours, it dawns
on me to look at the help file for the FOR command.
The trick is to define a delimiter that the program will never find…
so since I knew there weren’t any ~s in the names, I set the delimiter
to ~ and re-ran the command.
This may come in handy again someday.
Get all groupnames (short):
dsquery group -limit 50000 -o samid | sort
Get all groupnames (distinguished name):
dsquery group -limit 50000 | sort
Get all users:
dsquery user -name * -limit 50000 | sort
Get all users in each group:
for /f “delims=~” %%i in (groups-SHORT.txt) do echo %%i >>
foreachgroup-showitsmembers.txt && dsquery group -name “%%i” | dsget
group -members >> foreachgroup-showitsmembers.txt && type crlf.txt >>
foreachgroup-showitsmembers.txt
Get all groups that each user is in:
for /f “delims=~” %%i in (users.txt) do echo %%i >>
foreachuser-showgroupmembership.txt && cscript //nologo EnumGroup.vbs
%%i >> foreachuser-showgroupmembership.txt && type crlf.txt >>
foreachuser-showgroupmembership.txt