Create Multiple OUs in AD

Welcome Forums General PowerShell Q&A Create Multiple OUs in AD

This topic contains 3 replies, has 3 voices, and was last updated by

 
Keymaster
6 months ago.

  • Author
    Posts
  • #102478

    Participant
    Points: 0
    Rank: Member

    Hi All!
    I need to create several organizational units in an existing one by import CSV file, using the command new-adorganisationalunit.
    By using this command new-adorganisationalunit -name -path I can create just one at once, how can I use this with import-csv?
    I'm new at scripting, can you help me?

  • #102482

    Keymaster
    Points: 17
    Team Member
    Rank: Member

    Hi there Elmira,

    You can import the csv to a variable and then loop through it. Something like so:

    $csv = Import-csv -Path %PATH%
    ForEach($obj in $csv){
    
    New-ADOrganizationalUntil -Name $obj.Name 
    
    }

    You'll need to map the appropriate properties (column name) to the appropriate parameter (-Parameter $Obj.ColumnName). I don't have a working example right now, but this should get you started if you want to learn. Let us know if you have any questions.

    Also, some additional information on what each parameter expects for an input: https://technet.microsoft.com/es-es/library/hh852233(v=wps.620).aspx

  • #102536

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    Also, you should leverage the -WhatIf switch to show you what the script would do and not actually create the OU's.

    New-ADOrganizationalUnit-Name $obj.Name -WhatIf
    

    This will let you check your work before you make changes and have to delete 100 OU's.

    • #102568

      Keymaster
      Points: 17
      Team Member
      Rank: Member

      ^This. Read my post about PowerShell Math and the accidental creation of 2 million test accounts. 😛

The topic ‘Create Multiple OUs in AD’ is closed to new replies.