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

**URL:** <https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782>\
**Category:** PowerShell Help\
**Created:** [October 19, 2021, 2:09pm UTC](https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782 "2021-10-19T14:09:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsan91](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vsan91/32/1763_2.png) [@vsan91](https://forums.powershell.org/u/vsan91)\
**Post date:** [October 19, 2021, 2:09pm UTC](https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782/1 "2021-10-19T14:09:18Z")

</div>

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

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 19, 2021, 2:56pm UTC](https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782/2 "2021-10-19T14:56:45Z")

</div>

Without digging too deep in your convoluted code … 😉

`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.

---

<div class="post-metadata">

**Author:** ![vsan91](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vsan91/32/1763_2.png) [@vsan91](https://forums.powershell.org/u/vsan91)\
**Post date:** [October 19, 2021, 3:13pm UTC](https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782/3 "2021-10-19T15:13:58Z")

</div>

I actually figured it out… thank you…

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

---

<div class="post-metadata">

**Author:** ![vsan91](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vsan91/32/1763_2.png) [@vsan91](https://forums.powershell.org/u/vsan91)\
**Post date:** [October 19, 2021, 3:15pm UTC](https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782/4 "2021-10-19T15:15:08Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 19, 2021, 3:20pm UTC](https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782/5 "2021-10-19T15:20:12Z")

</div>

> [@vsan91](#):
>
> Is this how we can write it?

Hmmm … let me answer with a question: Would it hurt you when you just tried it? 😉 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. 😉

---

<div class="post-metadata">

**Author:** ![vsan91](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vsan91/32/1763_2.png) [@vsan91](https://forums.powershell.org/u/vsan91)\
**Post date:** [October 19, 2021, 3:21pm UTC](https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782/6 "2021-10-19T15:21:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 19, 2021, 3:25pm UTC](https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782/7 "2021-10-19T15:25:15Z")

</div>

> [@vsan91](#):
>
> I got an error

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.

---

<div class="post-metadata">

**Author:** ![Chen.Chen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chen.chen/32/1689_2.png) [@Chen.Chen](https://forums.powershell.org/u/Chen.Chen)\
**Post date:** [October 20, 2021, 2:38am UTC](https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782/8 "2021-10-20T02:38:22Z")

</div>

Hello Vsan,

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

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

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

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:20pm UTC](https://forums.powershell.org/t/how-to-add-group-to-ad-user-while-creating-a-user-in-powershell/17782/9 "2024-05-16T20:20:37Z")

</div>


