title: how to add users into a specific group in active directory based on the user selection type powershell
body: <p>I am working a script to add user to a specific group at the moment user gets created as a normal domain user but i am not sure how to add a user to an admin group of tester group.Also how to check if user doesnot enter first name i should prompt user to enter first name I am  using the following script</p>
  
  <pre><code>$NewUser = Read-Host "New Username"
  $firstname = Read-Host "First Name"
  $Lastname = Read-Host "Last Name"
  $NewName = "$firstname $lastname"
  
  
  New-ADUser -SamAccountName $NewUser -Name $NewName -GivenName $firstname -Surname  
  $lastname -displayname  $Newname -UserPrincipalName $firstname@handmade.local -Path   
  "CN=Users,DC=mydomain,DC=local" -AccountPassword (ConvertTo-SecureString -AsPlainText 
  "Password" -Force)  -Enabled $true
  
  write-host "!!!New User"$NewUser "Created!!!"
  </code></pre>
  
  <p>I am getting the following error:
  when I run this script</p>
  
  <pre><code>New-ADUser -SamAccountName $NewUser -Name $NewName -GivenName $FirstName -Surname   
  $LastName -displayname  $NewName -UserPrincipalName $FirstName@mydomain.local -Path 
  "CN=Users,DC=mydomain,DC=local" -AccountPassword (ConvertTo-SecureString -AsPlainText 
  "password" -Force)  -Enabled $true 
  
  
  $h = @{1="Dev";2="Admins"}
   Write-Host "Please select:"
   $h.getEnumerator() |sort | % {
  Write-Host ([string]$_.Name +". " + $_.Value)
  } 
  Write-Host
  [string]$g = Read-Host -Prompt "Select group number or numbers, coma separated"
  
  $groups = $g -split ","
  
  foreach($group in $groups){
  Add-ADGroupMember -Identity $h[$group] -Members $NewUser
  } 
  
  
  write-host "!!!New User"$NewUser "Created!!!"
  exit 0
   }
  
  }
  </code></pre>
  
  <p>Error:
  Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument
  and try the command again.
  At C:\Users\Administrator\Desktop\add user to AD.ps1:60 char:33
  +     Add-ADGroupMember -Identity $h[$group] -Members $NewUser</p>
  
  <p>I am selecting the groups as 1,2</p>