Menu

Newsletter

PowerShell : Move the computer accounts from one OU to another OU

Shinish 7 years ago 787
0 0
Read Time:48 Second

This script is for moving the bulk users in a specific OU to another domain in active directory. Compatible with PowerShell version 4.

<#
.SYNOPSIS
   Bulk move computer accounts to specific OU
.DESCRIPTION
   Bulk move computer accounts to specific OU
.EXAMPLE
  .\Move-ComputerAccounts.ps1
#>

Remove-Item '.\ADcomputerMove.log' -Ea SilentlyContinue
#Get the computer list from the servers.txt file
    $ComputerAccVar= Get-Content .\computers.txt
    #Specify the OU Path
    $TargetOUVar = 'OU=NU,OU=GDSScripts,DC=Sub,DC=Domain,DC=Com'
    foreach ($server in $ComputerAccVar){
    try{
        $DNVar= (Get-ADComputer $server).distinguishedname
        Move-ADObject -Identity $DNVar -TargetPath $TargetOUVar 
        Write-Host "Success: Moved computer account $server to the $TargetOUVar" -ForegroundColor Cyan 
        Out-File -FilePath '.\ADcomputerMove.log' -InputObject "Success:Moved computer account $server to the $TargetOUVar"  -Append -ea SilentlyContinue
        }
    catch 
    {
        $ErrorVar= "Failed :"
        $ErrorVar+=$_.Exception.Message
        Write-Host "Failed :"$_.Exception.Message -ForegroundColor Yellow 
        Out-File -FilePath '.\ADcomputerMove.log' -InputObject $ErrorVar -Append
        continue
    }
    }
Write-host "Script Completed Successfully. Report is available at .\ADcomputerMove.log" 

Happy
Happy
50 %
Sad
Sad
50 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
Written By

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%