PoshCode Logo PowerShell Code Repository

Get Network Utilization (modification of post by Jason Ochoa view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1900"></script>download | new post

get utilization from all network interfaces

  1. $counters = @()
  2. foreach ($inst in (new-object System.Diagnostics.PerformanceCounterCategory("network interface")).GetInstanceNames()){
  3.         $cur = New-Object system.Diagnostics.PerformanceCounter('Network Interface','Bytes Total/sec',   $inst)
  4.         $max = New-Object system.Diagnostics.PerformanceCounter('Network Interface','Current Bandwidth', $inst)
  5.  
  6.         $cur.NextValue() | Out-Null
  7.         $max.NextValue() | Out-Null
  8.  
  9.         $counters += @{"Throughput"=$cur;"Bandwidth"=$max;"Name"=$inst}
  10. }
  11.  
  12. sleep 2
  13.  
  14. foreach($counter in $counters) {
  15.  
  16.         $curnum = $counter.Throughput.NextValue()
  17.         $maxnum = $counter.Bandwidth.NextValue()
  18.  
  19.         New-Object PSObject -Property @{"Util"=$((( $curnum * 8 ) / $maxnum ) * 100);"Name"=$counter.Name}
  20. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me