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?

Thanks!
xouvangAsked:
Who is Participating?
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 WeaverNetwork EngineerCommented:
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.

https://technet.microsoft.com/en-us/library/Ee176874.aspx
0
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.
0

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

Regards

Guy
0
xouvangAuthor Commented:
Thank you all! I got it to work with your suggestions.
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.