Powershell Move Computers into new OU

I'm trying to move a list of computers into a new OU.
The code I'm running is

$ComputerName = Get-Content "c:\Computers2.txt"  

foreach ($Computer in $ComputerName) {  
        Get-ADComputer $ComputerName | Move-ADObject -TargetPath 'ou=WIN7_WIN8,ou=Workstations,dc=corp,dc=testtest,dc=com'

Open in new window

I'm getting the error

Get-ADComputer : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADComputer'
required by parameter 'Identity'. Specified method is not supported.
At C:\Users\admin\Desktop\laps.ps1:5 char:24
+         Get-ADComputer $ComputerName | Move-ADObject -TargetPath 'ou=WIN7_WIN8,o ...
+                        ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

Is the code I'm running incorrect?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Casey WeaverManaged Services Windows Engineer IIICommented:
Get-Content is going to feed a solid string of data into Get-ADComputer, which doesn't work.  Turn your file into a csv, and use Import-CSV instead.

yo_beeDirector of Information TechnologyCommented:
$computer should be used for the identity and not $computername

You stored all the computer names in an array in $computername and running the foreach loop against $computername.  So replace $computername with $computer.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy LidbetterCommented:
to clarify what yo_bee said...

This is what the code should look like.

$ComputerName = Get-Content "c:\Computers2.txt"  

foreach ($Computer in $ComputerName) {  
        Get-ADComputer $Computer | Move-ADObject -TargetPath 'ou=WIN7_WIN8,ou=Workstations,dc=corp,dc=testtest,dc=com'

Open in new window

When you say "foreach ($Computer in $ComputerName)".... $Computer is pipelined through not $Computername.
Also, if the list is long, you may want to add -Confirm:$False to the Move-ADObject cmdlet... otherwise you'll be doing a lot of confirming...


xouvangAuthor Commented:
Thank you all! I got it to work with your suggestions.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.