Move Computers between OUs- Using Powershell


Below script  wrote it for moving the computers with in the same domain as part of some OU re-consolidation, whole script must not be use it as whole since this was for my unique requirement. I want the server computer object moved from various location to a common OU place (you can mention actual OU structure, )

This will help you move the Computers any target OU without modifying the base script, also help you give multiple search root, for me it was non-production and production server containers.

#———————— SCRIPT START ——————————–

# Author : Vipin Vasudevan

#————————————————-

Import-Module Activedirectory

function Move-OU ($comp)
{
Get-ADComputer -Identity $comp | foreach {$Computer = $_.DistinguishedName}
Move-ADObject -Identity $Computer -TargetPath $OU
}

function Select-Item
{
PARAM
(
[Parameter(Mandatory=$true)]
$options,
[Parameter(Mandatory=$true)]
$displayProperty
)

function processOK
{
if ($lstOptions.SelectedIndex -lt 0)
{
$script:selectedItem = $null
}
else
{
$global:selectedItem = $options[$lstOptions.SelectedIndex]
}
$form.Close()
}
$script:selectedItem = $null

#Create the form
[Windows.Forms.form]$form = new-object Windows.Forms.form
$form.Size = new-object System.Drawing.Size @(225,250)
$form.text = “Server Selection”

#Create the list box.
[System.Windows.Forms.ListBox]$lstOptions = New-Object System.Windows.Forms.ListBox
$lstOptions.Name = “lstOptions”
$lstOptions.Width = 200
$lstOptions.Height = 175
$lstOptions.Location = New-Object System.Drawing.Size(5,5)
$lstOptions.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right

#Create the OK button
[System.Windows.Forms.Button]$btnOK = New-Object System.Windows.Forms.Button
$btnOK.Width=100
$btnOK.Location = New-Object System.Drawing.Size(50, 180)
$btnOK.Text = “OK”
$btnOK.add_click({processOK})
$btnOK.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
$form.Controls.Add($lstOptions)
$form.Controls.Add($btnOK)

#Populate ListBox
foreach ($option in $options)
{
$lstOptions.Items.Add($option.$displayProperty) | Out-Null
}
$form.ShowDialog() | Out-Null
return $script:selectedItem
}

# This is to select the root OU to search for the actual OU where the computer need to move.

$title = “NonProd or Prod”

$message = “Do you want move NonProduction or Production Server?”
$NonProduction = New-Object System.Management.Automation.Host.ChoiceDescription “&NonProduction”, “Check other server local admin password.”
$Production = New-Object System.Management.Automation.Host.ChoiceDescription “&Production”, “Exit this session”
$options = [System.Management.Automation.Host.ChoiceDescription[]]($NonProduction, $Production)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)

{

0
{
$SearchON = “OU=Non-Prod OU,DC=Child,DC=domain,DC=net”
}

1
{
$SearchON = “OU=Prod OU,DC=child,DC=domain,DC=net”
}

}

Write-host “Enter the OU details” -BackgroundColor Yellow -ForegroundColor Green -NoNewline
$Container = Read-Host “(not OU distinguished name)”
$OUs = Get-ADOrganizationalUnit -Filter ‘name -like “*$Container*”‘ -SearchBase $SearchON
$count = ($OUs).count
$count
if ($count -gt “1”)

{
$OU=Select-Item $OUs “DistinguishedName” |foreach { $_.DistinguishedName}
$OU
}

else
{
$OU = $OUs.DistinguishedName
}

# Choice For server list
$title = “Batch or Single server”
$message = “Do you want to select Single server or Batch of servers from file”
$Single = New-Object System.Management.Automation.Host.ChoiceDescription “&Single”, “Single.”
$Batch = New-Object System.Management.Automation.Host.ChoiceDescription “&Batch”, “Batch.”
$options = [System.Management.Automation.Host.ChoiceDescription[]]($Single, $Batch)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)

{

0
{
$comp = Read-host “Please enter the computer name”
Move-OU ($comp)
}

1
{
$computers = get-content <YOUR PATH>\computers.txt
foreach ($comp in $computers)
{
Move-OU ($comp)
}

}

}

# ————— Script End—————————

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s