locked
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 
    
    foreach
      
    ($Userin$ADUsers)
    
    {
      
    #Read user data from each field in each row and assign the data to a variable as below
    
    $Username=$User.username
    $Password=$User.password
    $Firstname=$User.firstname
    $Lastname=$User.lastname
    $OU=$User.ou #This field refers to the OU the user account is to be created in
    $email      =$User.email
    $streetaddress=$User.streetaddress
    $city       =$User.city
    $zipcode    =$User.zipcode
    $state      =$User.state
    $country    =$User.country
    $telephone  =$User.telephone
    $jobtitle   =$User.jobtitle
    $company    =$User.company
    $department=$User.department
    $Password=$User.Password
    #Check to see if the user already exists in AD
    if(Get-ADUser-F{SamAccountName-eq$Username})
    {
    #If user does exist, give a warning
    Write-Warning"A user account with username $Usernamealready exist in Active Directory."
     }
    else
    {
    #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
    New-ADUser`
    -SamAccountName$Username`
    -UserPrincipalName$usernameanddomain
    -Name"$Firstname$Lastname"`
    -GivenName$Firstname`
    -Surname$Lastname`
    -Enabled$True`
    -DisplayName"$Lastname, $Firstname"`
    -Path$OU`
    -City$city`
    -Company$company`
    -State$state`
    -StreetAddress$streetaddress`
    -OfficePhone$telephone`
    -EmailAddress$email`
    -Title$jobtitle`
    -Department$department`
    -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

Answers

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

    Try the following: 

    $ADUsers=Import-csv -path pathtocsv -delimiter ","
    Regards,
    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,

    (79,108,97,102|%{[char]$_})-join''

    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"
    regards,

    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 ","
    Regards,
    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. ;-)

    https://social.technet.microsoft.com/Forums/de-de/home?forum=powershell_de


    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Thursday, August 16, 2018 12:17 PM