Solved

remove all users from all ou's

Posted on 2016-09-01
4
31 Views
Last Modified: 2016-09-01
I have a home lab and testing some scripts.  How can I delete all users from all OU's and ou's within ou's?  I can delete all users in an ou with this. But within that OU I have about 10 different ou's and I have about 20 ou, with 10 different ou's within each.

Get-ADUser -Filter * -SearchBase "OU=Students,OU=Peabody,DC=homelab,DC=COM" | Remove-ADUser
0
Comment
Question by:Roccat
[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
  • 2
4 Comments
 

Author Comment

by:Roccat
ID: 41780733
it looks like this works

Get-ADUser -Filter * | Remove-ADUser
0
 
LVL 40

Accepted Solution

by:
Subsun earned 250 total points
ID: 41780751
Users OU contains default accounts.. Following command will give you all users except default Users OU, you can pipe it to  Remove-ADUser
Get-ADUser -Filter * |?{$_.distinguishedname -notmatch "CN=Users"}

Open in new window

0
 
LVL 40

Assisted Solution

by:footech
footech earned 250 total points
ID: 41780754
Your original command will remove all users within the "OU=Students,OU=Peabody,DC=homelab,DC=COM" OU and any child OUs.  Only if you changed the -searchscope parameter to 'OneLevel' would it not find users in child OUs.
0
 

Author Closing Comment

by:Roccat
ID: 41780812
Thanks !
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

691 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