harsh sharma
harsh sharma

Reputation: 75

How to add user in a Active directory Group using powershell

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.

Upvotes: 0

Views: 1423

Answers (1)

Chris Dent
Chris Dent

Reputation: 4260

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.

Upvotes: 2

Related Questions