0
votes

I am working on a PowerShell command where I have a .csv file with a certain attributes, but am actually stuck in completing it.

The attributes of my .csv file are in the following order:

  • userPrincipalName
  • sAMAccountName
  • password
  • givenName
  • sn
  • displayName
  • description
  • Path
  • title
  • company
  • memberOf
  • department
  • mustChangePassword

My current code is this:

$Users = Import-Csv -Path "C:\BulkUsers.csv"            
foreach ($User in $Users)            
{      
    $UPN = $User.userPrincipalName
    $SAM = $User.sAMAccountName
    $Password = $User.password     
    $UserFirstname = $User.givenName            
    $UserLastname = $User.sn 
    $Displayname = $User.givenName + " " + $User.sn  
    $Description = $User.description  
    $Path = $User.Path            
    $Title = $User.title
    $Company = $User.company
    $Group = $User.memberOf
    $Department = $User.department          

    New-ADUser -UserPrincipalName $UPN -SamAccountName $SAM -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -GivenName "$UserFirstname" -Surname "$UserLastname" -DisplayName "$Displayname" -Description "$Description" -Path "$Path" -title "$Title" -company "$Company" -memberOf  "$Group" -department "$Department" -ChangePasswordAtLogon $true
}

Am getting the below error while executing it:

New-ADUser : A parameter cannot be found that matches parameter name 'memberOf'.
At line:15 char:292
+ ... any "$Company" -memberOf  "$Group" -department "$Department" -ChangePasswordAtLo ...
+                    ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.NewADUser
1
Did you delete and reask this question? Did you see Ansgar's points about the last question. Your are not asking anything here. What is wrong with the code? Are you getting an error? You need to tell us what your problem is and your expected results.Matt
Yes.. sorry i forgot to mention the error code, i have edited the post above, now can u help me ??user2700217
Instead of deleting and re-posting a question you should improve the question you already asked. Simply deleting questions when they contract downvotes is bound to lead to a question ban sooner or later.Ansgar Wiechers
Well, thanks for highlighting that. Now well you be able to help on the above error for memberOf attribute error, my code works find if i delete that?? so where is the mistake?user2700217
Patience, young grasshopper. I was already working on that.Ansgar Wiechers

1 Answers

1
votes

The error message is pretty straightforward. If you take a look at the documentation for the New-ADUser cmdlet you'll see that it doesn't have a parameter -memberOf. To add the newly created user to a group use the Add-ADGroupMember cmdlet.

$Account = New-ADUser ... -PassThru
Add-ADGroupMember -Identity $Group -Members $Account

This is assuming that the memberOf field from the CSV contains just a single group name or distinguished name.

The additional parameter -PassThru allows you to assign the created account object to a variable, so you can use that variable in the group assignment. Without that parameter New-ADUser runs silently (without output).