[Solved]Script fail on to long last name when importing users from csv file

Hi guys,
i have a very besic script for importing users from CSV file,

it’s working and doing the things that i need, but i have only one problem with the code,

when the user have abit long last name, i’m getting this error:

New-ADUser : The name provided is not a properly formed account name
At line:30 char:1
+ New-ADUser -Name "$firstname $lastname" -GivenName $firstname -Surname $lastname ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (CN=Svetlana Chi...C=Crow,DC=local:String) [New-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:1315,Microsoft.ActiveDirectory.Management.Commands.NewADUser

I’ve already shortened the name and it working correctly and creating the user, but i don’t want to short any one’s name

here is the code

cls
#get the csv file
$filepath = import-csv "C:\users.csv"

#set the variable for the uers
$newusers = $filepath

#set Passwords for new users 

$securepassword = ConvertTo-SecureString "blahblah" -AsPlainText -Force

#start the loop 

foreach ($user in $newusers) {


#get user information
$firstname = $user.'First Name'.Trim()
$lastname = $user.'Last Name'.Trim()
$loginname= $user.SamAccountName
$jobtitle = $user.'Job Title'
$Department= $user.Department
$Description = $user.Description
$OuPath= $user.Path
$LoginScript=$user.ScriptPath
$displayname= $user.DisplayName

#creat the users in active directory

New-ADUser -Name "$firstname $lastname" -GivenName $firstname -Surname $lastname -UserPrincipalName $loginname -Path $OuPath -ScriptPath $LoginScript  -AccountPassword $securepassword -ChangePasswordAtLogon $false  -Department $Department -DisplayName $displayname -Description $Description -Title $jobtitle  -Enabled $true 



Write-Host "`n"
Write-Host "The account for $firstname $lastname created in $OuPath successfully"

}

Thanks alot for your help

Just an idea - you’re creating the -Name property from the first name plus the last name. Does it really have to be that long? :wink:

#region Input

$filepath = 'C:\users.csv' 
$password = 'blahblah'

#endregion

# Import users information from CSV file
$newusers = Import-Csv $filepath

# start the loop
foreach ($user in $newusers) {

    # Splatt the user information
    $UserInfo = @{
#        Name                  = "$($user.'First Name'.Trim()) $($user.'Last Name'.Trim())" # This will break if longer than 20 characters
        Name                  = $(                                                         # This ensures Name is not too long
            $firstPartInUserName = $user.'First Name'.Trim()
            $lastPartInUserName  = $user.'Last Name'.Trim()
            If ($firstPartInUserName.Length + $lastPartInUserName.Length -gt 18) {
                If ($lastPartInUserName.Length -le 17) {
                    $firstPartInUserName = $firstPartInUserName.Substring(0,(18 - $lastPartInUserName.Length))
                } Else {
                    $firstPartInUserName = $firstPartInUserName.Substring(0,1)
                    $lastPartInUserName = $lastPartInUserName.Substring(0,17)
                }
            }
            "$firstPartInUserName $lastPartInUserName"
        )
        GivenName             = $user.'First Name'.Trim()
        Surname               = $user.'Last Name'.Trim()
        UserPrincipalName     = $user.SamAccountName 
        Path                  = $user.Path
        ScriptPath            = $user.ScriptPath
        AccountPassword       = ConvertTo-SecureString $password -AsPlainText -Force 
        ChangePasswordAtLogon = $false             # Consider setting this to $true per standard security practice 
        Department            = $user.Department
        DisplayName           = $user.DisplayName
        Description           = $user.Description
        Title                 = $user.'Job Title'  
        Enabled               = $true 
    }

    # Create the users in active directory
    New-ADUser @UserInfo
    Write-Host "`n The account for $firstname $lastname created in $OuPath successfully"

}

Thanks Sam,
this is a bit complex for me yet, but i do understand what you did,

thanks alot :slight_smile:

Update:

I’ve figured it out in a much easier way :slight_smile:

I didn’t knew that the Pre windows 2000 logon name must be less then 20 characters, so instead of doing what sam writes which is a bit to complex for me yet

I’ve added another header to the csv file with “UserPrincipalName”