0

I want to search files from the subfolder and grep row from the file. Text files have 3 rows, the expected result is grepping 2nd row. I hope the file name pass to Get-Content, but it failed.

Get-ChildItem -Name -Recurse -File -Include *.txt* | (Get-Content $_)[2]

2 Answers 2

1

What you are doing is almost fine, the parenthesis should be wrapping the whole expression and $_ should not be there:

(Get-ChildItem -Name -Recurse -File -Filter *.txt* | Get-Content)[2]

You could add -TotalCount 2 to Get-Content so it only reads the first 2 lines however if the files only have 3 lines it wouldn't make much of a difference.

Sign up to request clarification or add additional context in comments.

Comments

0

I understand Get-ChildItem is not String.

$f = (Get-ChildItem -Name -Recurse -File -Filter *.txt*) -as [string[]]
$i = 1
foreach($j in $f) {
#Write-Host $i : $j
Write-Host (Get-Content $j)[2]
$i++
}

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.