How to delete all users from specific OU using PowerShell?



To remove all users from the specific OU, we need to first retrieve the users from that OU. For example, we have 3 users in the OU called LABDOMAIN and we need to delete them all.

Get-ADUser -SearchBase "OU=LabUsers,DC=labdomain,DC=local" -Filter *

The above command will retrieve users from the specific OU and then we can use the Remove-ADUser command to delete them all.

Example

Get-ADUser -SearchBase "OU=LabUsers,DC=labdomain,DC=local" -Filter * | Remove-ADUser -Confirm:$false -Verbose

-Confirm switch has been added to bypass the confirmation for all the users. The default confirmation is true.

Output

VERBOSE: Performing the operation "Remove" on target "CN=JensonA,OU=LabUsers,DC=labdomain,DC=local". VERBOSE: Performing the operation "Remove" on target "CN=ChrisT,OU=LabUsers,DC=labdomain,DC=local". VERBOSE: Performing the operation "Remove" on target "CN=ChiragN,OU=LabUsers,DC=labdomain,DC=local".

Advertisements