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 Sam,
this is a bit complex for me yet, but i do understand what you did,
thanks alot
Update:
I’ve figured it out in a much easier way
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”