Author Posts

November 17, 2014 at 1:07 pm

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.

November 17, 2014 at 9:13 pm

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
    }
}