Powershell – Create new user accounts from CSV & simultaneously populate these new accounts with template account info (Address & group membership)

I can create user accounts with a CSV ok in Powershell.

I can also create an account from a template account in Powershell, and the info like address & group memberships comes across ok.

However, when trying to combine both operations inside of a foreach loop, the additional info from the template account will not populate into the new accounts created from the CSV.

I tried using variables to capture the Template User properties, to include using a variable to capture only the group info from the template account.

I also tried using specific names for the accounts instead of variables. However, the data from the template account does not go over to the accounts created from CSV in the foreach loop.

You can see in the below images, that the data never populates in the newly created users. (I don’t know why the images aren’t showing, it showed me a thumbnail & gave me option to “Add Image”)

You can see in the following code the things that I have tried:

#------------------------- Create users with CSV & Template -------------------------------
# Goal = to get all settings from template account  to include city, address, & groups 
#   into each account that is being created from the CSV.
#      The accounts create, but the other data (address & groups) do not populate.

# Import the CSV file
$users = Import-Csv -Path "C:UsersAdministratorDesktopPosh_Server2022ADUsers.csv"

   # Get the template user 
$templateUser = Get-ADUser -Identity "Template User"

# Loop through each user in the CSV file
foreach ($user_ in $users) {
    # Create the new user based on the template
     New-ADUser  -Name $user_.Name `
    -GivenName $user_.GivenName `
    -Surname $user_.Surname `
    -SamAccountName $user_.UserName `
    -AccountPassword $user_.Password `
    -Instance $templateUser

# Get the groups that the template user is a member of
#$groups = Get-ADPrincipalGroupMembership -Identity $templateUser

############## THIS IS WHERE PROBLEMS START ##############
    #$tmpltUsr = Get-ADUser 'Template User' -Properties MemberOf
       #$tmpltUsr = Get-ADUser 'Template User' # THIS GIVES NO ERRORS BUT DOESN'T WORK
     #$tmpltUsr = Get-ADPrincipalGroupMembership -Identity "Template User"
            #ForEach($group in 'Template User'.MemberOf){
               # ForEach($group in $groups){
               ForEach($group in $templateUser.MemberOf){ # THIS GIVES NO ERRORS BUT DOESN'T WORK
                   # ForEach($group in $templateUser){
              Add-ADGroupMember $group -Members $user_ #THIS GAVE NO ERRORS
            #Add-ADGroupMember -Identity $group.Name -Members $user_
    }
}

###################################################

CSV:

Name,GivenName,Surname,UserName,AccountPassword

Test User1,Test,User1,tuser1,Password

Test User2,Test,User2,tuser2,Password

7

As iRon commented, you do not capture the object created by New-ADUser and your code now uses a PsCustomObject read from the CSV as value for parameter -Members, but that will not work.
Acceptable values for that can be found here

Also, I recommend not using those horrible backticks, but instead use Splatting on cmdlets that can take a lot of parameters.

Try

#------------------------- Create users with CSV & Template -------------------------------

# Import the CSV file
$csvUsers = Import-Csv -Path "C:UsersAdministratorDesktopPosh_Server2022ADUsers.csv"

# Get the template user (your code shows the Name only, not the SamAccountName which would be a lot easier)

# Get-ADUser by default returns these properties:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# If you need extra user properties, you must provide them in parameter `Properties

$propsToCopy  = 'StreetAddress', 'City', 'State', 'PostalCode', 'POBox'
$templateUser = Get-ADUser -Filter "Name -eq 'Template User' -or DisplayName -eq 'Template User'" -Properties MemberOf, $propsToCopy
if (!$templateUser) {
    throw "Cannot find the template user.."
}

# Loop through each user in the CSV file and collect the user objects in $newUsers
$newUsers = foreach ($user in $csvUsers) {
    # Create the new user based on the template
    $userParams = @{
        Name            = $user.Name
        GivenName       = $user.GivenName
        Surname         = $user.Surname
        SamAccountName  = $user.UserName
        AccountPassword = $user.Password
        Instance        = $templateUser
        Enabled         = $true
        PassThru        = $true  # this makes the New-ADUser cmdlet output the ADPrincipal object it created
    }

    New-ADUser @userParams
}

# add the new users to the groups the Template User is a member of
foreach ($groupDN in $templateUser.MemberOf) {
    Add-ADGroupMember -Identity $groupDN -Members $newUsers
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật