Moving objects from one OU to another

Often, Active Directory objects are moved between different OUs for reorganization purposes. The following script searches the AD for the given objects and moves them to the mentioned OU target. This script works for user, computer, and group object types:

Function Move-ObjectsToOU { [CmdletBinding()] param( [Parameter(Mandatory=$true,Position=0)] [string[]]$Name, [Parameter(Mandatory=$true,Position=1)] [ValidateSet("User","Group","Computer")] [string]$Type, [Parameter(Mandatory=$true,Position=2)] [string]$TargetOUPath ) if([ADSI]::Exists("LDAP://{0}" -f $TargetOUPath)) { Foreach($ObjectName in $Name) { try { $Object = Get-ADObject -Filter { Name -eq $ObjectName -and ObjectClass -eq $Type } -EA Stop Move-ADObject ...

Get Active Directory with PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.