I would like the file to save with the count variable between the file prefix and the file extension. Out-File (file prefix + count variable + file extension). sort of thing. I’m ridiculously new to Powershell so please be merciful 😛
$max = get-content h:test1\test.txt | Measure-Object
$h = get-content h:test1\test.txt
For($i=0; $i -lt $max.count ; $i++){
select-string h:test2\*.txt -pattern $($h[$i]) | Format-List | Out-File (Join-Path h:\text $i)
}
Simply use
PowerShell’s parsing gives rise to a few very weird problem in edge cases, but overall it is designed to work as you would expect it to work in most, if not all, common cases.
In your case I would probably solve the problem a little differently:
The first block for the
ForEach-Objectin this case is run once at the start of the loop and initialises the counter variable. I tend to avoid explicit looping constructs (such asfor (x; y; z)orforeach (x in y)) in favour of pipeline solutions.