Book Image

Active Directory with PowerShell

By : Pamarthi Venkata Sitaram, YELLAPRAGADA U PADMAVATHI
5 (1)
Book Image

Active Directory with PowerShell

5 (1)
By: Pamarthi Venkata Sitaram, YELLAPRAGADA U PADMAVATHI

Overview of this book

Table of Contents (16 chapters)
Active Directory with PowerShell
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 -Identity $Object -TargetPath $TargetOUPath - EA Stop
      Write-Host "$ObjectName : Moved successfully to target OU"
    } catch {
      Write-Warning "Cannot move $ObjectName"}
    }
} else {
  Write-Warning...