Jump to content

Recommended Posts

Posted

Hi

 

I'm in need of a bit of help, we are trying to move around 1000 computers from under the ou of one place example:Site into multiple ou's within ad. For example everything is currently under "site" but we would like to break it down into room numbers for troubleshooting.

 

Example:

Site

>Admin

>Staffroom1

>Students

>Classroom1

Does anyone know if it is possible to do this with a csv and PS ?

 

for example:

 

a csv or text file with and then use powershell to move the 1000 computers ?

 

[TABLE=width: 500]

[TR]

[TD]Name[/TD]

[TD]OU[/TD]

[/TR]

[TR]

[TD]computer1[/TD]

[TD]room101[/TD]

[/TR]

[TR]

[TD]computer2[/TD]

[TD]room102[/TD]

[/TR]

[/TABLE]

 

 

Many thanks for any help.

 

Phil

Posted (edited)

Hi Qwedsa,

 

You can import the data using Import-CSV.

 

Each one of the rows is treated as an object, you can target specific columns also. So you could store the new OU into the CSV, then with the script Ric provided, adapt that to your needs.

 

powershell.org is a great place to seek help if you require it :)

But,

here is a script that should do it, obviously you need to change the variables to match your scenario :)

 

 

$ComputersPath= Import-Csv -Path "D:\Files\Computers.csv"

$TargetOU = "OU=NewOU,DC=myDomain,DC=local"

foreach ($item in $ComputersPath){

 

$computer = Get-ADComputer $item.CompName

 

Move-ADObject -Identity $computer.DistinguishedName -TargetPath $TargetOU -Confirm:$false

 

Write-Host Computer account $computer.Name has been moved successfully

 

 

}

 

 

What this is doing, is reading column names from the CSV file, so where it says $item.CompName - this is because the column name in the CSV has a title of CompName. Then all of the PC's that need targeting are written below it.

Edited by chrispounds
Posted
If you want to "test run" the script, add -whatif after the "move-adobject" line and it will simulate the action for you first. Then just remove it and re-run it when you are happy.
Posted

here is a simple one liner that help moving users from one ou to another

Search-ADAccount –AccountDisabled –UsersOnly –SearchBase “OU=sites,DC=domiain,DC=co,DC=uk”  | Move-ADObject –TargetPath “OU=Room1,OU=Sites,DC=domain,DC=co,DC=uk”

 

This one liner is to move disabled user accounts out of one ou to another

 

Slightly adapting the searchbase you can target computers that are named like *room1-* in the same way

Posted

to be honest for 1000 computers in 1 ou it is probably more time consuming automating it then it would be to drag and drop.

 

granted if you had 1000 computers within a complex ou structure already then I would probably also look to automating it

Posted

If you have already done the piece of work around getting the computer name to OU mapping in to a csv file then the following would do it:-

 

[array]$WksToOu = Import-Csv .\WksToOUs.csv

 

$WksToOu | %{

Get-ADComputer $_.ComputerName | Move-ADObject -TargetPath $_.TargetOU

 

}

 

The CSV would look like this:-

ComputerName,TargetOU

IT-2276,"OU=W10,OU=StaffPC,OU=Workstations,DC=YOURDOM,DC=XX,DC=XX,DC=XX"

TestPC2,"OU=Room2,OU=StudentPC,OU=Workstations,DC=YOURDOM,DC=XX,DC=XX,DC=XX"

TestPC3,"OU=W10,OU=StudentPC,OU=Workstations,DC=YOURDOM,DC=XX,DC=XX,DC=XX"

TestPC4,"OU=W10,OU=StudentPC,OU=Workstations,DC=YOURDOM,DC=XX,DC=XX,DC=XX"

 

I will add as always some notes around this sort of script, although the above code will do the job (as will the code further up) if I was approaching this I would have it in a try/ catch and have some error trapping and the like. Also any code that has write-host in it needs these instantly changing to write-output as you can do something with the output, no one really runs scripts in the console....

  • 4 weeks later...
Posted
Hey all thanks for the replies, we got it sorted in the end and just manually did the majority of them but any bulk computer moves for classrooms we used the script.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...