Solved

Removing Disabled Users - Powershell Script

Posted on 2014-04-03
11
324 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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 500 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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

717 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