powershell – move machine types to certain OUs in active directory


-------------- select additional parameters ----------------------

Get-ADComputer -Filter * -Properties ipv4Address, OperatingSystem, OperatingSystemServicePack | Format-table name, ipv4*, operatingsystem

-------------- move script -----------------

[cmdletbinding()]

param (
[parameter(mandatory=$true)]
$TargetOU
)

Import-Module ActiveDirectory
$Domain = [ADSI]""
$DN=$domain.distinguishedName
$SourcePath = "CN=Computers," + $DN
$Computers = Get-ADComputer -Filter * -SearchBase $SourcePath
if(!$Computers) {
write-host "No Computers are found in default container"
return
}
foreach ($Computer in $Computers) {
if(!(Move-ADObject $Computer -TargetPath $TargetOU)) {
$Status = "SUCCESS"
} else {
$Status = "FAILED"
}
$OutputObj = New-Object -TypeName PSobject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.Name.tostring()
$OutputObj | Add-Member -MemberType NoteProperty -Name SourcePath -Value $SourcePath
$OutputObj | Add-Member -MemberType NoteProperty -Name DestinationPath -Value $TargetOU
$OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value $Status
$OutputObj
}