locked
In need of a Powershell script to move students from an OU to another OU RRS feed

  • Question

  • Good Day Everyone,

    I was tasked to move students from the Students OU to Full Time / Part Time OU. I can possibly get this done in a short time BUT since I am a newbie, I would like to know what would be the best approach for me to get this done. 

    Details

    1. There are about 1700 students

    2. Import Students from an CSV file to be moved [fulltimestudents.csv AND parttimestudents.csv]

    3. After importing the students > move them to the fulltime or parttime OU

    Any suggestions before I start this and thank you all for your help.

    AM



    Adrian Mohamed

    Friday, October 17, 2014 12:32 PM

Answers

  • Good Morning JRV + All,

    Here is a working code that I am now using for the move - any ideas on how to clean it up + maybe w/ comments would be much appreciated.


    remove-variable * -force

    Remove-Item -Path C:\temp\out.txt -Force

    $FULLTIMEOU='OU=Full Time,OU=Undergraduate,OU=Students,OU=Users,OU=LIM Objects,DC=limcollege,DC=edu'

    $PARTTIMEOU='OU=Part Time,OU=Undergraduate,OU=Students,OU=Users,OU=LIM Objects,DC=limcollege,DC=edu'

    clear

    # Import users from Carolyn’s CSV file [separate files for Full Time + Part Time]

    $user = ipcsv -path C:\temp\test2.csv


    # Get all students distinguished names > output them into a single file

    # Output to a txt gives us a paper trail of the actual students being moved

    foreach ($users in $user)

    {

        (Get-ADUser $users.name).distinguishedname | Out-File C:\temp\out.txt -Append

    }

    # Wait until the above transactions are complete

    Start-Sleep -s 2

    # Import the distinguished names > make the move to new OU

    $name = Get-Content -Path C:\temp\out.txt

    # Reiterate through the student names and perform the move

    foreach ($names in $name)

    {

        Move-AdObject -Identity $names -TargetPath $FULLTIMEOU

    }


    Adrian Mohamed

    • Marked as answer by Adrian Mohamed Tuesday, October 21, 2014 5:06 PM
    Tuesday, October 21, 2014 3:38 PM

All replies

  • Do the users already exist? It sounds like they don't. If the users do not already exist in AD I would use PowerShell to read from the CSV file to create the new user accounts in the intended OU directly rather than moving them.

    Check out New-ADUser.

    Alternatively you could add a distinguishedName in the CSV for the OU you want to import into and use CSVDE and they will end up in the correct OU.

    Note with either method the accounts will be created in a disabled state.


    Friday, October 17, 2014 1:32 PM
  • The CSV needs to have the logon name, user principal name, or the DistinguishedName to find then in AD.

    $targetOU='Ou=someOU,dc=domain,dc=com'
    Import-Csv students.csav | %{ Move-AdObject -Identity $_.SamAccountName -TargerPath $targetOU} 

    That is it.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, October 17, 2014 1:49 PM
    Friday, October 17, 2014 1:49 PM
  • Hi Jason,

    Thanks for your reply.

    I should have mentioned this - these are existing students > we are just merely moving them from ALL Students OU to either FULL TIME OU or PART TIME OU.

    Thank you.

    AM


    Adrian Mohamed

    Friday, October 17, 2014 1:52 PM
  • JRV,

    Thank you - will begin testing this weekend and if all goes well I will post the final code for you to preview + vote.

    Thank you and will keep you guys posted with the progress.

    TY.

    AM


    Adrian Mohamed

    Friday, October 17, 2014 2:23 PM
  • If the student user objects already exist in AD, you should be able to select them in bulk in ADUC, right click, select "Move", and select the new OU. This is much easier.

    Richard Mueller - MVP Directory Services

    Friday, October 17, 2014 4:54 PM
  • Hi Rich,

    Thanks for the reply. That would be the easiest however not all the students are going to a Single OU, depending on the list provided to me, certain students will be distributed to FULL TIME OU or PART TIME OU. So your solution is what HD is doing now but with 1700 students, we cant do that for all. Be here for awhile.

    Thx.


    AM.


    Adrian Mohamed

    Friday, October 17, 2014 5:21 PM
  • Is it easier to pick the students to be added to a csv, than to select them in ADUC? If you can identify the full time students in ADUC it should be easy to Ctrl-Click any subset of the objects in any OU for moving. If you are starting with a list or csv, that is different. Then you just need to make sure each student is identified by either distinguished name or sAMAccountName (pre-Windows 2000 name) in the csv.

    Edit: Oh, and when you move one group, are all the remaining students in the other group? If so, move the smaller group first, then you can move all the remaining in bulk in ADUC.


    Richard Mueller - MVP Directory Services



    Friday, October 17, 2014 6:09 PM
  • my guess is that only administration knows which students are which.  If two spreadsheets are delivered then the stub of code I posted would be easy and fast. 

    Add  "-WhatIf" to the Move-AdObject and capture to a file to validate that it is what you want.  Once you understand how this works you can just use it fairly safely.  After all a move is recoverable.  It is not like a delete.


    ¯\_(ツ)_/¯

    Friday, October 17, 2014 6:39 PM
  • Good Morning JRV + All,

    Here is a working code that I am now using for the move - any ideas on how to clean it up + maybe w/ comments would be much appreciated.


    remove-variable * -force

    Remove-Item -Path C:\temp\out.txt -Force

    $FULLTIMEOU='OU=Full Time,OU=Undergraduate,OU=Students,OU=Users,OU=LIM Objects,DC=limcollege,DC=edu'

    $PARTTIMEOU='OU=Part Time,OU=Undergraduate,OU=Students,OU=Users,OU=LIM Objects,DC=limcollege,DC=edu'

    clear

    # Import users from Carolyn’s CSV file [separate files for Full Time + Part Time]

    $user = ipcsv -path C:\temp\test2.csv


    # Get all students distinguished names > output them into a single file

    # Output to a txt gives us a paper trail of the actual students being moved

    foreach ($users in $user)

    {

        (Get-ADUser $users.name).distinguishedname | Out-File C:\temp\out.txt -Append

    }

    # Wait until the above transactions are complete

    Start-Sleep -s 2

    # Import the distinguished names > make the move to new OU

    $name = Get-Content -Path C:\temp\out.txt

    # Reiterate through the student names and perform the move

    foreach ($names in $name)

    {

        Move-AdObject -Identity $names -TargetPath $FULLTIMEOU

    }


    Adrian Mohamed

    • Marked as answer by Adrian Mohamed Tuesday, October 21, 2014 5:06 PM
    Tuesday, October 21, 2014 3:38 PM
  • I think the code looks fine. I just find this syntax confusing at first glance:

    ForEach ($Users in $User)

    It is correct in this case, but $User is an array, and $Users in one element of the array. I would expect the reverse. But minor point.

    Edit: Also, I cannot imagine the Start-Sleep would ever be necessary. The only time a pause might be required is if the first process is updating AD and you need to wait for synchronization before being certain an subsequent query will reflect the update.


    Richard Mueller - MVP Directory Services


    Tuesday, October 21, 2014 3:49 PM