How to add group to ad user while creating a user in powershell

This is what I have so far… I am basically trying to add any user that I create here automatically to the 3 groups I listed below

Please scroll down to the area where I have Add-AD GroupMember

$title = "Create a User Account in Active Directory"

Import-Module ActiveDirectory -EA Stop

sleep 5
cls

Write Host
$firstname = Read-Host "Enter First Name"
Write Host
$lastname = Read-Host " Enter Last Name"
Write Host
$fullname = Read-Host "$firstname $lastname"
Write Host
$i = 1
$logonname = $firstname.substring(0,$i) + $lastname
$OU = "OU=Testing, DC=silvertech, DC=local"
$domain = $env:userdnsdomain
$Description = Read-Host "Enter in the User Description"

DO
{
If ($(Get-ADUser -Filter {SamAccountName -eq $logonname})) {
        Write-Host "WARNING: Logon name" $logonname.toUpper() "already exists!!" -ForegroundColor:Green
        $i++
        $logonname = $firstname.substring(0,$i) + $lastname
        Write-Host
        Write-Host
        Write-Host "Changing Logon name to" $logonname.toUpper() -ForegroundColor:Green
        Write-Host
        $taken = $true
        sleep 10
    } else {
    $taken = $false
    }
} Until ($taken -eq $false)
$logonname = $logonname.toLower()

cls
#Displaying Account information.
Write-Host "======================================="
Write-Host
Write-Host "Firstname:      $firstname"
Write-Host "Lastname:       $lastname"
Write-Host "Display name:   $fullname"
Write-Host "Logon name:     $logonname"
Write-Host "OU:             $OU"
Write-Host "Domain:         $domain"

#Setting minimum password length to 10 characters and adding password complexity.
$PasswordLength = 10
 
Do
{
Write-Host
    $isGood = 0
    $Password = Read-Host "Enter in the Password" -AsSecureString
    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
    $Complexity = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
 
    if ($Complexity.Length -ge $PasswordLength) {
                Write-Host
            } else {
                Write-Host "Password needs $PasswordLength or more Characters" -ForegroundColor:Green
        }
 
    if ($Complexity -match "[^a-zA-Z0-9]") {
                $isGood++
            } else {
                Write-Host "Password does not contain Special Characters." -ForegroundColor:Green
        }
 
    if ($Complexity -match "[0-9]") {
                $isGood++
            } else {
                Write-Host "Password does not contain Numbers." -ForegroundColor:Green
        }
 
    if ($Complexity -cmatch "[a-z]") {
                $isGood++
            } else {
                Write-Host "Password does not contain Lowercase letters." -ForegroundColor:Green
        }
 
    if ($Complexity -cmatch "[A-Z]") {
                $isGood++
            } else {
                Write-Host "Password does not contain Uppercase letters." -ForegroundColor:Green
        }
 
} Until ($password.Length -ge $PasswordLength -and $isGood -ge 3)

Write-Host
Read-Host "Press Enter to Continue Creating the Account"
Write-Host "Creating Active Directory user account now" -ForegroundColor:Green
 
#Creating user account with the information you inputted.
New-ADUser -Name $fullname -GivenName $firstname -Surname $lastname -DisplayName $fullname -SamAccountName $logonname -UserPrincipalName $logonname@$Domain -AccountPassword $password -Enabled $true -Path $OU -Description $Description -Confirm:$false
 
**sleep 2**
**Add-ADGroupMember -Identity "groupname" -Members "epaida"**
**Add-ADGroupMember -Identity "groupname" -Members "epaida"**
**Add-ADGroupMember -Identity "groupname" -Members "epaida"**

What would I replace the username "epaida" with? above? Would it be $firstname?
 
 
Write-Host
 
$ADProperties = Get-ADUser $logonname -Properties *
 
Sleep 3
 
cls
 
Write-Host "========================================================"
Write-Host "The account was created with the following properties:"
Write-Host
Write-Host "Firstname:      $firstname"
Write-Host "Lastname:       $lastname"
Write-Host "Display name:   $fullname"
Write-Host "Logon name:     $logonname"
Write-Host "OU:             $OU"
Write-Host "Domain:         $domain"
Write-Host
Write-Host

Without digging too deep in your convoluted code … :wink:

New-ADUser does not output anything by default. But you can add the parameter -PassThru to return the object refencing the newly created user. You cann use this object to add it to the needed groups.

I actually figured it out… thank you…

I just did:
Add-ADGroupMember -Identity "Staff" -Members $logonname

But I do have another question… How can we automatically set the email address in the email field in AD?

Is this how we can write it?

set-aduser -emailaddress $firstname.$lastname@silvertech.com

Hmmm … let me answer with a question: Would it hurt you when you just tried it? :wink: Most of the time the things we figured out by ourselfs are much easier to remember than thing you just heard of or read about.

Regardless of that - before you ask someone else you should try to solve a problem by yourself. :wink:

Yes I tried that and I got an error, so that’s why I asked :slight_smile:

Errors are an important feature for scripters. They tell you what’s wrong - not just that something is wrong. And sometimes they even tell what to change. If you get some you should share them along with your question. Please format them as code as well.

Thanks in advance.

Hello Vsan,

I guess you have a erro with
+ CategoryInfo : ParserError: (:slight_smile: , ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingPropertyName
Because $firstname doesn’t have the $lastname property.

You should add “” at the start and end of this string.

set-aduser -emailaddress "$firstname.$lastname@silvertech.com"