PowerShell script to move computer accounts to an OU

Hi
I need a power shell script to move computers accounts (that list in an txt) from all over the domain to another OU.
The txt will have the computer netbois name
I already have vbs but it has only move some machines and left the rest for no reason.
Thanks
LVL 5
rov17Asked:
Who is Participating?

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

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

Chris DentPowerShell DeveloperCommented:

If you grab the Quest PowerShell CmdLets this will be very easy:

http://www.quest.com/powershell/activeroles-server.aspx

It should let you do this:

Get-Content SomeFile.txt | %{
  Get-QADComputer | Move-QADObject -NewParentContainer "OU=SomewhereNew,DC=yourdomain,DC=com"
}

HTH

Chris
0
rov17Author Commented:
Hi Chris,

Thanks for the update, I have Quest PowerShell installed on the server, after running the script I got error message:
The term Get-QADComputer is not recognized as a cmdlet, function, operable program or script file. verify the term and try again

I have got ActiveRoles Management Shell 1.3.0, a part of Quest activeRoles Server 6.5.
 
0
Chris DentPowerShell DeveloperCommented:

Hmm are they loaded?

Run this then try again?

Get-PsSnapIn -Reg | Add-PsSnapIn

Chris
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Chris DentPowerShell DeveloperCommented:

Although I did make a mistake in the script. It should have been this (I've added the line to load the snap-in as well:

Add-PsSnapIn Quest.ActiveRoles.ADManagement
Get-Content SomeFile.txt | %{
  Get-QADComputer $_ | Move-QADObject -NewParentContainer "OU=SomewhereNew,DC=yourdomain,DC=com"
}

Otherwise it doesn't get the computer name which wouldn't do at all.

Chris
0
rov17Author Commented:
Hi Chris,
Yes it worked this time, but it acually didn't read the computer accounts from file, it has moved the machines form Computers OU.
Does the file has to have a header?  
0
Chris DentPowerShell DeveloperCommented:

No, you need the modified version I posted above since I failed to include $_ which represents the current line from  the text file. Sorry about that, Monday morning appears to be pushing it a little today.

Chris
0
rov17Author Commented:
So should be as below:

Add-PsSnapIn Quest.ActiveRoles.ADManagement
Get-Content $_  SomeFile.txt | %{
  Get-QADComputer $_ | Move-QADObject -NewParentContainer "OU=SomewhereNew,DC=yourdomain,DC=com"
}
0
Chris DentPowerShell DeveloperCommented:

No... it should be this:

Add-PsSnapIn Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue
Get-Content SomeFile.txt | %{
  Get-QADComputer $_ | Move-QADObject -NewParentContainer "OU=SomewhereNew,DC=yourdomain,DC=com"
}

The first command forces the Quest CmdLets to load. I've added "-ErrorAction SilentlyContinue" here so it won't complain if they're already loaded.

%  is ForEach-Object, it's a loop statement to help us work through the input file. Then $_ represents the current line in the text file and is the computer we want to find.

Chris
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
rov17Author Commented:
Something wrong Chris
The script did move the accounts that in the file to the destenation OU, then it starts to move all the computers accounts and the servers to the same ou (which is not in the file)
the file only had 130 account and I have managed to stop the script after it has moved over 300 machines.
0
Chris DentPowerShell DeveloperCommented:

Are there any blank lines in the text file? A trailing blank line will possibly cause that behaviour.

Chris
0
rov17Author Commented:
The script has moved the accounts, but then starts to move every thing to the target OU, no empty lines in the file.
Thanks for your help.
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.