[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Removing Disabled Users - Powershell Script

Posted on 2014-04-03
11
Medium Priority
?
326 Views
Last Modified: 2014-05-01
Afternoon,

There is a script I found online that should allow me to look at a particular OU and remove any users from AD Groups that are disabled.

Making minor modifications to the script, it ran without any errors reported in the shell, however there were no users deleted once it ran.

Script
 Get-QADGroup -SearchRoot "OU=Disabled Users,DC=domain,DC=local" | Foreach-Object{
     $group = $_
     Get-QADGroupMember -Identity $group -Disabled -Type User | Remove-QADGroupMember -Identity $group -WhatIf
 }



User is part of two other groups. From what I can see, this script appears accurate.(domain would be our domain name of course)

The groups however have remained there, even with the -WhatIf statement removed.

Thanks!
0
Comment
Question by:victory2201
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 37

Expert Comment

by:Jamie McKillop
ID: 39977893
Hello,

Are the groups located in the OU "OU=Disabled Users,DC=domain,DC=local"? The searchroot parameter is for looking for the location of the groups, not the users. If you want to remove the users from any group in your domain, remove the searchroot parameter from your command.

-JJ
0
 

Author Comment

by:victory2201
ID: 39978315
Get-QADGroup  "OU=Disabled Users,DC=domain,DC=local" | Foreach-Object{
     $group = $_
     Get-QADGroupMember -Identity $group -Disabled -Type User | Remove-QADGroupMember -Identity $group -WhatIf
 }


What about the whatif statement? I thought that would have to be removed as well before deletion could occur? (I removed the -searchroot above)
0
 

Author Comment

by:victory2201
ID: 39978324
Yes, they are located in the OU labeled disabled users for testing right now.

The actual OU structure would look like this if applying on a per site basis instead of that OU.

OU=SiteName, OU=Disabled Users, DC=DomainName, DC=local
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:Jamie McKillop
ID: 39978326
Yes, you also need to remove the whatif statement.

-JJ
0
 
LVL 37

Expert Comment

by:Jamie McKillop
ID: 39978337
After the line: $group = $_

Add the line: $group

This will echo back the group and confirm that it is finding the groups you want it to.

-JJ
0
 

Author Comment

by:victory2201
ID: 39978372
Ok,

Modified the script as discussed. The result however was still the same.

Thanks.
EX5.png
0
 
LVL 37

Expert Comment

by:Jamie McKillop
ID: 39978403
Remove "OU=Disabled Users,DC=domain,DC=local" from your script

Your script should look like this:

Get-QADGroup  | Foreach-Object{
     $group = $_
     Get-QADGroupMember -Identity $group -Disabled -Type User | Remove-QADGroupMember -Identity $group 
 }

Open in new window


-JJ
0
 

Author Comment

by:victory2201
ID: 39978425
OK, question though.

That modification you made above is no longer looking to a particular OU.

Ideally, what I am looking for is being able to filter, per disabled user OU, to remove all current disabled accounts from any groups that they may still be part of.

EX:
Username John Smith, is part of HQGlobal and HQMarketing. We need that script to run on that OU and remove him from those groups, since he is no longer an employee.

I need this to happen for multiple users that are within the OU as well, during the script.


Thanks. : )
0
 
LVL 37

Accepted Solution

by:
Jamie McKillop earned 1500 total points
ID: 39978556
OK, you need to approach it a bit differently to do what you want. try this script:

$users = Get-QADUser -SearchRoot "OU=Disabled Users,DC=domain,DC=local" 
foreach ($user in $users) {
	Get-QADGroup  | where {Get-QADGroupMember -Identity $_ -Name $user.Name} | % {
		Remove-QADGroupMember -Identity $_ -Member $user
	}
}

Open in new window


-JJ
0
 

Author Comment

by:victory2201
ID: 39978624
Ok, that startled me when the -searchroot command ran inside ISE. >_>

That should show a progress bar as it scans AD and its entirety correct?
0
 
LVL 37

Expert Comment

by:Jamie McKillop
ID: 39978637
Yes, it will search all of AD for the groups. You may need to add -sizelimit 0 to the get-qadgroup command ig you have a lot of groups. If all your groups are in a single OU, you can add the -searchroot parameter to specify that OU only be searched.

-JJ
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question