Share via


Need to read text file as an array and get elements from each line of array using Powershell

Question

Friday, June 1, 2012 4:35 PM

Hello,

I'm trying to read in a text file in a Powershell script.  The text file name is Database.txt and for this example it contains two lines as seen below:

Database.txt contents:

one,two,three,four
five,six,seven,eight

I need to read each line of the text file and assign each element of each line to a variable for further processing.  This code snipit below shows what I'm trying to do:

$DB = Get-Content C:\scripts\Database.txt
 foreach ($Data in $DB)
 {
 $First = $Data[0]
 $Second = $Data[1]
 $Third = $Data[2]
 $Fourth = $Data[3]
 
 write-host "First is: "$First
 write-host "Second is: "$Second
 write-host "Third is: "$Third
 write-host "Fourth is: "$Fourth
 Write-Host ""
 }

This code does not return the needed results.  The results it returns is this:

First is:  o
Second is:  n
Third is:  e
Fourth is:  ,

First is:  f
Second is:  i
Third is:  v
Fourth is:  e

I need it to return this instead:

First is:  one
Second is:  two
Third is:  three
Fourth is:  four

First is:  five
Second is:  six
Third is:  seven
Fourth is:  eight

I'm missing something and have tried other variations but so far I cannot get the needed results.  Does anyone know how to get the results I'm look for?  Thank you!

All replies (10)

Friday, June 1, 2012 4:48 PM ✅Answered | 3 votes

You can do it like this:

$DB = import-csv Database.txt
 foreach ($Data in $DB)
 {
 $First = $Data.v1
 $Second = $Data.v2
 $Third = $Data.v3
 $Fourth = $Data.v4
 
 write-host "First is: "$First
 write-host "Second is: "$Second
 write-host "Third is: "$Third
 write-host "Fourth is: "$Fourth
 Write-Host ""
 }

Database.txt:

v1,v2,v3,v4
one,two,three,four
five,six,seven,eight


Friday, June 1, 2012 4:58 PM ✅Answered | 1 vote

Or, if it is impractical to convert the database file into a .csv by adding a header row at the top, you should be able to convert $Data from an array of characters to an array of strings by addin one statement:

  foreach ($Data in $DB)
  {
  $Data = $Data -split(',')
  $First = $Data[0]

Al Dunbar


Friday, June 1, 2012 5:42 PM ✅Answered | 2 votes

$DB = Get-Content $file
foreach ($Data in $DB) {
  $First, $Second, $Third, $Fourth = $Data -split ',' -replace '^\s*|\s*$'

  write-host "First is: "$First
  write-host "Second is: "$Second
  write-host "Third is: "$Third
  write-host "Fourth is: "$Fourth
  Write-Host ""
}

Friday, June 1, 2012 6:30 PM

Thank you all for your speedy replies.  Each of these methods work when I try them.  I knew there was a way but just didn't see it.


Friday, June 1, 2012 6:30 PM

Thank you.  This works and I'll have to decide which way will work best for me.


Friday, June 1, 2012 6:30 PM

Thank you.  This works and I'll have to decide which way will work best for me.


Wednesday, May 9, 2018 9:54 AM

By default, Get-Content reads all the line in a text file and creates an array as its output with each line of the text as an element in that array.In this case, the array index number is equal to the text file line number. So we can get the each line of the txt file by using the array index number.

You can loop the array to read each line.

$TxtContent = Get-content -Path "C:\path\TestFile.txt"

$TxtContent1
$TxtContent[2]

[Refer this for complete example] : http://dotnet-helpers.com/powershell-demo/reading-from-text-files-with-powershell/


Wednesday, May 9, 2018 10:02 AM | 1 vote

It took you 6 years to figure that out?  Its a record.

\(ツ)_/


Thursday, July 26, 2018 7:40 PM

Despite the Moderator's snarky comment, this was very helpful to me, so thank you!


Friday, August 3, 2018 7:43 PM

Its taking you a lot longer to figure how to actually help people.