Parsing Raw Text (Part 1)

by Dec 30, 2016

Sometimes, you may want to extract valuable information from pure text results. One easy way is the use of Select-String. This example extracts only the text lines containing “IPv4”:

 
PS C:\> ipconfig | Select-String 'IPv4'

   IPv4 Address. . . . . . . . . . . : 192.168.2.112
 

If you are just interested in the actual IP address, you can refine the result and use a regular expression to extract the value you are after:

 
PS C:\> $data = ipconfig | select-string 'IPv4' 
PS C:\> [regex]::Matches($data,"\b(?:\d{1,3}\.){3}\d{1,3}\b") | Select-Object -ExpandProperty Value

192.168.2.112
 

[Regex]::Matches() takes the raw data and a regular expression pattern describing what you are after. The content matching the pattern can then be found in the property “Value”.

Twitter This Tip! ReTweet this Tip!