Export (0) Print
Expand All
Expand Minimize

Converting the FileSystemObject's Line Property


Windows PowerShell


Definition: When reading through a file one line at a time, this property returns the line number of the current line.

Line


When you use the Get-Content cmdlet to read the contents of a text file in PowerShell, each line of the file is returned as an array item. You can access individual lines by accessing the appropriate index number of that line.

This example reads the text file C:\Scripts\test.txt. The text file will be stored as an array in the variable $a, one line of the text file in each array item. We then loop through the array, from the beginning (array item 0) to the end (array item $a.Length, the length of the array), displaying each line as we go along with the line number of that line. The current line is the array index plus 1 (because arrays start at 0).


$a = get-content c:\scripts\test.txt
for ($i = 0; $i -lt $a.length; $i++)
{
    $a[$i]
    "Line is " + ($i + 1)
}


See conversions of other FileSystemObject methods and properties.
Return to the VBScript to Windows PowerShell home page

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft