0

I'm trying to read a csv file and store its data into variables that I will use for Powershell scripts. I managed to read the file by doing the following command:

$csv = Import-Csv C:\Users\[...]\Documents\ipaddresses.csv -delimiter "|" 
$csv

The output looks like this:

IP          Name
--          ----
165.XX.XX.1 n1
165.XX.XX.2 n2
165.XX.XX.3 n3
165.XX.XX.4 n4
165.XX.XX.5 n5
165.XX.XX.6 n6

The idea is that I could create 6 variables each like:

$Name = $IP

However, when I try to loop the objects of the $csv variable and store them into arrays like this:

foreach($item in $csv){
    IP = $($item.IP) 
    Name = $($item.Name)
}

It returns me empty arrays. How do I fix this?

5
  • 1
    Your syntax for variable assignment is incorrect in this example. Variables are referenced by $ preceding their names. So at a minimum, you must have $IP = $line.IP and $Name = $line.Name. Even with correct syntax, your variables in this example would get overwritten during each iteration of your loop. That would mean that any operation that depends on them needs to happen within that loop. I would not use $item as a variable name as I am not sure if that could cause a conflict in certain cases with reserved/automatic variables. Commented Apr 24, 2019 at 13:16
  • 1
    $csv does already contain all the information you could want. If you need to maintain access to data throughout your code, specifically outside of your loops, you can reference specific lines with indexes ($csv[3] or $csv[2].IP). You can also access properties from the whole array ($csv.Name or $csv.IP). Also, variable assignment typically suppresses success stream output. So you would need to explicitly ask for the assigned value by typing the variable name $IP or $Name without assignment. Commented Apr 24, 2019 at 13:21
  • @AdminOfThings when I type $csv[2] I do get the line of the index 2 returned. However, when I type in $csv[2].IP or even just $csv.IP, nothing is returned. I believe this is the root of the problem. Commented Apr 24, 2019 at 14:00
  • Are manipulating $csv at all after you do the Import-Csv? If all of your data including the headers is not truly separated by |, then using the -Delimiter "|" will result in your situation. If the CSV file has commas rather than |, don't use -Delimiter. That switch tells PowerShell how your data is already separated and not how you want it to be separated after you import it. Commented Apr 24, 2019 at 14:34
  • I found why it wasn't working! Even though the headers of my csv are in caps, it only returned me the wanted output if i didn't use the caps. Meaning if I type $csv.ip instead of $csv.IP. Cannot believe it was this minor detail all along lol. Thank you for your outputs! Commented Apr 25, 2019 at 14:13

2 Answers 2

1

Taking your approach literal, you can use New-Variable, Get-Variable

## Q:\Test\2019\04\24\SO_55830910.ps1
$csv = Import-Csv $Env:USERPROFILE\Documents\ipaddresses.csv -delimiter "|" 

foreach($item in $csv){
    "{0} = {1}" -f $item.Name,$item.IP
    New-Variable -Name $item.Name -Value $item.IP
}

1..6|%{Get-Variable "n$_"}

n1 = 165.XX.XX.1
n2 = 165.XX.XX.2
n3 = 165.XX.XX.3
n4 = 165.XX.XX.4
n5 = 165.XX.XX.5
n6 = 165.XX.XX.6

Name                           Value
----                           -----
n1                             165.XX.XX.1
n2                             165.XX.XX.2
n3                             165.XX.XX.3
n4                             165.XX.XX.4
n5                             165.XX.XX.5
n6                             165.XX.XX.6
Sign up to request clarification or add additional context in comments.

Comments

0

Your loop is returning nothing because you're not storing or yielding any result. Here's how it should be done :

$csv = Import-Csv C:\Users\[...]\Documents\ipaddresses.csv -delimiter "|"
$csv | Select-Object -Property IP,Name

# Or if you want a custom property
$csv | Select-Object -Property IP, @{Name="Custom Name", Expression = {"Hostname is : " + $_.Name}

Read more about Select-Object : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-3.0

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.