Search code examples
powershellpowershell-2.0powershell-3.0powershell-4.0

I want to capture a set of 5 consecutive lines from a file where after my condition matches by using for each loop in powershell


$ourfilesdata = Get-Content "P:\myfiles\details.txt" 

    foreach ($i in $ourfilesdata )
        {
        if ( $i -match '\Mobile\b') {continue)
          {
          Write-Output "$i"
          }
        }

**My input is like 50 lines **

aaaaaaa
bbbbb
Request
Mobile
Sim
datacard
internet
ccccccc
dddddddd
fffffff

Output

mobile
sim
datacard
internet

Note :- These input lines are horizontal fashion in my file


Solution

  • Select-String  "P:\myfiles\details.txt" -Pattern 'Request\b' -Context 0,5 |
        Foreach-Object { $_.Line,$_.Context.PostContext}
    

    This will 100% works and thank me later :-)