Announcement

Collapse
No announcement yet.

Batch file to remove a user from all groups

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch file to remove a user from all groups

    I want to take a list of DNs (in users.txt) and remove them from all groups. I've got the following bit of code but it doesn't quite work.

    Code:
    FOR /F "delims=*" %%B in ("users.txt") do (DSQUERY GROUP -limit 0 | DSMOD GROUP -C -RMMBR %%B)
    Right now, the command evaluates like this, which clearly isn't correct.

    Code:
    (DSQUERY GROUP -limit 0   | DSMOD GROUP -C -RMMBR C:\Documents and Settings\username\desktop\users.txt )
    I'm really...really terrible with the FOR command. I can't figure out how to get %%B to be the DN. Thanks.

  • #2
    Re: Batch file to remove a user from all groups

    So, when I put the text file in the same directory as the script, and remove the quotes from around the file name, that fixed it. I haven't tried running it from a different directory with singe-quotes.

    Comment


    • #3
      Re: Batch file to remove a user from all groups

      Originally posted by mhashemi View Post
      when I put the text file in the same directory as the script, and remove the quotes from around the file name, that fixed it. I haven't tried running it from a different directory with singe-quotes.
      You can remove the quotes when the file name or provided path does not contain embedded spaces.

      When you add quotes around the complete set in the For/F command, you'll have to use the usebackq argument. Otherwise the file name itself will be taken as phrase.

      You can not replace the double-quote signs here with single-quote signs. The For/F command will then interpret the provided txt-file as command line.

      additionally,
      Instead of querying all groups like you did in your sample, you can use DSGET to retrieve only the groups the user is actualy a member of,
      Code:
      FOR /F "usebackq delims=" %%* in ("c:\test folder\users.txt") do (
         DSGET.exe USER "%%*" -memberof | DSMOD.exe GROUP -C -RMMBR "%%*")

      Note, The user' s Primairy group cannot not be removed from the object.


      \Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Batch file to remove a user from all groups

        Gotcha on the quotes and thanks for reminding me of dsget. I never use it so I didn't even consider it for this script. Works like a champ.

        Comment

        Working...
        X