0

The code I have used to create user is:

 Import-Module ActiveDirectory

 $total = 2
 for ($userIndex=0; $userIndex -lt $total; $userIndex++) 
 { 
  $userID = “{0:0000}” -f ($userIndex + 1)
  $userName = “Super.admin$userID”

  Write-Host “Creating user” ($userIndex + 1) “of” $total “:” $userName

  New-ADUser `
   -AccountPassword (ConvertTo-SecureString “admin@123” -AsPlainText -Force) `
   -City “City” `
   -Company “Company” `
   -Country “US” `
   -Department “Department” `
   -Description (“TEST ACCOUNT ” + $userID + “: This user account does not represent a real user and is meant for test purposes only”)`
   -DisplayName “Test User ($userID)” `
   -Division “Division” `
   -EmailAddress “[email protected]” `
   -EmployeeNumber “$userID” `
   -EmployeeID “ISED$userID” `
   -Enabled $true `
   -Fax “703-555-$userID” `
   -GivenName “Test” `
   -HomePhone “703-556-$userID” `
   -Initials “TU$userID” `
   -MobilePhone “703-557-$userID” `
   -Name “Super.Admin ($userID)” `
   -Office “Office: $userID”`
   -OfficePhone “703-558-$userID” `
   -Organization “Organization” `
   -Path  "OU=BusinessUnit,DC=Domain,DC=com" `
   -POBox “PO Box $userID”`
   -PostalCode $userID `
   -SamAccountName $userName `
   -State “VA – Virginia” `
   -StreetAddress “$userID Any Street” `
   -Surname “User ($userID)” `
   -Title “Title” `
   -UserPrincipalName “[email protected]“
 }

Under my business unit group HR is created. How can I add a user in this group or create the users and assign the HR group to the users using the above script? I tried to change the -Path

-Path "CN=HR,OU=Utility,DC=DESMOSEDICI,DC=com"

But it is not working.

1 Answer 1

2

Path is the Organizational Unit (or Container) the account will be created in. It has nothing to do with Group membership.

Use:

Add-ADGroupMember "CN=HR,OU=Utility,DC=DESMOSEDICI,DC=com" -Member "[email protected]"

Edit: This shows the command in the context of your script:

Import-Module ActiveDirectory

$total = 2
for ($userIndex=0; $userIndex -lt $total; $userIndex++) { 
    $userID = "{0:0000}" -f ($userIndex + 1)
    $userName = "Super.admin$userID"

    Write-Host "Creating user" ($userIndex + 1) "of" $total ":" $userName

    New-ADUser `
        -AccountPassword (ConvertTo-SecureString "admin@123" -AsPlainText -Force) `
        -City "City" `
        -Company "Company" `
        -Country "US" `
        -Department "Department" `
        -Description ("TEST ACCOUNT " + $userID + ": This user account does not represent a real user and is meant for test purposes only")`
        -DisplayName "Test User ($userID)" `
        -Division "Division" `
        -EmailAddress "[email protected]" `
        -EmployeeNumber "$userID" `
        -EmployeeID "ISED$userID" `
        -Enabled $true `
        -Fax "703-555-$userID" `
        -GivenName "Test" `
        -HomePhone "703-556-$userID" `
        -Initials "TU$userID" `
        -MobilePhone "703-557-$userID" `
        -Name "Super.Admin ($userID)" `
        -Office "Office: $userID"`
        -OfficePhone "703-558-$userID" `
        -Organization "Organization" `
        -Path  "OU=BusinessUnit,DC=Domain,DC=com" `
        -POBox "PO Box $userID"`
        -PostalCode $userID `
        -SamAccountName $userName `
        -State "VA – Virginia" `
        -StreetAddress "$userID Any Street" `
        -Surname "User ($userID)" `
        -Title "Title" `
        -UserPrincipalName "[email protected]"

    Add-ADGroupMember "CN=HR,OU=Utility,DC=DESMOSEDICI,DC=com" -Member "[email protected]"
}

If you are receiving errors from New-ADUser something is wrong with your existing script, the new command is entirely separate and must fall after New-ADUser has done its job.

Sign up to request clarification or add additional context in comments.

7 Comments

Not Working + CategoryInfo : InvalidArgument: (:) [New-ADUser], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.NewADUser
The error you've got it being raised by New-ADUser. The command above can only be executed after New-ADUser has finished. This must fall after your -UserPrincipalName “[email protected] line as a separate command.
Added after -UserPrincipalName Still not working Add-ADGroupMember ` -Identity "CN=SuperAdmin,OU=Utility,DC=Domain,DC=com" ` -Member "[email protected]" `
I've edited the example above, showing the command inline to clarify.
This Error occurred Add-ADGroupMember : Cannot find an object with identity: '[email protected]' under: 'DC=domain,DC=COM'. At line:44 char:3 + Add-ADGroupMember "CN=HR,OU=Utility,DC=domain,DC=com" -Member "$ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: ([email protected]:ADPrincipal) [Add-ADGroupMember], ADIdentity NotFoundException + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddAD GroupMember
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.