Answered by:
New-ADUser : The name provided is not a properly formed account name

Question
-
Hello Folks,
CV has 2 column: GivenName and Lastname.
I have recently put a script together and it is the first time is it giving me such headaches. I have tried to print out each element to confirm that I was able to get the info properly.
However, when I do run the New-User, I get an error :
New-ADUser : The name provided is not a properly formed account name
At line:1 char:I am not sure as of why I get that. Knowing when I print out $Name, it shows the proper format and everything is correct. Am I missing something ?
Function Find-CSVUsers($fileName){ [CmdletBinding()] $File = Get-ChildItem -Path $env:USERPROFILE -Name "$fileName.csv" -Recurse $File = $env:USERPROFILE + '\' + $File Import-CSV $File } Function Get-SamAccountName() { [CmdletBinding()] param ( [Parameter(Mandatory = $true, Position = 0)] [string]$firstName, [Parameter(Mandatory = $true, Position = 1)] [string]$lastname, [Parameter(Mandatory = $true, Position = 2)] [ValidateSet('CompanyA', 'CompanyB', 'CompanyC')] [string]$company ) switch ($company) { "CompanyA" { return ("$($firstName.Substring(0,1))$($lastname)").ToLower() } "CompanyB" { return ("$($firstName).$($lastname)").ToLower() } "CompanyC" { return ("$($firstName)-$($lastname.Substring(0, 1))").ToLower() } } } Function New-Users($list){ [CmdletBinding()] #Variable $domainInfo = Get-ADDomain $dn = $domainInfo.DNSRoot $net = $domainInfo.NetBIOSName $path = Read-Host "Which OU should we place created users?" $uPath = (Get-ADOrganizationalUnit -Filter "Name -like '*$path*'").DistinguishedName $Pass = Read-Host 'Enter generic password' $loginFormat = Read-Host 'Select the format a) jdoe b) john.doe c) john-d Enter your selection' $Password = (ConvertTo-SecureString -AsPlainText $Pass -Force) $TextInfo = (Get-Culture).TextInfo For($i = 0; $i -lt $list.count; $i++){ $Name = $TextInfo.ToTitleCase($list.GivenName[$i] + ' ' + $list.surname[$i]) $login = Get-SamAccountName -FirstName $list.GivenName[$i] -LastName $list.Surname[$i] -Company ('Company' + $loginFormat) New-ADUser -givenName $list.givenName[$i] -surname $list.surname[$i] -name $Name -displayName $Name ` -userPrincipalName ($login + '@' + $dn) -sAMAccount ($net + '\' + $login) ` -AccountPassword $Password -ChangePasswordAtLogon:$true -Enabled:$true -Path "$uPath" } } # MODULES # BEGIN $fileName = "users" $list = Find-CSVUsers($fileName) New-Users($list)
- Edited by Nevets24 Friday, December 20, 2019 3:52 PM
Friday, December 20, 2019 3:49 PM
Answers
-
The Name property must be 64 characters or less, and be unique in the parent OU or container.
The sAMAccountName must be 20 characters or less, and be unique in the domain. Also, the following characters are not allowed in the sAMAccountName:
" [ ] : ; | = + * ? < > / \ ,
Plus leading and trailing spaces are not allowed.
Your format for sAMAccountName looks wrong to me. The leading NetBIOSName\ should be removed.
Edit: And the parameter should be -SAMAccountName, not -SAMAccount. If you don't specify a sAMAccountName, I believe the default is to use the Name as the sAMAccountName, which is probably a problem.
Richard Mueller - MVP Enterprise Mobility (Identity and Access)
- Edited by Richard MuellerMVP Friday, December 20, 2019 4:09 PM
- Marked as answer by Nevets24 Friday, December 20, 2019 4:19 PM
Friday, December 20, 2019 4:03 PM
All replies
-
The Name property must be 64 characters or less, and be unique in the parent OU or container.
The sAMAccountName must be 20 characters or less, and be unique in the domain. Also, the following characters are not allowed in the sAMAccountName:
" [ ] : ; | = + * ? < > / \ ,
Plus leading and trailing spaces are not allowed.
Your format for sAMAccountName looks wrong to me. The leading NetBIOSName\ should be removed.
Edit: And the parameter should be -SAMAccountName, not -SAMAccount. If you don't specify a sAMAccountName, I believe the default is to use the Name as the sAMAccountName, which is probably a problem.
Richard Mueller - MVP Enterprise Mobility (Identity and Access)
- Edited by Richard MuellerMVP Friday, December 20, 2019 4:09 PM
- Marked as answer by Nevets24 Friday, December 20, 2019 4:19 PM
Friday, December 20, 2019 4:03 PM -
Oh my gosh... Thanks for pointing that out. I haven't seen the sAMAccount error I did. Surely it will not work hence why I received the error message !
Cheers,
Steven
Friday, December 20, 2019 4:12 PM -
I also missed it at first, but I'm glad the issue is resolved.
Richard Mueller - MVP Enterprise Mobility (Identity and Access)
Friday, December 20, 2019 4:33 PM