2

Code:

$Computers = Import-Csv -Path C:\temp\Computers.csv
write-host $Computers

CSV file (where US-1 is A1, US-2 is A2, and so on)

US-1
US-2
US-3
US-4

I believe Import-Csv is importing incorrectly because Write-Host $Computers gives me this:

@{US-1=US-2} @{US-1=US-3} @{US-1=US-4}

But if $Computers is assigned this way:

$Computers = "US-1", "US-2", "US-3", "US-4"

The output is correct:

US-1 US-2 US-3 US-4

So, I would need to import from a CSV for convenience but having each computer name saved the right way, without brackets or symbols. This makes using the computers names in the rest of my program very difficult.

EDIT: as discussed, I have correctly formatted the csv, which is now this:

Computers,
US-1
US-2
US-3
US-4

Now getting output:

@{Computers=US-1} @{Computers=US-2} @{Computers=US-3} @{Computers=US-4}

5
  • Strange output. What datatype is $Computers? Commented Oct 13, 2017 at 11:46
  • Default data type, most likely string, not sure. what you see if the entire test program I created for stack overflow Commented Oct 13, 2017 at 11:48
  • Post a sample of your CSV file Commented Oct 13, 2017 at 11:49
  • What is the output of $Computers.gettype()? Commented Oct 13, 2017 at 11:57
  • Import-csv interpreted the first record as a header, while you intended it to be data. Therefore, it decoded the csv data as one field, named US-1. See Jeff Zeitlin's answer below. Commented Oct 13, 2017 at 12:29

3 Answers 3

4

Simple CSV

Create your CSV (computers.csv):

Computer
US-1
US-2
US-3

Import your CSV:

$computers = Import-Csv Computers.csv

Access your values:

Write-Host $computers # This results to something like this: @{Computers=US-1} @{Computers=US-2} @{Computers=US-3} @{Computers=US-4}
# This is perfectly fine. It's all of your computer-objects

Access each value:

write-host $computers[n] # replace n with your index, starting at 0 to n-1
write-host $computers[1] # results in US-2

If only the computers are of interest you could do something like this:

write-host $computers.Computer # restults in US-1 US-2 US-3 US-4

Likewise for greater CSVs:

Computers, OperatingSystem
US-1, Windows
US-2, Linux
US-3, SomeOtherUnix
US-4, MacOS

Import your CSV:

$computers = Import-Csv Computers.csv

Access your values:

Write-Host $computers # This results to something like this: 
# @{Computers=US-1; OperatingSystem=Windows} @{Computers=US-2; OperatingSystem=Linux} @{Computers=US-3; OperatingSystem=SomeOtherUnix} @{Computers=US-4; OperatingSystem=MacOS}
# This is perfectly fine. It's all of your computer-objects

Access each value:

write-host $computers[n] # replace n with your index, starting at 0 to n-1
write-host $computers[1] # results in 

Computers OperatingSystem
--------- ---------------
US-1      Windows

If only the computers/OperatingSystems are of interest you could do something like this:

write-host $computers.Computers # restults in US-1 US-2 US-3 US-4
write-host $computers.OperatingSystem # restults in US-1 US-2 US-3 US-4

And so on... :)

Sign up to request clarification or add additional context in comments.

3 Comments

what does comp mean? also i am using a for each loop later on so I can't refer to individual computers
nvm, figured it out, my script works well now thanks!
Ah my bad! I had $comp as my script variable insted of $computers. I'll edit my post for the sake of readability.
2

Your CSV file is malformed; it has no header line. Either regenerate the file so that it has a header line, or use the -Header parameter to Import-CSV.

Once you have a correctly-formatted CSV (or have imported with a manually-supplied header), you will be able to reference $computers as an array of PSObjects, where each PSObject contains a member with the supplied name - for example, if you used

$Computers = Import-CSV -Header "RegionName" -Path C:\TEMP\Computers.CSV

then you could refer to the individual records as $Computers[$n].RegionName.

Following the edit to the original question, the correct way to access the individual items in the array would be $Computers[$n].Computers. To retrieve the computers from a text file as originally described, without needing to use field names, use Get-Content instead of Import-CSV.

5 Comments

Hi, I have looked up a guide on creating csv headers and now my csv file looks like this:
If you have changed your CSV, and are still having problems, update your question to reflect that - or ask a new question.
yes sorry i hit enter for new line on the comment and it posted it, I edited my original post
Is the header "computer" singular or plural?
@WalterMitty - your choice, but you need to reference it using whichever you use in the header. Your example made the header "computers", so that's how you'd have to reference the field in the resulting object.
0

Summery of what Akaino answered in short form!

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.