J7Ts
J7Ts

Reputation: 110

Windows Powershell - trouble importing CSV and iterating

I'm new to Powershell (of course), and having troubles with a seemingly simple process. I have found a couple of examples that I think I am following, but they aren't working for me.

What I am trying to do: add a bunch of users to the local Windows OS, by reading from a CSV file (has names, usernames, passwords, etc).

My understanding is that the 'Import-CSV' cmdlet is supposed to return an object-like thing you can iterate over:

"The result of an Import-Csv command is a collection of strings that form a table-like custom object."

When I perform that step, saving it to a variable, it seems that there is only ever 1 row present. And if I don't provide the "-Header" parameter, I get errors about a 'member is already present'... even if I include the header in the CSV file (my original file did not include a header row in the CSV file.)

I have tried various methods trying to get a Count of the imported CSV results, just trying to see what the data is, but I'm not having any luck. (MS Docs say you can use the Count property.)

MS Docs (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-7.2) say this about "Import-CSV":

Outputs

Object

This cmdlet returns the objects described by the content in the CSV file.

...

Notes

Because the imported objects are CSV versions of the object type...

The result of an Import-Csv command is a collection of strings that form a table-like custom object. Each row is a separate string, so you can use the Count property of the object to count the table rows. The columns are the properties of the object and items in the rows are the property values.

An example of my input CSV file:

"ISA","LOG","Consulting & Other","Vendor","Isalog","alsdkjfalsdjflasdkfjalsdkfjlaksdjflkasdfj"
"Bry","Link","Bry Link","Vendor","Bry","asdkfjalsdjflaksdjflasdkjflaksdfj"
"Michael","Had","Premier Service Of Western","Vendor","Michael","alsdkfjalskdjflaksdjflaksdfjalksdfj"

Code of one example that I am testing:

param ($InputFile)
Write-Host "Provided input file: $InputFile"

$CSV = Import-CSV -Path $InputFile -Header 'FirstName', 'LastName', 'FirmName', 'Type', 'Username', 'Password'

foreach($LINE in $CSV)
{
    $NewUser="$($LINE.USERNAME)"
    $NewPass="$($LINE.PASSWORD)" 
    $SecurePass=ConvertTo-SecureString –AsPlainText -Force -String "$NewPass"
    Write-Host "User = $NewUser"

    #New-LocalUser -Name $NewUser -Password $SecurePass
} 

And a screenshot of my script plus the run results: enter image description here

Running on: Windows server 2019 datacenter. Powershell version: 5.1

Upvotes: 0

Views: 542

Answers (1)

J7Ts
J7Ts

Reputation: 110

The ultimate answer was that the character encoding for the CSV file I was using as input was causing problems for Powershell. Specifically, the line-ending encoding.

My original file was created on a Mac. The line-ending enconding was 'Macintosh (CR)'. The files that worked OK were created on this Windows machine, and used the line-ending encoding = "Windows (CR LF)".

Thanks to Olaf who got me thinking about this issue and made me investigate that area further.

Upvotes: 0

Related Questions