Home > Software engineering >  Trying to create multiple AD users at once using CSV and Powershell. Splatting appears to be causing
Trying to create multiple AD users at once using CSV and Powershell. Splatting appears to be causing

Time:06-10

As the title says, I'm trying to use powershell in combination with a CSV file to create multiple users at once but keep encountering an error. I have included the error and my code below. Any help in fixing this is much appreciated!

Powershell Error:

New-ADUser : The object name has bad syntax
At line:32 char:17
              } } New-ADUser @hash
                  ~~~~~~~~~~~~~~~~
      CategoryInfo          : NotSpecified: (CN=Test User...REFORM,DC=local:String) [New-ADUser], ADException
      FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.NewADUser

Powershell Code:

$ADUsers = Import-csv EnterFilePathHere

foreach ($User in $ADUsers)
{

       $Firstname   = $User.firstname
       $Surname    = $User.surname
       $Password    = $User.password
       $OU          = $User.ou
       $Description = $User.description
       $Email       = $User.email
       $Username    = -join("$Firstname", "_", "$Surname") 

         
            $hash = @{
                SamAccountName = $Username
                UserPrincipalName = "$Username@EnterDomainHere"
                Name = "$($User.firstName) $($User.surName)"
                givenName = $FirstName
                surName = $Surname
                Enabled = $true
                ChangePasswordAtLogon = $false
                DisplayName = "$FirstName $Surname"
                Path = $OU
                Description = $Description
                EmailAddress = $Email
                AccountPassword = (ConvertTo-SecureString "$Password" -AsPlainText -Force)
              
            } New-ADUser @hash      
} 
        

As mentioned by theo, New-ADUser @hash is currently outside of the loop. Frustratingly, when I move it up a line (as I now have in the code displayed above) I am faced with another error:

            
At line:32 char:15
              } New-AdUser @hash }
                ~~~~~~~~~~
Unexpected token 'New-AdUser' in expression or statement.
      CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      FullyQualifiedErrorId : UnexpectedToken

Here's an example of the CSV file as displayed in Excel. The file is saved as CSV UTF-8 (Comma delimited

CodePudding user response:

You should try to indent better, then you will spot the error soon enough. The New-ADUser cmdlet should be on its own separate line right below the closing bracket of the Hashtable.

Also, you don't need all those variables, just a few to make things easier

$ADUsers = Import-Csv -Path '<EnterFilePathHere>'

foreach ($User in $ADUsers) {
    # for convenience create some variables that will be used more often
    $Firstname = $User.firstname
    $Surname   = $User.surname
    $Username  = '{0}_{1}' -f $Firstname, $Surname
    # create the splatting Hashtable
    $hash = @{
        SamAccountName        = $Username
        UserPrincipalName     = "$Username@EnterDomainHere"
        Name                  = "$Firstname $Surname"
        GivenName             = $FirstName
        Surname               = $Surname
        DisplayName           = "$FirstName $Surname"
        Enabled               = $true
        ChangePasswordAtLogon = $false
        Path                  = $User.ou
        Description           = $User.description
        EmailAddress          = $User.email
        AccountPassword       = (ConvertTo-SecureString $User.password -AsPlainText -Force)
    } 
    New-ADUser @hash      
} 
  • Related