0
#gets the file location/file name of the IP address
Param($addr)
$file = Get-Content $addr
$i = 0
while ($i -ne $file.Count) {
  Write-Host $file
  $i++
}

Output:

8.8.8.8 127.0.0.1 208.67.222.222
8.8.8.8 127.0.0.1 208.67.222.222
8.8.8.8 127.0.0.1 208.67.222.222

File content:

8.8.8.8
127.0.0.1
208.67.222.222

I only want it to iterate and print out one line, not all three lines on a single line 3 times. I need this so I can run a ping command on each address.

2
  • 2
    If you only want one line then you should be indexing, which you are not currently, in your line write-host $file. So the simple fix would be to $file[$i]. But you could be using different loop structures alltogether. get-content $addr | Foreach-Object{ "do something with $_"} Commented Apr 19, 2017 at 16:13
  • This worked, thank you :) Commented Apr 19, 2017 at 16:14

2 Answers 2

1

You've to use the index operator [] on $file.

#gets the file location/file name of the IP address
param($addr)
$file = get-content $addr
$i=0 
while ($i -ne $file.count){
   write-host $file[i]
   $i++
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can use $_ as file row with pipe like this :

Param($addr)
Get-Content $addr | foreach{ $_ }


#short version
Param($addr)
gc $addr | %{ $_ }

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.