How can I filter output of my command? (Powershell)

Viewed 58

I am running a following script

[System.Net.Dns]::GetHostEntry('') | Where-Object {$_.AddressList -like "IPv4*"}

Which gives me this output:

HostName                      Aliases AddressList                                
--------                      ------- -----------                                
SERVER1.domain.com {}      {REDACTED_IPv6, IPv4.x.x.x}

My desired result would be this:

HostName                      Aliases AddressList                                
--------                      ------- -----------                                
SERVER1.domain.com                    {IPv4.x.x.x}

How can I go about achieving that?

1 Answers

Since the addresslist is an object itself you need to extract out the info you want. This should achieve your desired result.

[System.Net.Dns]::GetHostEntry('') |
    Select-Object HostName,
                  Aliases,
                  @{n="AddressList";e={$_.addresslist.ipaddresstostring}}

or

[System.Net.Dns]::GetHostEntry('') |
       Select-Object HostName,
                     Aliases,
                     @{n="AddressList";e={($_.addresslist|where addressfamily -eq InterNetwork).ipaddresstostring}}

or

[System.Net.Dns]::GetHostEntry('') |
       Select-Object HostName,
                     Aliases,
                     @{n="AddressList";e={$_.addresslist -like "10.*"}}

or

[System.Net.Dns]::GetHostEntry('') |
       Select-Object HostName,
                     Aliases,
                     @{n="AddressList";e={$_.addresslist -match "\d+\.\d+\."}}
Related