{"id":2198,"date":"2014-01-17T00:01:00","date_gmt":"2014-01-17T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/01\/17\/gathering-network-statistics-with-powershell\/"},"modified":"2014-01-17T00:01:00","modified_gmt":"2014-01-17T00:01:00","slug":"gathering-network-statistics-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/gathering-network-statistics-with-powershell\/","title":{"rendered":"Gathering Network Statistics with PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about various ways to gather network statistics by using Windows PowerShell.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Hey, it is nearly the weekend! This means we are one week closer to Windows PowerShell Saturday #007&#8230;<\/p>\n<p><a href=\"http:\/\/powershellsaturday.com\/007\/conference\/powershell-saturday-007-charlotte-nc-february-8-2014\/\" target=\"_blank\">PowerShell Saturday #007<\/a> will be held in Charlotte, North Carolina on February 8, 2014. This will be an awesome chance to meet and to learn from some of the best PowerShellers around. In fact, five of the speakers are also speakers at the PowerShell Summit this year. There are a limited number of tickets still available for this event, so you&rsquo;ll want to sign up now. The Scripting Wife wrote a great post that provides a quick overview of the event: <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/psst-charlotte-powershell-saturday-details-leaked\/\" target=\"_blank\">Psst&#8230;Charlotte PowerShell Saturday Details Leaked<\/a>.<\/p>\n<p><b>&nbsp;&nbsp;&nbsp;&nbsp; Note<\/b>&nbsp; Today I am concluding my series about working with network adapters. <br \/> &nbsp;&nbsp;&nbsp;&nbsp; You may want to refer to the earlier posts to catch up on the series:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-identify-network-adapter-characteristics\/\" target=\"_blank\">Use PowerShell to Identify Network Adapter Characteristics<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/enabling-and-disabling-network-adapters-with-powershell\/\" target=\"_blank\">Enabling and Disabling Network Adapters with PowerShell<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/renaming-network-adapters-by-using-powershell\/\" target=\"_blank\">Renaming Network Adapters by Using PowerShell<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/using-powershell-to-find-connected-network-adapters\/\" target=\"_blank\">Using PowerShell to Find Connected Network Adapters<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/working-with-network-adapter-power-settings\/\" target=\"_blank\">Working with Network Adapter Power Settings<\/a><span style=\"text-decoration:underline\"><\/span><\/li>\n<\/ul>\n<p>One of the cool things about the Windows platform is all the ways that are possible to obtain networking statistical information. There are NetStat, Netsh, performance counters, and the <b>Get-NetworkStatistics<\/b> function from the <b>NetAdapter<\/b> Windows PowerShell module. All of these methods can be used inside the Windows PowerShell console or from within the Windows PowerShell ISE.<\/p>\n<h2>Netsh<\/h2>\n<p>Using Netsh to obtain network statistics is easy and powerful. For example, to show IP statistics, I use the following command:<\/p>\n<p style=\"margin-left:30px\">netsh interface ipv4 show ipstats<\/p>\n<p>A sample output from this command is shown in the image that follows:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>To use Netsh to show TCP statistics, I use the command shown here:<\/p>\n<p style=\"margin-left:30px\">netsh interface ipv4 show tcpstats<\/p>\n<p>The command and the output from the command are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One of the cool things about using Netsh from within Windows PowerShell is that I have the power of Windows PowerShell at my fingertips. Rather than going back and forth to find stuff, I can pipe the results from a command to the <b>Select-String<\/b> cmdlet.<\/p>\n<p>For example, if I am interested in how many commands are available to show statistics, I use the following command because I noticed that each of the commands contains the letters <b>stats<\/b>:<\/p>\n<p style=\"margin-left:30px\">netsh interface ipv4 show&nbsp; | Select-String &quot;stats&quot;<\/p>\n<p>The output from the command is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; netsh interface ipv4 show&nbsp; | Select-String &quot;stats&quot;<\/p>\n<p style=\"margin-left:30px\">show icmpstats &#8211; Displays ICMP statistics.<\/p>\n<p style=\"margin-left:30px\">show ipstats&nbsp;&nbsp; &#8211; Displays IP statistics.<\/p>\n<p style=\"margin-left:30px\">show tcpstats&nbsp; &#8211; Displays TCP statistics.<\/p>\n<p style=\"margin-left:30px\">show udpstats&nbsp; &#8211; Displays UDP statistics.<\/p>\n<p>In addition to the IPV4 interface, I can also work with the IPV6 interface and obtain similar statistics. Here is the command that I used to obtain that information:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; netsh interface ipv6 show&nbsp; | Select-String &quot;stats&quot;<\/p>\n<p style=\"margin-left:30px\">show ipstats&nbsp;&nbsp; &#8211; Displays IP statistics.<\/p>\n<p style=\"margin-left:30px\">show tcpstats&nbsp; &#8211; Displays TCP statistics.<\/p>\n<p style=\"margin-left:30px\">show udpstats&nbsp; &#8211; Displays UDP statistics.<\/p>\n<p>In addition to using the <b>Select-String<\/b> cmdlet to parse the output from the Netsh Help, I can use it to hone in on specific information from the statistics. For example, the following command retrieves IPv6 interface IP stats:<\/p>\n<p style=\"margin-left:30px\">netsh interface ipv6 show&nbsp; ipstats<\/p>\n<p>I can hone in on the output and look for errors by piping the results to the <b>Select-String<\/b> cmdlet and choosing <b>errors<\/b>. This command is shown here.<\/p>\n<p style=\"margin-left:30px\">netsh interface ipv6 show ipstats | Select-String errors<\/p>\n<p>In the image that follows, I first show the command to retrieve the IPV6&nbsp; IP statistics. Next I show the output from the command. Then I filter the output to only errors by using the <b>Select-String<\/b> cmdlet. Lastly, I show the output from the filtered string.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>NetStat<\/h2>\n<p>The NetStat command has been in the Windows world for a long time. It provides a quick snapshot of connections from local ports to remote ports in addition to the protocol and the state of those connections.<\/p>\n<p>It takes a couple of minutes to run, and as a result, it makes sense to store the results of NetStat into a variable. I can then examine the information several times if I want to without having to wait to gather the information additional times. Here is an example of running the <b>NetStat<\/b> command and storing the results in a variable:<\/p>\n<p style=\"margin-left:30px\">$net = NetStat<\/p>\n<p>To display the information in an unfiltered fashion, I simply type <b>$net<\/b> at the Windows PowerShell prompt, and it displays all of the information that it gathered:<\/p>\n<p style=\"margin-left:30px\">$net<\/p>\n<p>The command to run <b>NetStat<\/b>, store the results in a variable, and examine the contents of the <b>$net<\/b> variable are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The real power, however, comes in using Windows PowerShell to parse the text output to find specific information. For example, the previous output shows multiple connections in various connected states. I can easily parse the output and find only the connections that are <b>Established<\/b>. The command I use is:<\/p>\n<p style=\"margin-left:30px\">$net | select-string &quot;Established&quot;<\/p>\n<p>The command and the output from the command are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Interestingly enough, I can also use Netsh to report on TCP connections. The command is shown here:<\/p>\n<p style=\"margin-left:30px\">netsh interface ipv4 show tcpconnections<\/p>\n<p>The output from the command, as shown in the image that follows, is a bit different than that received from <b>NetStat<\/b>:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-06.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-06.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Performance counters<\/h2>\n<p>To easily collect performance counter information, I need to know the performance counter set names. To do this, I use the <b>Get-Counter<\/b> cmdlet, and I choose all of the <b>ListSets<\/b>. I then like to sort on the <b>CounterSetName<\/b> property, and then select only that property. The following command retrieves the available list sets:<\/p>\n<p style=\"margin-left:30px\">Get-Counter -ListSet * |<\/p>\n<p style=\"margin-left:30px\">Sort-Object CounterSetName |<\/p>\n<p style=\"margin-left:30px\">Select-Object CounterSetName<\/p>\n<p>If I pipe the output to the <b>Out-GridView<\/b> cmdlet, I can easily filter the list to find the list sets I want to use. This command is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Counter -ListSet * |<\/p>\n<p style=\"margin-left:30px\">Sort-Object CounterSetName |<\/p>\n<p style=\"margin-left:30px\">Select-Object CounterSetName |<\/p>\n<p style=\"margin-left:30px\">Out-GridView<\/p>\n<p>The resulting <b>Out-GridView<\/b> pane makes it easy to filter for different values. For example, the image that follows filters for IP:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-07.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-07.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>When I have the <b>CounterSetName<\/b> value that &nbsp;want to query, it is a simple matter of plugging it into <b>Get-Counter<\/b> to first obtain the paths, as shown here:<\/p>\n<p style=\"margin-left:30px\">$paths = (Get-Counter -ListSet ipv4).paths<\/p>\n<p>Next, I use the paths with the <b>Get-Counter<\/b> cmdlet to retrieve a single instance of the IPv4 performance information:<\/p>\n<p style=\"margin-left:30px\">Get-Counter -Counter $paths<\/p>\n<p>The commands and the output from the commands are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-08.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-08.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I want to monitor a counter set for a period of time, I use the <b>&ndash;SampleInterval<\/b> property and the <b>&ndash;MaxSamples <\/b>parameter. In this way, I can specify how long I want the counter collection to run. An example of this technique is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Counter -Counter $paths -SampleInterval 60 -MaxSamples 60<\/p>\n<p>If I want to monitor continuously, until I type <b>Ctrl+C<\/b> to break the command, I use the <b>&ndash;Continuous<\/b> parameter and the <b>&ndash;SampleInterval<\/b> parameter. An example of this command is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Counter -Counter $paths -SampleInterval 30 -Continuous<\/p>\n<h2>Using Get-NetAdapterStatistics<\/h2>\n<p>The easiest way to gather network adapter statistics is to use the <b>Get-NetAdapterStatistics<\/b> function from the <b>NetAdapter<\/b> module. It provides a quick overview of the sent and received packets. An example of the command is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-NetAdapterStatistics<\/p>\n<p>The command and a sample output are shown in the image that follows:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-09.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-09.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I want to work with a specific network adapter, I can use the name of the adapter; or for more flexibility, I can pipe the results from the <b>Get-NetAdapter<\/b> function. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-NetAdapter -ifIndex 12 | Get-NetAdapterStatistics<\/p>\n<p>The <b>Get-NetAdapterStatistics<\/b> function returns more than only the bytes sent and received. To find the additional information, I like to pipe the results to the <b>Format-List<\/b> cmdlet. An example of this technique is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-NetAdapter -ifIndex 12 | Get-NetAdapterStatistics | format-list *<\/p>\n<p>The command,and the output associated with the command are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-10.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-1-17-14-10.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>This concludes Network Adapter Week. Join me tomorrow when I will talk about mobile devices, Active Sync, and Exchange. It is cool stuff that you do not want to miss.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about various ways to gather network statistics by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Hey, it is nearly the weekend! This means we are one week closer to Windows PowerShell Saturday #007&#8230; PowerShell Saturday #007 will be held in Charlotte, North Carolina on February [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[37,60,3,298,45],"class_list":["post-2198","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-networking","tag-performance","tag-scripting-guy","tag-tcpip","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about various ways to gather network statistics by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Hey, it is nearly the weekend! This means we are one week closer to Windows PowerShell Saturday #007&#8230; PowerShell Saturday #007 will be held in Charlotte, North Carolina on February [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2198"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2198\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}