Home > Net >  How to move adobject to another OU
How to move adobject to another OU

Time:05-28

i want to know how to move adobject to another ou?

Im not sure what im doing wrong with the -filter

$CSVFiles = @"
adobject;                                              
StdUser
TstUser
SvcAcc
"@ | Convertfrom-csv -Delimiter ";"


$TargetOU =  "OU=ARCHIVE,DC=contoso,DC=com"

foreach ($item in $CSVFiles){
 get-adobject -Filter {(cn -eq $item.adobject)} -SearchBase "OU=ADMIN,DC=contoso,DC=com"| select distinguishedname | Move-ADObject -Identity {$_.objectguid}  -TargetPath $TargetOU
 
 }

CodePudding user response:

-Identity { $_.ObjectGuid } shouldn't be there, this parameter can be bound from pipeline, you also do not need to strip the objects from it's properties, in other words, Select-Object DistinguishedName has no use other than overhead.

Also filtering with a script block (-Filter { ... }) is not well supported in the AD Module and should be avoided. See about_ActiveDirectory_Filter for more info.

$TargetOU = "OU=ARCHIVE,DC=contoso,DC=com"

foreach ($item in $CSVFiles){
    $adobj = Get-ADObject -LDAPFilter "(cn=$($item.adobject)" -SearchBase "OU=ADMIN,DC=contoso,DC=com"
    if(-not $adobj) {
        Write-Warning "'$($item.adobject)' could not be found!"
        # skip this object
        continue
    }
    $adobj | Move-ADObject -TargetPath $TargetOU
}
  • Related