0
votes

I've been having an issue with moving user accounts using PowerShell. I want to move specific users accounts from OU to OU based on what they have as an Office attribute.

The script is:

$prague = Get-ADUser -Filter * -Properties * | Select-Object -Property Office,SamAccountName | where -Property Office -eq "prague"

Move-ADObject -Identity $prague -TargetPath "OU=LAB,DC=test,DC=cz"

Powershell returns:

Move-ADObject : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADObject' required by parameter 'Identity'. Specified method is not supported. 
   At line:6 char:25 
   + Move-ADObject -Identity $prague -TargetPath "OU=LAB,DC=test... 
   + ~~~~~~~ 
   + CategoryInfo : InvalidArgument: (:) [Move-ADObject], ParameterBindingException 
   + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.MoveADObject

Is it possible to run Move-ADObject cmdlet based on the Get-ADUser with plenty of parameters?

1

1 Answers

0
votes

Move-Object is expecting to move one object. You are cramming several into it via the array $prague. You need to use the pipeline or a loop to address this.

Get-ADUser -Filter {Office -eq "prague"} -Properties Office | 
    Move-ADObject  -TargetPath "OU=LAB,DC=test,DC=cz"

Also don't use -Properies * when you only need -Properies Office. Else you are creating a potential performance issue by pulling all properties, including non cached ones, into your session. We simplified this further by only querying for users of the appropriate office instead of post processing with Where