PowerShell doesnt load values from .csv File RRS feed

  • Question

  • Hi,

    I want to automatically create Users in Active Directory using a .csv File and Powershell.My .csv GFile Looks like this:

    firstname middleInitial lastname username email   streetaddress city zipcode state country department password telephone jobtitle company ou

    Test             Test                                 Test           testmailadress

    and so on, so on....I filled it up with the values I Need.Now i want to load the values into a Array using Powershell:

    $ADUsers=Import-csv  pathtocsv
     #Loop through each row containing user details in the CSV file 
    #Read user data from each field in each row and assign the data to a variable as below
    $OU=$User.ou #This field refers to the OU the user account is to be created in
    $email      =$User.email
    $city       =$User.city
    $zipcode    =$User.zipcode
    $state      =$User.state
    $country    =$User.country
    $telephone  =$User.telephone
    $jobtitle   =$User.jobtitle
    $company    =$User.company
    #Check to see if the user already exists in AD
    #If user does exist, give a warning
    Write-Warning"A user account with username $Usernamealready exist in Active Directory."
    #User does not exist then proceed to create the new user account
    #Account will be created in the OU provided by the $OU variable read from the CSV file
    -DisplayName"$Lastname, $Firstname"`
    -AccountPassword(convertto-securestring$Password-AsPlainText-Force) -ChangePasswordAtLogon$False

    as far as i know thats the way to go right?

    But as soon as i start the script it tells me that the varibale $Username is not definded.

    How come this error happens? the path to the .csv File works properly.

    Sorry for bad english I'm not a native Speaker...

    Thursday, August 16, 2018 7:24 AM


  • Machines are so dumb, you need to specify the delimiter you are using.

    Try the following: 

    $ADUsers=Import-csv -path pathtocsv -delimiter ","
    Thursday, August 16, 2018 9:15 AM

All replies

  • Could you please edit your post and format the code as code using the code formatting tool provided in the bar right on top of the post editor? This way it is almost unreadable. And please try to avoid posting a lot of white space.

    Thanks in advance.

    Best regards,


    Thursday, August 16, 2018 7:59 AM
  • Which delimiter have in your csv file?

    You could try to specify it:

    $ADUsers=Import-csv  -path pathtocsv -delimiter "<delimiter_used>"

    If your delimiter is a tab, you can specify with:

    $ADUsers=Import-csv -path pathtocsv -delimiter "`t"

    Thursday, August 16, 2018 8:17 AM
  • Hi,

    sorry, I am new to the Forum as well as Powershell.

    I re formatted the code it should be easier to read now!

    Thursday, August 16, 2018 8:45 AM
  • Hi,

    thanks for your quick answer!

    I'll have you know that i am a rookie in using PowerShell and still in trining... so please by kind if i ask kind of dumb questions.

    I followed some kind of tutorial creating this script and the .csv

    A Delimiter wasn't mentioned at any Point. My .csv file is - when i open it with Microsoft Excel - sorted by the columns (A,B,C...)

    When i open it with the Text Editor it is sorted by ",".

    Do I have to specify a delimiter for it to work? Or is the script somehow able to get the values sorted by the columns?

    Thursday, August 16, 2018 9:03 AM
  • Machines are so dumb, you need to specify the delimiter you are using.

    Try the following: 

    $ADUsers=Import-csv -path pathtocsv -delimiter ","
    Thursday, August 16, 2018 9:15 AM
  • Thank you very much!

    With the delimiter specified it works as it is supposed to be!

    You brightened up a rookies day and Motivation with your quick Responses!

    Thursday, August 16, 2018 9:33 AM
  • Just for your information: There is a German Powershell Forum as well. Sometimes it's easier to ask a question in your native language. ;-)


    Best regards,


    Thursday, August 16, 2018 12:17 PM