user945761
user945761

Reputation: 21

How to use Select-Object with Test-Connection to report only certain columns

I'm using Test-Connection with Power-Shell 5.1. The command is

Test-Connection -Source $source -Destination $remoteHost

The result is:

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
DESKTOP-BH... 192.168.1.253                                                             32       1

I don't want to display the columns IPV4Address and IPV6Address. How can I use Select-Object to display the other columns Source, Destination, Bytes and Time? Command like:

Test-Connection -Source $source -Destination $remoteHost | Select-Objects Column1,column2 ...

Is there a documented list of names of objects displayed in Test-Connection power shell command?

Thanks.

Elik

I'm trying to get the right syntax of the power shell command and the location of the related document.

Upvotes: 1

Views: 84

Answers (3)

mklement0
mklement0

Reputation: 440637

Note: This answer applies to Windows PowerShell (the legacy, ships-with-Windows, Windows-only edition of PowerShell whose latest and last version is 5.1) rather than PowerShell (Core) 7 (in the latter, there is no discrepancy between the for-display column names and property names in Test-Connection's for-display output).


As js2010's helpful answer notes, the column names in the for-display output formatting definitions associated with the type of the objects output by Test-Connection do not correspond to the property names of the underlying objects, whereas only true property names can be used with Select-Object.

However, using the real property names alone will invariably also use these names as column names, which won't give you the desired output.

In order to use the original column names, you can use calculated properties to effectively rename the underlying properties:

# Windows PowerShell only.
Test-Connection -Source $source -Destination $remoteHost |
  Select-Object @{ Name='Source'; Expression='PSComputerName' },
                @{ Name='Destination'; Expression='Address' },
                @{ Name='Bytes'; Expression='BufferSize' },
                @{ Name='Time(ms)'; Expression='ResponseTime' }

A less verbose alternative, given that the calculated-property syntax is verbose and that all properties need renaming in your case, is to use a ForEach-Object call in which you can construct a [pscustomobject] from each Test-Connection output object:

# Windows PowerShell only.
Test-Connection -Source $source -Destination $remoteHost |
  ForEach-Object {
    [pscustomobject] @{
      Source      = $_.PSComputerName
      Destination = $_.Address
      Bytes       = $_.BufferSize
      'Time(ms)'  = $_.ResponseTime
    }
  }

Optional background information:

Is there a documented list of names of objects displayed in Test-Connection power shell command?

  • Fundamentally, as Santiago notes, you can pipe a cmdlet's output Get-Member to discover its members (adding -MemberType Properties restricts the output to property members).

    • For discovering property members only, including property values, you can alternatively pipe to Format-List -Force -Property *, but note that you'll get a separate block of property/value pairs for each input object.
  • As sirtao's answer shows, you can usually find at least the names of the output type(s) in the documentation, though there's often no link to the latter's documentation that would facility discovery of their members.
    (In the case at hand, the Windows PowerShell version of the Test-Connection documentation provides only a link to an abstract base class, whereas the PowerShell (Core) 7 version provides no link at all.)

Also, it may be tempting to use the -ExcludeProperty parameter with Select-Object (... | Select-Object -ExcludeProperty IPv?Address), but this does not work:

  • This is because when you use this parameter, the candidate set of properties are all (public) properties, not just the ones shown in the default output formatting, so you'll typically end up with additional, unwanted properties (display columns).

  • In other words: you must explicitly enumerate those properties (display columns) that you do want, and the properties shown by default may not be actual properties of the output objects, because the real property names may differ from the ones displayed (and/or their values may be transformed for display), which is indeed the case here in Windows PowerShell.

Upvotes: 0

js2010
js2010

Reputation: 27626

That command is notorious for not having the column names match the property names. One way to view the unformatted output, pipe to format-list (or format-list *):

test-connection yahoo.com -count 1 | fl

__GENUS                        : 2
__CLASS                        : Win32_PingStatus
__SUPERCLASS                   :
__DYNASTY                      : Win32_PingStatus
__RELPATH                      : Win32_PingStatus.Address="yahoo.com",BufferSize=32,NoFragmentation=FALSE,RecordRoute=0
                                 ,ResolveAddressNames=FALSE,SourceRoute="",SourceRouteType=0,Timeout=4000,TimestampRout
                                 e=0,TimeToLive=80,TypeofService=0
__PROPERTY_COUNT               : 24
__DERIVATION                   : {}
__SERVER                       : DELL2019
__NAMESPACE                    : root\cimv2
__PATH                         : \\DELL2019\root\cimv2:Win32_PingStatus.Address="yahoo.com",BufferSize=32,NoFragmentati
                                 on=FALSE,RecordRoute=0,ResolveAddressNames=FALSE,SourceRoute="",SourceRouteType=0,Time
                                 out=4000,TimestampRoute=0,TimeToLive=80,TypeofService=0
Address                        : yahoo.com
BufferSize                     : 32
NoFragmentation                : False
PrimaryAddressResolutionStatus : 0
ProtocolAddress                : 2001:4998:124:1507::f000
ProtocolAddressResolved        :
RecordRoute                    : 0
ReplyInconsistency             :
ReplySize                      :
ResolveAddressNames            : False
ResponseTime                   : 32
ResponseTimeToLive             :
RouteRecord                    :
RouteRecordResolved            :
SourceRoute                    :
SourceRouteType                : 0
StatusCode                     : 0
Timeout                        : 4000
TimeStampRecord                :
TimeStampRecordAddress         :
TimeStampRecordAddressResolved :
TimestampRoute                 : 0
TimeToLive                     : 80
TypeofService                  : 0
PSComputerName                 : DELL2019
IPV4Address                    : 74.6.143.26
IPV6Address                    : 2001:4998:124:1507::f000

Thus the real property names are pscomputername, address, buffersize, and responsetime:

test-connection yahoo.com -count 1 | 
  select pscomputername, address, buffersize, responsetime

PSComputerName address   buffersize responsetime
-------------- -------   ---------- ------------
DELL2019       yahoo.com         32           31

Upvotes: 1

sirtao
sirtao

Reputation: 2933

On Get-Help -Full -Name Test-Connection(OUTPUTS section) or The Docs, OUTPUTS section you can find the class(es) of the output.

by searching for that class(a combination of TestConnectionCommand+PingMtuStatus in this instance), you will find the list of its properties

This, of course, is also valid for any other command\class.

Upvotes: 0

Related Questions