New-AdUser : The name provided is not a properly formed account name

I’m trying to create users in AD via PowerShell. The problem is that my CSV file has the GivenName and the LastName columns and it does not have the Name column/header. It looks like this:

 GivenName,LastName,Initials,Phone,OU,Location
 Alex,Smith,AW,(902)123-1161,"OU=Users,OU=AlexFirstLocation,DC=AlexTest,DC=Local",Halifax
 Peter,King,PJ,(902)123-1251,"OU=Users,OU=AlexFirstLocation,DC=AlexTest,DC=Local",Halifax
 John,Brown,JS,(902)123-1341,"OU=Users,OU=AlexFirstLocation,DC=AlexTest,DC=Local",Halifax
 Steve,Plain,ST,(902)123-1431,"OU=Users,OU=AlexFirstLocation,DC=AlexTest,DC=Local",Halifax
 Chris,Blue,CS,(902)123-1521,"OU=Users,OU=AlexFirstLocation,DC=AlexTest,DC=Local",Halifax
 Smile,Workbench,SJ,(902)123-1611,"OU=Users, OU=AlexFirstLocation,DC=AlexTest,DC=Local",Halifax

So I just combined GivenName and LastName columns, and since the requirement was to have a comma-separated username, I thought that this script will solve my problem:

Import-Csv -Path C:\TestsAndLabs\TestAndLabPsScripts\Users.csv |
foreach {
            New-AdUser  -SamAccountName "($_.LastName).$($_.Initials)" `
                        -Name "$($_.GivenName), $($_.LastName)" `
                        -GivenName $_.FirstName `
                        -SurName $_.LastName `
                        -Initials $_.Initials `
                        -AccountPassword $null `
                        -CannotChangePassword $false `
                        -ChangePasswordAtLogon $true `
                        -DisplayName "$($_.LastName), $($_.FirstName)" `
                        -UserPrincipalName "$($_.LastName).$($_.Initials)@AlexTest.Local" `
                        -Path $_.OU `
                        -City $_.Location `
                        -HomePhone $_.Phone
        }

Wrong!!! This scrip throws an error:
New-ADUser : The name provided is not a properly formed account name.

CreateUser_ImportCSVfile_Error01

The books I use as my reference and the Internet sources (a very good link: http://social.technet.microsoft.com/wiki/contents/articles/5312.active-directory-characters-to-escape.aspx ) keep telling me that the New-ADUser cmdlet does not require that commas be escaped in the value passed to the -Name parameter. This was true for me for as long as I did not use Import-CSV cmdlet. But I had to use CSV file and I ended up changing the code in the script as follows:

The first version:

$ADUsers = Import-CSV C:\TestsAndLabs\TestAndLabPsScripts\Users.csv
foreach($ADUser in $ADUsers)
{
	$Params = @{
		SamAccountName = "$($ADUser.LastName).$($ADUser.Initials)"
		Name = "$($ADUser.GivenName), $($ADUser.LastName)"
		GivenName = $ADUser.GivenName
		Surname = $ADUser.LastName
		Initials = $ADUser.Initials
		AccountPassword = $null
		CannotChangePassword = $false
		ChangePasswordAtLogon = $true
		DisplayName = "$($ADUser.LastName), $($ADUser.GivenName)"
		UserPrincipalName = "$($ADUser.LastName).$($ADUser.Initials)@AlexTest.Local"
		Path = $ADUser.OU
		City = $ADUser.Location
		HomePhone = $ADUser.Phone
	}
	New-ADUser @Params
}

The second version (with PowerShell ver 2.0 PsObject and Add-Member cmdlet):

$obj = New-Object -TypeName PSObject
Import-Csv -Path C:\TestsAndLabs\TestAndLabPsScripts\Users.csv |
ForEach-Object {
                $obj | Add-Member -Force -MemberType Noteproperty -Name 'Name' -Value "$($_.GivenName), $($_.LastName)"
                $obj | Add-Member -Force -MemberType Noteproperty -Name 'GivenName' -Value "$($_.GivenName)"
                $obj | Add-Member -Force -MemberType Noteproperty -Name 'SurName' -Value "$($_.LastName)"
                $obj | Add-Member -Force -MemberType Noteproperty -Name 'Initials' -Value "$($_.Initials)"
                $obj | Add-Member -Force -MemberType Noteproperty -Name 'SamAccountName' -Value "$($_.LastName).$($_.Initials)"
                $obj | Add-Member -Force -MemberType Noteproperty -Name 'DisplayName' -Value "$($_.LastName), $($_.GivenName)"
                $obj | Add-Member -Force -MemberType Noteproperty -Name 'UserPrincipalName' -Value "$($_.LastName).$($_.Initials)@AlexTest.Local"
                $obj | Add-Member -Force -MemberType Noteproperty -Name 'PathToOU' -Value "$($_.OU)"
                $obj | Add-Member -Force -MemberType Noteproperty -Name 'City' -Value "$($_.Location)"
                $obj | Add-Member -Force -MemberType Noteproperty -Name 'HomePhone' -Value "$($_.Phone)"

New-AdUser -Name $obj.Name -GivenName $obj.GivenName -SurName $obj.SurName -Initials $obj.Initials `
           -SamAccountName $obj.SamAccountName -AccountPassword $null -CannotChangePassword $false -ChangePasswordAtLogon $true  `
           -DisplayName $obj.DisplayName -UserPrincipalName $obj.UserPrincipalName`
           -Path $obj.PathToOU -City $obj.City -HomePhone $obj.HomePhone
              }

The third version (PowerShell ver 3.0 with type adapter [pscustomobject]):

Import-Csv -Path C:\TestsAndLabs\TestAndLabPsScripts\Users.csv |
foreach {
        $Parameters = [pscustomobject]@{SamAccountName = "$($_.GivenName).$($_.Initials)"
                                      	Name = "$($_.GivenName), $($_.LastName)"
                                      	GivenName = $_.GivenName
                                      	SurName = $_.LastName
                                      	Initials = $_.Initials
                                        AccountPassword = $null
                                        CannotChangePassword = $false
                                        ChangePasswordAtLogon = $true
                                      	DisplayName = "$($_.LastName), $($_.GivenName)"
                                      	UserPrincipalName = "$($_.LastName).$($_.Initials)@AlexTest.Local"
                                      	Path = $_.OU
                                      	City = $_.Location
                                      	HomePhone = $_.Phone
                                      }
        $Parameters | New-ADUser
        }

All (three listed versions) scripts worked just fine and created user object in AD by importing data from CSV file with combined GivenName and LastName columns. And the comma was handled properly by the -Name parameter, without escaping. I wonder if there is something wrong with New-AdUser cmdlet, I think it a caught a bug… What is your experience?





Please note: Although the author has made every reasonable attempt to achieve complete accuracy of the content, he assumes no responsibility for errors or omissions. Also, you should use this information as you see fit, and at your own risk.

One thought on “New-AdUser : The name provided is not a properly formed account name

Leave a Reply

Your email address will not be published. Required fields are marked *