• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3510
  • Last Modified:

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
0
rov17
Asked:
rov17
  • 6
  • 5
1 Solution
 
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
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

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

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now