Using powershell to import a csv file for OU's

Welcome Forums General PowerShell Q&A Using powershell to import a csv file for OU's

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

 
Participant
4 years, 6 months ago.

  • Author
    Posts
  • #14113

    Participant
    Points: 0
    Rank: Member

    I'm trying to import a csv file to create OU's in active directory using powershell. Now I have figured out to set it up to get the file. What I haven't been able to figure out is the scripting to get the names and create the actual UO's. I've searched the web and everyone has a different setup. I've attached a copy of my csv file of what I'm try to setup as Ou's. This is what i have started but after that I'm lost. Any help would be appreciated.


    Import-Csv "OUspreadsheet.csv" | ForEach-Object {
    -ParentContainer $_."GroupLocation"
    -SamAccountName $_."Logon Username"
    -FirstName $_."First Name"
    -LastName $_."Last Name"
    -Description $_."Member"

  • #14142

    Participant
    Points: 0
    Rank: Member

    no help on this please?

  • #14143

    Participant
    Points: 0
    Rank: Member

    Assuming you have RSAT tools installed, you can use the AD module and do something like this:
    # OU.csv
    #
    #"Name","Path"
    #"MyNewOU1", "DC=FABRIKAM,DC=COM"
    #"MyNewOU2", "DC=FABRIKAM,DC=COM"
    #"MyNewOU3", "DC=FABRIKAM,DC=COM"

    Import-Module ActiveDirectory
    Import-CSV C:\Users\rob\desktop\OU.csv | ForEach{
    New-ADOrganizationalUnit -Name $_.Name -Path $_.Path -WhatIf
    }

    or simplify it further leveraging the values via pipeline, since the CSV has a "Name" and "Path" matching the switches you can just do this:

    Import-CSV C:\Users\rob\desktop\OU.csv | New-ADOrganizationalUnit -WhatIf

    either should produce this:

    What if: Performing operation "New" on Target "OU=MyNewOU1,DC=FABRIKAM,DC=COM".
    What if: Performing operation "New" on Target "OU=MyNewOU2,DC=FABRIKAM,DC=COM".
    What if: Performing operation "New" on Target "OU=MyNewOU3,DC=FABRIKAM,DC=COM".

    You'll just need to remove the -Whatif when you are ready to make the actual changes.

  • #14150

    Participant
    Points: 0
    Rank: Member

    I'm configuring this on Windows server 2012 so not using Rsat tools. Is that going to bring in whats on the spreadsheet?

  • #14171

    Participant
    Points: 0
    Rank: Member

    You don't have to be on a specific server, the previously shared code could be run from any machine with RSAT tools. Otherwise, you would need to use ADSI to create the OU's:

    # OU.csv
    #
    #"Name","Path"
    #"MyNewOU1", "DC=FABRIKAM,DC=COM"
    #"MyNewOU2", "DC=FABRIKAM,DC=COM"
    #"MyNewOU3", "DC=FABRIKAM,DC=COM"

    Import-CSV C:\Users\rob\desktop\OU.csv | ForEach{
    $OU = [adsi]("LDAP://{0}" -f $_.Path)
    $CreateOU = $OU.Create("OrganizationalUnit",("OU={0}" -f $_.Name))
    $CreateOU.SetInfo()

    }

    I did not test the above code, so be sure to test the code prior to running against an entire CSV by just running the bolded code with static values.

  • #14172

    Participant
    Points: 0
    Rank: Member

    Well I went an built a server 2012 virtual machine and been trying to figure things out from there. Tried running the script with changing where my spread heet is located and got errors. Attached are the errors. I feel so lost on this powershell stuff. I would give up on it if I didn't need to get it done.

  • #14174

    Participant
    Points: 0
    Rank: Member

    Did you test creating a single test OU to validate the code? Try running something simple like this:

    $OU = [adsi]“LDAP://DC=YourDomain,DC=com"
    $CreateOU = $OU.Create(“OrganizationalUnit”,“OU=Test”)
    $CreateOU.SetInfo()

    The rest of the code is just looping through the CSV and replacing static values with variables from the CSV. Also, sometimes when posting code from the internet the quotes get messed up. Above, the code I copied has a different quote in front of LDAP versus the quote after DC=com, so I would just replace all of the quotes in the code inside the editor you are using.

  • #14175

    Participant
    Points: 0
    Rank: Member

    Well I tried to do a test but came up with errors again. Less errors though (see attached).

The topic ‘Using powershell to import a csv file for OU's’ is closed to new replies.