none
Move AD users around AD OUs based on CSV RRS feed

  • Question

  • Hi,

    I have a CSV file with below columns:

    samaccountname,OU_Distinguished_Name

    user1,OU=IT,DC=X,DC=com

    user2,OU=HR,DC=X,DC=com

    Can you help me with a script to move users to their intended OUs using the CSV above?

    Monday, March 26, 2018 3:43 PM

Answers

  • Something such as:


    Import-Csv "C:\Scripts\csvfile.csv" | ForEach-Object {
      $user = Get-ADUser $_.samaccountname
      if ( $user ) {
        Move-ADObject $user -TargetPath $_.OU_Distinguished_Name -WhatIf
      }
    }
    

    This presumes the column names match your example and you have a valid CSV file.

    Of course you would remove the -WhatIf parameter if the command does what you need.

    We would strongly recommend testing this command in a test environment first before making changes in a production environment, particularly if you are not familiar with PowerShell and/or scripting.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Monday, March 26, 2018 9:32 PM
    • Marked as answer by AhmadJY Tuesday, March 27, 2018 10:30 AM
    Monday, March 26, 2018 4:26 PM
    Moderator

All replies

  • This forum doesn't write scripts for you, you can search the gallery or Google for examples. Let us know what you have tried so far and what specific issues you are getting.

    Start researching the "import-csv", "get-aduser" and "set-aduser" commands


    Learn PowerShell                     Script Requests

    -Remember to mark the correct response as the answer-

    Monday, March 26, 2018 4:14 PM
  • Something such as:


    Import-Csv "C:\Scripts\csvfile.csv" | ForEach-Object {
      $user = Get-ADUser $_.samaccountname
      if ( $user ) {
        Move-ADObject $user -TargetPath $_.OU_Distinguished_Name -WhatIf
      }
    }
    

    This presumes the column names match your example and you have a valid CSV file.

    Of course you would remove the -WhatIf parameter if the command does what you need.

    We would strongly recommend testing this command in a test environment first before making changes in a production environment, particularly if you are not familiar with PowerShell and/or scripting.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Monday, March 26, 2018 9:32 PM
    • Marked as answer by AhmadJY Tuesday, March 27, 2018 10:30 AM
    Monday, March 26, 2018 4:26 PM
    Moderator