Unable to Import-CSV to AD with New-ADuser

I am having a problem in getting my Import-CSV to work.

Here's my csv data:
name,path,accountexpires,cn,codePage,countryCode,distinguishedName,GivenName,sn,displayName,sAMAccountName,sAMAccountType,userPassword,description,memberOf,homeDirectory,homeDrive,instanceType,objectCategory,objectClass,primaryGroupID,userAccountControl,userPrincipalName
"Alex Lupe ","OU=700,OU=employees,DC=west,DC=local","(never)","70007335","0","0","CN=70007335,OU=700,OU=employees,DC=west,DC=local","Alex","Lupe","70007335","70007335","805306368","6/22/2004","144","west.local/Users,"OU=700,OU=employees,DC=west,DC=local","H:","4","CN=Person,CN=Schema,CN=Configuration,DC=west,DC=local","top;person;organizationalPerson;user","513","66048","70007335"


My script is pretty straight forward I thought:

Import-Csv C:\AD_Import\ActiveEmployees.csv | ForEach-Object {
    New-ADUser
        -Name $_.name
        -GivenName $_.givenName
        -Surname $_.sn
        -accountpassword (ConvertTo-SecureString -AsPlainText $_.userPassword -force)
        -Path $_.path
        -AccountExpires  $_.accountexpires
        -CN  $_.cn
        -codePage $_.codePage
        -countryCode $_.countryCode
        -description $_.description
        -distinguishedName $_.distinguishedName
        -displayName $_.displayName
        -sAMAccountName $_.sAMAccountName
        -sAMAccountType $_.sAMAccountType
        -HomeDirectory $_.homedirectory
        -HomeDrive $_.homeDrive
        -instanceType $_.instanceType
        -objectCategory $_.objectCategory
        -objectClass $_.objectClass
        -primaryGroupID $_.primaryGroupID
        -userAccountControl $_.userAccountControl
        -userPrincipalName $_.userPrincipalName
        -Enabled $True
        -ChangePasswordAtLogon $False
}


But when I run it, I keep getting:

cmdlet New-ADUser at command pipeline position 1
Supply values for the following parameters:
Name:

Been working on this for hours and can not seem to find what is wrong.  Any ideas??
starr8303Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Matthew BorrussoCommented:
While I may not be able to offer specific insight into what is exactly wrong with your particular script, I am attaching a link to a technet blog. This blog has an example of the tools in use with formatting examples of the supporting files. It may not be solution, but it may provide some direction as to how to make it work.
http://blogs.technet.com/b/samdrey/archive/2011/09/26/bulk-populate-an-ad-using-a-csv-file-and-new-aduser-including-passwords.aspx

The only real difference is the blog does not use the foreach-object call. Unless there is a particular reason for doing it exactly this way, give the blog method a go.

Good Luck with it,
Matt
0
yo_beeDirector of Information TechnologyCommented:
Are you running this from the Active Directory powershell command shell or start Powershell?

 If you are running this from the standard Powershell did you import the active directory module?

Import-module Activedirectory.
0
oBdACommented:
You can't just add line breaks at will. If you want to format a single command over several lines, you need to tell PS that the line continues by adding a backtick ` at the very end of the line.
Import-Csv C:\AD_Import\ActiveEmployees.csv | ForEach-Object {
	New-ADUser `
		-Name $_.name `
		-GivenName $_.givenName `
		-Surname $_.sn `
		-accountpassword (ConvertTo-SecureString -AsPlainText $_.userPassword -force) `
		-Path $_.path `
		-AccountExpires  $_.accountexpires `
		-CN  $_.cn `
		-codePage $_.codePage `
		-countryCode $_.countryCode `
		-description $_.description `
		-distinguishedName $_.distinguishedName `
		-displayName $_.displayName `
		-sAMAccountName $_.sAMAccountName `
		-sAMAccountType $_.sAMAccountType `
		-HomeDirectory $_.homedirectory `
		-HomeDrive $_.homeDrive `
		-instanceType $_.instanceType `
		-objectCategory $_.objectCategory `
		-objectClass $_.objectClass `
		-primaryGroupID $_.primaryGroupID `
		-userAccountControl $_.userAccountControl `
		-userPrincipalName $_.userPrincipalName `
		-Enabled $True `
		-ChangePasswordAtLogon $False
}

Open in new window

0
starr8303Author Commented:
I was able to resolve the error and also found that I was trying to load some predetermined values into AD.  Here is what my final script looked like:
Import-Csv C:\AD_Import\ActiveEmployees.csv | ForEach-Object {
    $newemployee=@{
        Name=$_.'name'
        GivenName=$_.'givenName'
        Surname=$_.'sn'
        accountpassword=(ConvertTo-SecureString -AsPlainText $_.'userPassword' -force) 
        Path=$_.'path'
        description=$_.'description'
        displayName=$_.'displayName'
        sAMAccountName=$_.'sAMAccountName'
        HomeDirectory=$_.'homedirectory' 
        HomeDrive=$_.'homeDrive'
        userPrincipalName=$_.'userPrincipalName'

        }

        new-aduser @newemployee
         }

Open in new window



Thank you all for your thoughts!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
starr8303Author Commented:
Was a combination of reasons that this code was failing.  The other answers focused on syntax, but the answer also involved invalid items trying to import into AD.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.