Question

How do I remove users from all our group in AD

I currently have a file where I run a DSQUERY on users within a specific OU Which I then PIPE into a DSMOD USER so that i can change the Description on the Accounts.

When this is done I then end up with a DSMOVE to move the Account into another OU.  This all works fine but I know want to also remove the USERS from all the groups they maybe a part of.

The current text I'm using to do all the above barring the removing from groups is as follows:

dsquery user "OU=Disabled Accounts,OU=Data Has Been Archived,OU=base,OU=Staff,OU=Accounts - Archive,DC=Home,DC=co,DC=uk" | dsmod user -desc "Archived %Date%"
For /F "delims=*" %%u IN ('dsquery user -desc Archived* "OU=Disabled Accounts,OU=Data Has Been Archived,OU=Base,OU=Staff,OU=Accounts - Archive,DC=Home,DC=co,DC=uk"') DO dsmove %%u -newparent "OU=Data Has Been Archived,OU=Base,OU=Staff,OU=Accounts - Archive,DC=Home,DC=co,DC=uk"

Many thanks for anyones help

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-03 at 06:47:45ID24867178
Tags

Dsmod

,

Active Directory

Topics

Lightweight Directory Access Protocol (LDAP)

,

Active Directory

,

Programming User Management

Participating Experts
1
Points
500
Comments
30

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Error DSMod Group
    I have a user:- "CN=Jeff Bartles,OU=office-Users,DC=sslonline,DC=co,Dc=uk" and I want to add it to another group:- DSMod group "CN=TS Users,OU=Users,DC=sslonline,DC=Co,Dc=uk" -addmbr "CN=Jeff Bartles,OU=office-Users,DC=sslonline,DC=co,Dc=uk" ...
  2. Dsquery / Dsmod Help - Users in group / change profile …
    I could use some help please I need to change everyone that belongs to group "Blah" over to a different profile path If I run this, I get all of my members (bonus) but when I try and dsmod this, it isnt flying. dsquery group "CN=BLAH,OU=Sites,OU=Security Gro...
  3. How to add users to a group using a "for" loop with dsqu…
    Almost every user in our company is assigned a floor space id like this: 2003, 1041, 3091. The first digit is the floor they sit on, the rest of the digits are irrelevant to what I want to do. We also have a distribution group setup for each floor (we only have three floors)....
  4. Use dsquery dsmod
    I'm trying to pipe a dsquery to a dsmod to change the company attribute of all users in a certain OU. If I type: (removed my domain name for posting) dsquery user "ou=Cav USERS TEST,ou=Cavalier USERS,ou=Cavalier,dc=domain,dc=domain,dc=domain,dc=domaindc=domain" I g...
  5. dsquery fails adding users in an ou to a group is any memb…
    The script is supposed to add all users in a specified OU to a group. dsquery user OU=zztemp,DC=domain,DC=com | dsmod group "CN=studentsgroup,OU=zztemp,DC=domain,DC=com" -c -addmbr BUT If any users in that OU are ALREADY in that group then the script returns the ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Dusan_BajicPosted on 2009-11-03 at 08:45:23ID: 25730826

This will list members of OU, for each member it will list groups, then it will remove member from each group. User will remain member of his primary group (Domain Users)


For /F "delims=*" %%w IN ('dsquery user -desc Archived* "OU=Data Has Been Archived,OU=Base,OU=Staff,OU=Accounts - Archive,DC=Home,DC=co,DC=uk"') DO (
      For /F "delims=*" %%g IN ('dsget user %%w -memberof -expand') DO (
            dsmod group %%g -rmmbr %%w
      )
)

 

by: Colchester_InstitutePosted on 2009-11-04 at 01:16:03ID: 25737573

Hi Dusan

That seems to be near enough what I need except it leaves one group other than the Domain users (That I know of)

That group is our Broadcast all staff Group which is our Distribution list for all staff for mass email.  The Group itselfs is set up as a Security type.  I know group mod can only do a 100 groups unless you set the limit to more, but where abouts would i set this limit on what you have written?

Many thanks

 

by: Dusan_BajicPosted on 2009-11-04 at 04:31:04ID: 25738621

dsmod command in this script removes only one user from one group at each pass. You could hit 100 users limit at "dsquery user" command (and you can fix that with "-limit 1000" ) but in that case you would be left with some users that have intact their group membership.

Is any user successfully removed from that group?
Can you check from AD Users GUI "member of" tab for some of the users, is that group still listed?
Can you run cmd "dsquery user" and "dsget user" commands from script manually and see results?
 

 

by: Colchester_InstitutePosted on 2009-11-04 at 04:37:22ID: 25738662

I'm only running it as a test on two users at the moment and neither one got removed....All other groups that I added to them users went and they were just left with Domain Users and Broadcast All Staff.

 

by: Dusan_BajicPosted on 2009-11-04 at 04:50:50ID: 25738747

do you get any errors when you run script?

 

by: Colchester_InstitutePosted on 2009-11-04 at 04:58:00ID: 25738801

Only cant remove from group but that is concerning groups they are not a part of so i'd expect that?

 

by: Dusan_BajicPosted on 2009-11-04 at 06:56:49ID: 25739898

Only error should be when trying to remove user from Domain Users. Script first checks what groups is user member of, and then removes user from those groups.

can you paste output here?

 

by: Colchester_InstitutePosted on 2009-11-04 at 07:28:27ID: 25740258

See below for the code this happens for most groups.....I'm not convinced its searching all the groups within the domain because of how quick it happens

C:\>(dsmod group "CN=Users,CN=Builtin,DC=colchester,DC=ac,DC=uk" -rmmbr "CN=Fleu
r Soards,OU=Data Has Been Archived,OU=Base,OU=Staff,OU=Accounts - Archive,DC=
Home,DC=ac,DC=uk" )
dsmod failed:CN=Users,CN=Builtin,DC=home,DC=co,DC=uk:The specified account
 name is not a member of the local group.:CN=USER NAME,OU=Data Has Been Archi
ved,OU=Base,OU=Staff,OU=Accounts - Archive,DC=Home,DC=co,DC=uk could no
t be removed from the group.
type dsmod /? for help.

 

by: Colchester_InstitutePosted on 2009-11-04 at 07:33:26ID: 25740318

By watching the script go thru, I'd say its trying to pull a list of the groups from the domain and then trying to remove the user, instead of actually searching the user first to see what groups they are in??

 

by: Dusan_BajicPosted on 2009-11-04 at 07:53:20ID: 25740557

For /F "delims=*" %%w IN ('dsquery user -desc Archived* "OU=Data Has Been Archived,OU=Base,OU=Staff,OU=Accounts - Archive,DC=Home,DC=co,DC=uk"') DO (
      For /F "delims=*" %%g IN ('dsget user %%w -memberof -expand') DO (
            dsmod group %%g -rmmbr %%w
      )
)

1. Script uses dsquery user to get list of all users from "OU=Data Has ... ... ,DC=uk" with "Archived*" description (check please if I made any typing errors!).
2. First FOR loop goes through that list and for each user lists groups that one particular user is member of ("dsget user" uses -memberof switch to get that list)
3. Second FOR loop goes through that list of groups and removes user using "dsmod group".

I created OU on my domain and added few test users and joined them into random groups, script did its job with no mistake.

Let's troubleshoot each step. First run at cmd prompt:

dsquery user -desc Archived* "OU=Data Has Been Archived,OU=Base,OU=Staff,OU=Accounts - Archive,DC=Home,DC=co,DC=uk"

Do you get list of users you need to process?
 

 

by: Colchester_InstitutePosted on 2009-11-04 at 07:55:21ID: 25740581

OK interestingly I've found that another group that is in the same OU as the broadcast all staff dont get removed either.  

I added a group (Itservices) from the same OU that the Broadcast group is in and then run the script, And the two were there, this lead me to think may its that OU that the groups were based in but I then added a third random one from the OU and the script removed it as it should do but still left me with the Broadcast and ITServices...

 

by: Colchester_InstitutePosted on 2009-11-04 at 07:57:12ID: 25740607

I'm a step ahead head of you...I can use dsquery User and and pipe that into dsget user -memberof and that shows the groups each member is a part of and works fine

 

by: Colchester_InstitutePosted on 2009-11-04 at 08:14:30ID: 25740821

heres another piece of investing

I have 3 groups (BroadcastALL STAFF,ITService,NIS) all in the same OU and two of them dont remove (BroadcastALLStaff and ITServices) but one does (NIS), so I run

Dsquery user "OU=Blah,OU=Blah,DC=Home,DC=co,DC=UK"|dsget user -memberof

that showed exactly what I expected the the groups the users were part of and theres nothing untoward the out except one thing..theres a \ before the names of BroadcastALLSTAFF and ITservices eg:
"CN=\ IT Services,OU=E-Mail Groups and Users

But theres not for the NIS so that outputs as
CN=NIS Team,OU=E-Mail Groups and Users

So I'm guessing its the \ thats stopping them groups from being removed?? Any ideas on how to get round that?

k

 

by: Dusan_BajicPosted on 2009-11-04 at 08:18:35ID: 25740868

If you pick one member, and try to manually remove him from all groups (one group at a time) using
dsmod group "Group...." -rmmbr "User..."

Does it work?

 

by: Colchester_InstitutePosted on 2009-11-04 at 08:23:02ID: 25740924

would what be the syntax for that?

 

by: Dusan_BajicPosted on 2009-11-04 at 08:27:17ID: 25740968

for example:

dsmod group "CN=\ IT Services,OU=Blah,OU=Blah,DC=Home,DC=co,DC=UK" -rmmbr "CN=John Doe,OU=Data Has Been Archived,OU=Base,OU=Staff,OU=Accounts - Archive,DC=Home,DC=co,DC=uk"

 

by: Colchester_InstitutePosted on 2009-11-04 at 08:34:41ID: 25741058

i get the following error when run for the IT Services or BroadcasALLStaff

dsmod failed:Value for `Target object for this command' has incorrect format.
type dsmod /? for help.

But its successful for NIS

i dont know where the "CN=\ IT Services" is coming from as I would have thought that that should have been "CN=IT Services"

 

by: Dusan_BajicPosted on 2009-11-04 at 08:39:02ID: 25741123

It probably has something to do with way those groups were created (probably by some software that integrates in AD). I'll think about solution for this, but not before tomorrow, it's time to go home :)

 

by: Colchester_InstitutePosted on 2009-11-04 at 10:38:13ID: 25742376

What you cant go home!! HAHA no thank you, you're help is appreciated i'll give it some thought also. I've checked the Groups and theres nothing strange about the names etc, and they, well BroadcastALLStaff will have been around since the damn of time, I'll see if theres other groups dating back to then and see if they have the same effect....its def the \ thats missing it up

 

by: Colchester_InstitutePosted on 2009-11-05 at 01:49:03ID: 25747927

its a space before the name of the group, at least thats what i think

Fro example the Canonical name of NIS is
Home.co.uk/E-Mail Groups and Users (Exchange)/NIS

Buts for ITServices its

Home.ac.uk/E-Mail Groups and Users (Exchange)/ IT Services

Thus i dont think it likes the space, but i dont know how to get around that? ad we have a few groups like this

 

by: Dusan_BajicPosted on 2009-11-05 at 02:06:28ID: 25747997

If all 'faulty' groups have that same problem, I'll fix it. Just try "dsmod group" command manually without backslash and see if it works that way.

 

by: Colchester_InstitutePosted on 2009-11-05 at 02:12:31ID: 25748025

all faulty groups have that issue...some may have more than one space

and if i try to mod group without the \ i get directory object not found

CN=IT Services

instead of CN=\ IT Services

 

by: Dusan_BajicPosted on 2009-11-05 at 02:26:01ID: 25748094

We need to find what works manually. Try all combinations: removing backslash, removing backslash and space, adding one more backslash, adding two backslashes...

 

by: Colchester_InstitutePosted on 2009-11-05 at 02:30:24ID: 25748114

works if a space is placed first so CN= IT Services

 

by: Dusan_BajicPosted on 2009-11-05 at 08:19:59ID: 25751035

OK, can you try this please:

SETLOCAL ENABLEDELAYEDEXPANSION
set u=
set w=
set g=
set qwe=
For /F "delims=*" %%w IN ('dsquery user -desc Archived* "OU=Data Has Been Archived,OU=Base,OU=Staff,OU=Accounts - Archive,DC=Home,DC=co,DC=uk"') DO (
      For /F "delims=*" %%g IN ('dsget user %%w -memberof -expand') DO (
        set qwe=%%g
      set qwe=!qwe:\ = !
      dsmod group "!qwe!" -rmmbr %%w
      )
)

 

by: Colchester_InstitutePosted on 2009-11-05 at 08:39:08ID: 25751242

got the following error?

dsquery failed:A referral was returned from the server.

 

by: Dusan_BajicPosted on 2009-11-05 at 08:42:11ID: 25751276

did you add the first line: SETLOCAL ENABLEDELAYEDEXPANSION

 

by: Colchester_InstitutePosted on 2009-11-06 at 02:12:32ID: 25757892

Copied and pasted exactly what you have there I get two errors

I get
1) dsmod failed:Value for `Target object for this command' has incorrect format.
2) dsmod failed:`-' is an unknown parameter.

1 refers to the \ broadcastallstaff group

and i think 2 refers to a group that is something like ISA - SET PROXY

 

by: Dusan_BajicPosted on 2009-11-06 at 02:35:17ID: 25757973

can you try again with this and paste error

SETLOCAL ENABLEDELAYEDEXPANSION
set u=
set w=
set g=
set qwe=
For /F "delims=*" %%w IN ('dsquery user -desc Archived* "OU=Data Has Been Archived,OU=Base,OU=Staff,OU=Accounts - Archive,DC=Home,DC=co,DC=uk"') DO (
      For /F "delims=*" %%g IN ('dsget user %%w -memberof ') DO (
        set qwe=%%g
      set qwe=!qwe:\ = !
      dsmod group !qwe! -rmmbr %%w
      )
)

 

by: Colchester_InstitutePosted on 2009-11-06 at 02:44:01ID: 25758006

SUCCESS!!!! Thats it! the only error I get is Cant remove from group when it gets to the Domain Users whihc is as expected

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...