Automating MemberOf category for AD

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Craig Duff Craig Duff 2 years ago.

  • Author
    Posts
  • #20641
    Profile photo of Eric Bronnert
    Eric Bronnert
    Participant

    As the title says, I'm trying to figure out how to automate the MemberOf section on AD via Powershell by importing a CSV file and piping it into New-ADUser. I have looked through ADSI Edit and ADUsers syntax and can not find anything to use as a header for my information. What am I missing? I have tried using headers on my csv like MemberOf and Identity but to no avail.

  • #20645
    Profile photo of Craig Duff
    Craig Duff
    Participant

    If you are trying to import a csv file and pass it directly to new-aduser, then you are doing something called passing by property name. I.e. if you are doing this:

    Import-CSV c:\in.csv | New-ADUser

    To find out what the header/property names you can use do this:

    Get-Help New-ADUser -Full

    And look for properties that say: Accept pipeline input? True (ByPropertyName)

    There is no property for New-ADUser that lets you define it's memberships, bypropertyname or otherwise.

    You would have to loop thru the lines in the csv, and for each iteration of the loop:
    [ol]
    [li]run New-ADUser[/li]
    [li]loop thru the groups (depends on how you have the groups in the csv)[/li]
    [li]add user to group (Add-ADGroupMember)[/li]
    [/ol]

    Example:

    ForEach($line in $csv){
        $line | select-object Name,SamAccountName,SurName,GivenName | New-ADUser
        ForEach($group in ($line.groups.split(";"))){
            Add-ADGroupMember -Identity $group -Members $line.SamAccountName
        }
    }
    

You must be logged in to reply to this topic.