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.)
Running on: Windows server 2019 datacenter. Powershell version: 5.1
CodePudding user response:
It's not an answer but the code looks ok to me and works as expected ...
$CSV = @'
"ISA","LOG","Consulting & Other","Vendor","Isalog","alsdkjfalsdjflasdkfjalsdkfjlaksdjflkasdfj"
"Bry","Link","Bry Link","Vendor","Bry","asdkfjalsdjflaksdjflasdkjflaksdfj"
"Michael","Had","Premier Service Of Western","Vendor","Michael","alsdkfjalskdjflaksdjflaksdfjalksdfj"
'@ |
ConvertFrom-Csv -Header 'FirstName', 'LastName', 'FirmName', 'Type', 'Username', 'Password'
foreach ($LINE in $CSV) {
$NewUser = "$($LINE.USERNAME)"
Write-Host "User = $NewUser"
}
And the output looks like this:
User = Isalog
User = Bry
User = Michael
Is it possible that your input file looks different than you showed here?
And BTW: You don't need the intermediate variables inside your loop. You can use them directly in your commands.