none
How to move users between OU's from an exported list RRS feed

  • Question

  • Hey Guys

    New to Powershell

    We are going to move large number of users from one OU to another.. For that I am using

    get-aduser -filter * -searchbase "OU=ou1,DC=domain" | move-adobject -targetpath "OU=ou2,DC=domain"

    As a backout plan we want to reverse that.. so what I am thinking is to right click on the OU1 and export list as txt. So we have all users in a tab/csv format with username.. How can I then write a one liner / script to search for all the users in the list in AD (or in specific OU2 in AD) and then move them to another OU (or OU1 as above example)?

    Or if there is a better quicker method please let me know.

    Cheers

    Thursday, June 19, 2014 4:13 AM

Answers

  • Simple plan.  SAve user samname and ou in CSV.  Move users.

    Get-AddUser -Filter * -Target $ou1 |
         select samaccountnaame, @{N='OriginalOU';E={$OU1}} |
         Export-Csv before.csv

    Now move users.  That is all.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, June 19, 2014 5:01 AM
    • Marked as answer by FIM N00b Thursday, June 19, 2014 5:42 AM
    Thursday, June 19, 2014 5:00 AM
  • Thanks

    And how do I reverse this from the csv file? So select all users from the CSV file in OU2 and move them to OU1 ?

    Just read the file in and get the users and move them back.

    Import-CSv  before.csv |
         ForEach-Object[
              Get-AdUser $_.SamAccountName | Move-AdUser -Target $_.OriginalOU
         }


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, June 19, 2014 5:05 AM
    • Marked as answer by FIM N00b Thursday, June 19, 2014 5:45 AM
    Thursday, June 19, 2014 5:05 AM

All replies

  • What specific thing is it that you do not know how to do?  Are you asking how to export users to a CSV?

    Your question is a bit vague.

    What do you want to export?

    What do you want to move?

    What is the purpose?

    If you can answer all of those questions and say what it is you do not know how to do.


    ¯\_(ツ)_/¯

    Thursday, June 19, 2014 4:37 AM
  • Hi,

    Here's something to start with:

    $targetOU = 'OU=Test 2,DC=domain,DC=com'
    
    Get-ADUser -Filter * -SearchBase 'OU=Test 1,DC=domain,DC=com' | ForEach {
    
        $props = @{
            Name = $_.Name
            Username = $_.SamAccountName
            OriginalOU = ($_.DistinguishedName -split ',OU=' -split ',')[1]
            NewOU = ''
            Status = ''
        }
    
        try {
    
            Move-ADObject -Identity $_.DistinguishedName -TargetPath $targetOU -ErrorAction Stop
            $props.NewOU = ($targetOU -split ',' -replace 'OU=','')[0]
            $props.Status = 'Success'
        
        } catch {
    
            $props.Status = 'FAILED'
    
        }
    
        New-Object PsObject -Property $props
    
    } | Sort Name | Select Name,Username,OriginalOU,NewOU,Status | Export-Csv .\userMoveDetails.csv -NoTypeInformation
    


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Thursday, June 19, 2014 4:40 AM
  • Mike - how were you able to decode that?  That is one interpretation.  Are you sure it is the correct one?

    To me it sounds like he wants to export the samname to CSV for all users before being moved then move and have a memory of who ws originally where but that seems unnecessary.


    ¯\_(ツ)_/¯

    Thursday, June 19, 2014 4:46 AM
  • What specific thing is it that you do not know how to do?  Are you asking how to export users to a CSV?

    Your question is a bit vague.

    What do you want to export?

    What do you want to move?

    What is the purpose?

    If you can answer all of those questions and say what it is you do not know how to do.


    ¯\_(ツ)_/¯

    Want to move all users from one OU to another.. purpose is restructure of OU.. backout plan is needed and need to move all those users back to OU1 if for some reason other systems fail..

    Should be very simple move as stated with my above code.. all found in container OU1 moved to OU2.. Just want to be able to do the reverse..

    One thing to point out is that OU2 will have existing users.. so I need to differentiate between users who came from OU1 to OU2 .. and therefore was thinking an export by right clicking on OU1

    Thursday, June 19, 2014 4:53 AM
  • Hi,

    Here's something to start with:

    $targetOU = 'OU=Test 2,DC=domain,DC=com'
    
    Get-ADUser -Filter * -SearchBase 'OU=Test 1,DC=domain,DC=com' | ForEach {
    
        $props = @{
            Name = $_.Name
            Username = $_.SamAccountName
            OriginalOU = ($_.DistinguishedName -split ',OU=' -split ',')[1]
            NewOU = ''
            Status = ''
        }
    
        try {
    
            Move-ADObject -Identity $_.DistinguishedName -TargetPath $targetOU -ErrorAction Stop
            $props.NewOU = ($targetOU -split ',' -replace 'OU=','')[0]
            $props.Status = 'Success'
        
        } catch {
    
            $props.Status = 'FAILED'
    
        }
    
        New-Object PsObject -Property $props
    
    } | Sort Name | Select Name,Username,OriginalOU,NewOU,Status | Export-Csv .\userMoveDetails.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Sorry new to coding.. but this looks awefully complicated for my situation.. Won't the code which I have said simple move users from one container to another existing one? Just need to know how to do the reverse if needed .. OU2 will already have users.. so need to differentiate and was thinking the right click on OU1 and export list.

    Thanks

    Thursday, June 19, 2014 4:55 AM
  • Simple plan.  SAve user samname and ou in CSV.  Move users.

    Get-AddUser -Filter * -Target $ou1 |
         select samaccountnaame, @{N='OriginalOU';E={$OU1}} |
         Export-Csv before.csv

    Now move users.  That is all.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, June 19, 2014 5:01 AM
    • Marked as answer by FIM N00b Thursday, June 19, 2014 5:42 AM
    Thursday, June 19, 2014 5:00 AM
  • Thanks

    And how do I reverse this from the csv file? So select all users from the CSV file in OU2 and move them to OU1 ?

    Thursday, June 19, 2014 5:02 AM
  • Want to move all users from one OU to another.. purpose is restructure of OU.. backout plan is needed and need to move all those users back to OU1 if for some reason other systems fail..

    Should be very simple move as stated with my above code.. all found in container OU1 moved to OU2.. Just want to be able to do the reverse..

    One thing to point out is that OU2 will have existing users.. so I need to differentiate between users who came from OU1 to OU2 .. and therefore was thinking an export by right clicking on OU1

    You are overthinking this. You only need a list of users that you are going to move and the name of the source OU.

    ¯\_(ツ)_/¯

    Thursday, June 19, 2014 5:03 AM
  • Thanks

    And how do I reverse this from the csv file? So select all users from the CSV file in OU2 and move them to OU1 ?

    Just read the file in and get the users and move them back.

    Import-CSv  before.csv |
         ForEach-Object[
              Get-AdUser $_.SamAccountName | Move-AdUser -Target $_.OriginalOU
         }


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, June 19, 2014 5:05 AM
    • Marked as answer by FIM N00b Thursday, June 19, 2014 5:45 AM
    Thursday, June 19, 2014 5:05 AM
  • Thanks

    So I guess I will need to define the following somewhere as I was just trying to run as a single line command from prompt instead of a script.

    $ou1

    @{N='OriginalOU';E={$OU1}}

    $_.SamAccountName

    $_.OriginalOU

    Thursday, June 19, 2014 5:08 AM
  • I think you need to learn a little PowerShell.

    The code I posted needs nothing to be defined.

    The $ou1 I all that you need to assign.  Just assign it to the source OU distinguished Name.


    ¯\_(ツ)_/¯

    Thursday, June 19, 2014 5:14 AM
  • how were you able to decode that?

    Magic 8ball. I swear it works.

    That is one interpretation. Are you sure it is the correct one?

    Nope.

    To me it sounds like he wants to export the samname to CSV for all users before being moved then move and have a memory of who ws originally where but that seems unnecessary.

    Yeah, that's basically my assumption as well. I figured I'd just mash the reporting in with the actual move to save a step.

    I think the real lesson in all this for me is this: I should stop handing out these candybars so quickly.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Thursday, June 19, 2014 5:17 AM
  • Mike - see why I questioned the original post.  It was sort of hinted that the OP did not know PowerShell of AD and was looking for something simple but did not know how to ask the question.  Once a user understands how these things are done and defined the questions get clearer.

    FIM - you see my issues now I believe. By the time you finish this exercise you will know a ton more and ask better questions.

    Boot-strapping into scripting for the first time is hard.  Books are a big help.


    ¯\_(ツ)_/¯

    Thursday, June 19, 2014 5:18 AM
  • how were you able to decode that?

    Magic 8ball. I swear it works.

    That is one interpretation. Are you sure it is the correct one?

    Nope.

    To me it sounds like he wants to export the samname to CSV for all users before being moved then move and have a memory of who ws originally where but that seems unnecessary.

    Yeah, that's basically my assumption as well. I figured I'd just mash the reporting in with the actual move to save a step.

    I think the real lesson in all this for me is this: I should stop handing out these candybars so quickly.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)


    That is why I do not like to do whole script answers. It does not help anyone learn how it works.  YOur script does everything soup-to-nuts but is not a one liner. It is a real script.

    ¯\_(ツ)_/¯

    Thursday, June 19, 2014 5:20 AM
  • If you are interested in typing things on one line then this can  be easily done:

    This:

    Import-CSv  before.csv |
         ForEach-Object[
              Get-AdUser $_.SamAccountName | Move-AdUser -Target $_.OriginalOU
         }

    Can be done like this:

    Import-CSv  before.csv | %[ Get-AdUser $_.SamAccountName | Move-AdUser -Target $_.OriginalOU }

    One line.

    The original is also a "one liner" but it is spread over 4 lines as it is easier to type that way.


    ¯\_(ツ)_/¯

    Thursday, June 19, 2014 5:41 AM
  • THe following is also a "one-liner":

    Get-AddUser -Filter * -Target $ou1 |
         select samaccountnaame, @{N='OriginalOU';E={$OU1}} |
         Export-Csv before.csv

    See:

    Get-AddUser -Filter * -Target $ou1 |  select samaccountnaame, @{N='OriginalOU';E={$OU1}} | Export-Csv before.csv

    Same thing just spread out.

    Lines in PowerShell naturally extend on all incomplete structures like these: {} [] () and comma separated lists as well as other syntactic structures.


    ¯\_(ツ)_/¯

    Thursday, June 19, 2014 5:44 AM
  • Thanks

    Got it to work.. had to use

    move-adobject -targetpath

    Understood all the variables.. :)

    Thursday, June 19, 2014 5:46 AM