locked
move users to a new OU, getting information from .csv file. RRS feed

  • Question

  • I need a way to move users to a new OU, getting information for using via .csv file.

    For example, I have some users with there login name need to move them to OU1

    another users another files need to move them to OU2

    any advice ?


    @hmad

    Tuesday, October 16, 2012 12:37 PM

Answers

  • An example using AD Modules in PowerShell:

    # Specify target OU.
    $TargetOU = "ou=NewUsers,ou=West,dc=MyDomain,dc=com"

    # Read user sAMAccountNames from csv file (field labeled "Name").
    Import-Csv -Path Users.csv | ForEach-Object {
        # Retrieve DN of User.
        $UserDN = (Get-ADUser -Identity $_.Name).distinguishedName

        # Move user to target OU.
        Move-ADObject -Identity $UserDN -TargetPath $TargetOU
    }

    -----



    Richard Mueller - MVP Directory Services

    • Proposed as answer by Bigteddy Tuesday, October 16, 2012 4:52 PM
    • Marked as answer by m_hmad Tuesday, October 16, 2012 5:13 PM
    Tuesday, October 16, 2012 4:50 PM

All replies

  • An example using AD Modules in PowerShell:

    # Specify target OU.
    $TargetOU = "ou=NewUsers,ou=West,dc=MyDomain,dc=com"

    # Read user sAMAccountNames from csv file (field labeled "Name").
    Import-Csv -Path Users.csv | ForEach-Object {
        # Retrieve DN of User.
        $UserDN = (Get-ADUser -Identity $_.Name).distinguishedName

        # Move user to target OU.
        Move-ADObject -Identity $UserDN -TargetPath $TargetOU
    }

    -----



    Richard Mueller - MVP Directory Services

    • Proposed as answer by Bigteddy Tuesday, October 16, 2012 4:52 PM
    • Marked as answer by m_hmad Tuesday, October 16, 2012 5:13 PM
    Tuesday, October 16, 2012 4:50 PM
  • Thx alot Richard, is there any AD Modules in PowerShell to rename computers in AD.


    @hmad

    Tuesday, October 16, 2012 5:15 PM
  • Yes, there is a Rename-ADObject cmdlet. You can check what is available, and get syntax help and examples, using Get-Help at a PowerShell prompt. For example, to find Rename cmdlets:

    Get-Help Rename*

    And to get full help for this cmdlet:

    Get-Help Rename-ADObject -Full


    Richard Mueller - MVP Directory Services

    Tuesday, October 16, 2012 5:39 PM
  • Yes, I tried Rename-ADObject, but it did not work, in users it work !!! Any another suggestion ?

    Even I tried ActiveRoles Management Shell for Active Directory from quest, I could not find anything !


    @hmad

    Wednesday, October 17, 2012 8:35 PM
  • Hi Richard,

    I do appreciate taking the time to help.

    Running the script on my end gave me error below.  Your help is much appreciated.

    The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:7 char:26
    +     $UserDN = (Get-ADUser <<<<  -Identity $_.Name).distinguishedName
        + CategoryInfo          : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
     
    The term 'Move-ADObject' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try agai
    n.
    At line:10 char:18
    +     Move-ADObject <<<<  -Identity $UserDN -TargetPath $TargetOU
        + CategoryInfo          : ObjectNotFound: (Move-ADObject:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Wednesday, January 15, 2014 5:37 PM
  • Hi,

    This thread is already marked as answered. If you still need help, please start a new question. You can reference this thread, if appropriate.

    Bill

    Wednesday, January 15, 2014 5:38 PM
  • Bill is right, you should start your own thread.

    But since this could be a simple fix, I'll post a quick tip. Those cmdlets are part of the AD module. Make sure that it's available and that you're importing it properly (Import-Module ActiveDirectory).


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

    Wednesday, January 15, 2014 5:43 PM
  • Richard

    I was wondering could I make the $TargetOU a variable in the csv file with the username?

    Thursday, March 26, 2015 12:13 AM