如何使用 PowerShell 从特定 OU 中删除所有用户?

powershellmicrosoft technologiessoftware & coding更新于 2026/2/9 20:07:17

要从特定 OU 中删除所有用户,我们首先需要检索该 OU 中的用户。例如,我们在名为 LABDOMAIN 的 OU 中有 3 个用户,我们需要删除所有用户。

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

上述命令将从特定 OU 中检索用户,然后我们可以使用 Remove-ADUser 命令将其全部删除。

示例

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

-Confirm 开关已添加,可绕过所有用户的确认。默认确认为 true。

输出

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".

相关文章