Extracting Text Information

by May 18, 2016

Tracert.exe tells you the hops your data packages need to take to get to a given host. Here is an example:

 
PS C:\> tracert www.microsoft.com

Tracing route to e10088.dspb.akamaiedge.net [2.16.194.227]
over a maximum of 30 hops:

  1     8 ms     8 ms     9 ms  1st.railnet.train [10.204.90.1] 
  2    55 ms    47 ms    48 ms  ntp3.railnet.core [10.10.128.1] 
  3    61 ms    68 ms    51 ms  10.64.17.193 
  4   176 ms   160 ms   240 ms  10.64.21.37 
  5     *        *        *     Request timed out.
  6  2019 ms  1724 ms   942 ms  10.64.23.37 
  7     *        *        *     Request timed out.
  8     *        *        *     Request timed out.
  9     *        *        *     Request timed out.
 10     *      499 ms   713 ms  193.159.166.50 
 11  1864 ms  2370 ms     *     a2-16-194-227.deploy.akamaitechnologies.com [2.16.194.227] 
 12  3199 ms  3787 ms     *     a2-16-194-227.deploy.akamaitechnologies.com [2.16.194.227] 
 13     *        *        *     Request timed out.
 14     *        *        *     Request timed out.
 15  1173 ms     *     2522 ms  a2-16-194-227.deploy.akamaitechnologies.com [2.16.194.227] 

Trace complete.
 

If you were just interested in the names of the hops your packages took, what would you do?

One way is to count the number of characters that you want to ignore per line. In our example, the first 32 characters per line should be ignored. Here is how:

 
PS C:\>  $route = tracert www.microsoft.com  
PS C:\>  $route.Foreach{$_.SubString(32)} 
 
maiedge.net  [2.16.194.227]
1st.railnet.train  [10.204.90.1] 
ntp3.railnet.core  [10.10.128.1] 
10.64.17.193 
10.64.21.37 
Request timed out.
10.64.23.37 
Request timed out.
Request timed out.
Request timed out.
193.159.166.50 
a2-16-194-227.deploy.akamaitechnologies.com  [2.16.194.227] 
a2-16-194-227.deploy.akamaitechnologies.com  [2.16.194.227] 
Request timed out.
Request timed out.
a2-16-194-227.deploy.akamaitechnologies.com  [2.16.194.227]
 

Note that the Foreach() method was introduced in PowerShell 3.0. Use a pipeline with ForEach-Object in older PowerShell versions.

Twitter This Tip! ReTweet this Tip!