Don’t do that: You want to return the line number of a specific string “user04”.
File.txt
1 2 3 4 5 |
user01 user02 user03 user04 user05 |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 |
$file = Get-Content -Path .\test.txt $i = 0 foreach ($line in $file) { $i++ if ($line -match 'user04') { Write-Output -InputObject "Line : $i" } } |
Do that: A faster method to return the line number is:
(Select-String -Path .\test.txt -Pattern 'user04').LineNumber