ak2595
ak2595

Reputation: 321

How to move adobject to another OU

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
 
 }

Upvotes: 1

Views: 1008

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 59900

-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
}

Upvotes: 1

Related Questions