Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Read each line from text file and write to excel

$
0
0

Hi All,

I am currently working on script where I have a list of servers in a text file. Something like,

server01

server02

server03

one by one in a line. Now I want to read each line from this text file and write to a excel. I am doing it in this way... I will count the number of lines in the text file given. I will loop a variable until that number of lines and every time I write that value to excel column. I have got until here: 

 

foreach ($computer in (Get-Content "$var")) {  #$var is a text file with server names

$k = 1

$numberofservers = Get-Content "$var" | Measure-Object -Line -IgnoreWhiteSpace | Select-Object -ExpandProperty Lines

for (; $k -le $numberofservers; $k++)

$sheet5.Cells.Item(1,$k) = "$computer"

}

}

 

Here, for all the columns, it is writing the last server name. I want it in as given in the below screenshot. 

But it is coming like this:

Any help?

Chaitanya.

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles