Welcome to ciysys blog

Parsing text into object

Published on: 5th Dec 2019

Updated on: 16th Jan 2022


The result from a DOS command is text lines which do not have a "real" column. If you want to analyze the result, you must parse the text yourself.

For example, we want to find out the IP to physical addresses, we use the following command:

arp -a

The result looks like this:

Interface: --- 0x5
Internet Address      Physical Address      Type           18-31-bf-53-7a-24     dynamic         ff-ff-ff-ff-ff-ff     static            01-00-5e-00-00-16     static           01-00-5e-00-00-fb     static           01-00-5e-00-00-fc     static       01-00-5e-7f-ff-fa     static       ff-ff-ff-ff-ff-ff     static 

To parse this result, we will run the following Powershell script:

class myObject {

$l = (arp -a)

$inPreamble = $true

switch -Regex ($l) {

    # skip the header
    {$inPreamble -and $_ -match 'Internet Address'} { $inPreamble = $false; continue }

    # parse the item lines with pattern:
    # - spaces
    # - ip (non spaces)
    # - addr (non spaces)
    # - type (non spaces)

    "^\s+(?<ip>\S+)\s+(?<addr>\S+)\s+(?<type>\S+)" {        
        [myObject] @{
            ip = $matches.ip
            addr = $matches.addr
            type = $matches.type




Jump to #POWERSHELL blog


Lau Hon Wan, software developer.