Foreach Loop only running last object

CmoneyG 21 Reputation points
2021-01-10T21:16:18.88+00:00

Hi! I am trying to work on an assignment for school creating new-adusers from a csv. but when i run my foreach loop it only runs the last object. Any help would be appreciated.

PS C:\windows\system32> ForEach ($ADUser in $NewUsers)
{

$First = $ADUsers.First_Name
$Last = $ADUsers.Last_Name
$Name = $First + " " + $Last
$post = $ADUser.PostalCode
$ophone = $ADUser.OfficePhone
$mphone = $ADUser.MobilePhone

New-ADUser -GivenName $Frist -Surname $Last -Name $Name -DisplayName $Name -OfficePhone $ophone -MobilePhone $mphone -PostalCode $post -Path $OU -Verbose
}
VERBOSE: Performing the operation "New" on target "CN=Kimbery Madarang,OU=Finance,DC=ucertify,DC=com".
VERBOSE: Performing the operation "New" on target "CN=Kimbery Madarang,OU=Finance,DC=ucertify,DC=com".
New-ADUser : The specified account already exists
At line:11 char:1

  • New-ADUser -GivenName $Frist -Surname $Last -Name $Name -DisplayName ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : ResourceExists: (CN=Kimbery Mada...ucertify,DC=com:String) [New-ADUser], ADIdentityAlreadyExistsException
  • FullyQualifiedErrorId : ActiveDirectoryServer:1316,Microsoft.ActiveDirectory.Management.Commands.NewADUser

VERBOSE: Performing the operation "New" on target "CN=Kimbery Madarang,OU=Finance,DC=ucertify,DC=com".
New-ADUser : The specified account already exists
At line:11 char:1

  • New-ADUser -GivenName $Frist -Surname $Last -Name $Name -DisplayName ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : ResourceExists: (CN=Kimbery Mada...ucertify,DC=com:String) [New-ADUser], ADIdentityAlreadyExistsException
  • FullyQualifiedErrorId : ActiveDirectoryServer:1316,Microsoft.ActiveDirectory.Management.Commands.NewADUser
Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2021-01-10T22:16:03.153+00:00

    The Foreach is working the way it should. Your problem is that you've used $ADSUsers (which is, presumably, an array) inside the code block instead of $ADUser (which is one item of the array). You've also misspelled "fRist" in the cmdlet's -GivenName parameter.

    Here's a clearer way to accomplish this:

    $NewUsers |
        ForEach-Object{
            $Params = @{
                GivenName =     $_.First_Name
                SurName =       $_.Last_Name
                Name =               "{0} {1}" -f $_.First_Name, $_.Last_Name
                DisplayName =   "{0} {1}" -f $_.First_Name, $_.Last_Name
                PostalCode =    $_.PostalCode
                OfficePhone =   $_.OfficePhone
                MobilePhone =   $_.MobilePhone
                Path        =   $OU
                Verbose =       $True
            }
            New-ADUser @Params
        }
    
    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. CmoneyG 21 Reputation points
    2021-01-10T22:04:58.077+00:00

    It was a really simple answer! Reddit ended up coming through for me.

    First and last are calling the wrong variable it looks like for a start should be $aduser.... not $adusers

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.