none
Move Computer account AD to multiple OU from csv file RRS feed

  • Question

  • Hello,

    I know to move computer account AD to the OU :

     gc d:\temp\comps.txt | Get-ADComputer | Enable-ADAccount | Move-ADObject --TargetPath 'OU=humanresources,OU=Workstation,DC=society,DC=ad,DC=com'

    But I need to move a computers account AD list with different OU for each computer.

    I create csv file with two columns (name,newOU) but I need your help to modify this script.

    Thursday, October 6, 2016 12:41 PM

Answers

  • Hi Puffydee,

    you can import csv files using the ... Import-Csv cmdlet. This will then return objects, one per row, with properties named for the columns.

    Example:

    $computers = Import-Csv comps.csv
    foreach ($computer in $computers)
    {
        Get-ADComputer $computer.Name | Enable-ADAccount | Move-ADObject -TargetPath $computer.newOU
    }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Proposed as answer by Mike Laughlin Thursday, October 6, 2016 1:15 PM
    • Marked as answer by puffydee13 Thursday, October 6, 2016 1:16 PM
    Thursday, October 6, 2016 1:01 PM
  • Hi Puffydee,

    this error means that there was no correct column named "Name" in the csv.

    Check your csv:

    • Is the column named correctly?
    • Is it delimited by "," or is it in fact instead delimited by ";"?

    If it is delimited by ";", replace "Import-Csv comps.csv" with "Import-Csv comps.csv -Delimiter ";"".

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by puffydee13 Thursday, October 6, 2016 1:16 PM
    Thursday, October 6, 2016 1:09 PM

All replies

  • Hello,

    $source = d:\temp\comps.txt

    get-adcomputer $source | Move-ADObject -TargetPath 'OU=humanresources,OU=Workstation,DC=society,DC=ad,DC=com'

    cheers

    nuno



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 6, 2016 12:58 PM
  • Thank you but like I said I know this command, but I need to move computers in multiple OU, not just one; for each computer in the different OU.
    Thursday, October 6, 2016 1:00 PM
  • Hi Puffydee,

    you can import csv files using the ... Import-Csv cmdlet. This will then return objects, one per row, with properties named for the columns.

    Example:

    $computers = Import-Csv comps.csv
    foreach ($computer in $computers)
    {
        Get-ADComputer $computer.Name | Enable-ADAccount | Move-ADObject -TargetPath $computer.newOU
    }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Proposed as answer by Mike Laughlin Thursday, October 6, 2016 1:15 PM
    • Marked as answer by puffydee13 Thursday, October 6, 2016 1:16 PM
    Thursday, October 6, 2016 1:01 PM
  • i dont have a way to test it. please check if this helps.
    import-csv c:\temp\serverdata.csv | ForEach { get-adcomputer $_.name | Move-ADObject -TargetPath $_.newOU}

    Thursday, October 6, 2016 1:02 PM
  • Thank you Fred, I try your script but I have this message :

    Get-ADComputer : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
    At D:\temp\test.ps1:4 char:20
    +     Get-ADComputer $computer.Name | Enable-ADAccount | Move-ADObject -TargetPath ...
    +                    ~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-ADComputer], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

    Thursday, October 6, 2016 1:04 PM
  • Hi Puffydee,

    this error means that there was no correct column named "Name" in the csv.

    Check your csv:

    • Is the column named correctly?
    • Is it delimited by "," or is it in fact instead delimited by ";"?

    If it is delimited by ";", replace "Import-Csv comps.csv" with "Import-Csv comps.csv -Delimiter ";"".

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by puffydee13 Thursday, October 6, 2016 1:16 PM
    Thursday, October 6, 2016 1:09 PM
  • Thank you, you're the best !
    Thursday, October 6, 2016 1:16 PM
  • Also, the field that identifies the computer in AD must be one supported by the Get-ADComputer cmdlet. It must be either the sAMAccountName, the distinguishedName, the SID, or the GUID. The cmdlet won't work if the value is the RDN (the Relative Distinguished Name, also called the common name), as this does not uniquely identify the object.

    If you know for certain that the common name of the computer (the value of the cn attribute) uniquely identifies the object in your domain, then you can filter on the name instead. If there is more than one computer with the name, Get-ADUser will retrieve all such objects.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Thursday, October 6, 2016 1:20 PM
    Moderator