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://docs.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:
Running on: Windows server 2019 datacenter. Powershell version: 5.1
Answer
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.
Related Questions
- → Azure function doesnt see JSON body passed from Shopify properly
- → How to check git if folder has been changed
- → Using git with ssh-agent on Windows
- → How to right align something in Powershell prompt?
- → What are your favorite Powershell Cmdlets?
- → git fetch tags on Visual studio online build agent
- → Powershell process, set location
- → Unable to update Dart SDK. Retrying
- → flutter doctor doesn't work on neither Command Prompt or PowerShell window?
- → Powershell long string with "`n" appears fine in console but get .count = 1
- → How do I shorten the name of a program for powershell
- → Get "ParserError: (:) [], ParentContainsErrorRecordException" when executing python script in Powershell
- → Wifi WPS client start in Windows 10 in script or code