Search code examples
powershellselect-string

how to extract specific line after matched line in select-string powershell


I want to search for text in a block or rather after the matched string but not immediately

e.g: From output of 'ipconfig', I wanna extract the IPv4 address in WiFi section only

ipconfig | sls -Pattern "Wifi" -Context 0, 4

returns matched string and 4 lines after it but I want only 4th line and "-Pattern 'IPv4'" returns all ips from all the adapters

How do I extract only Wifi IPv4?


Solution

  • The output object(s) returned by Select-String will have a Context property, which in turn holds an object with a PostContext property that holds the lines following the matched one:

    ipconfig | sls -Pattern 'Wifi' -Context 0,4 |ForEach-Object { $_.Context.PostContext[-1] }