PowerShell: Get network adapter connection status

by Sitaram Pamarthi on November 9, 2011

This powershell function helps you to get the network status of all network adapters in a given computer. Using this script it is easy to determine the status of all network connections in remote computer to see whether it is connected/disconnected/media disconnected/and different other statuses. This script queries Win32_NetworkAdapter WMI class for list of network adapters and then fetches the status of each adapter by querying “netconnectionstatus” parameter. This is a uint16 data type which returns any value ranging from 0-12. Each value from 0 to 12 has it’s own meaning. See the below table for details(source: MSDN)

Value Meaning
0 (0×0)
Disconnected
1 (0×1)
Connecting
2 (0×2)
Connected
3 (0×3)
Disconnecting
4 (0×4)
Hardware not present
5 (0×5)
Hardware disabled
6 (0×6)
Hardware malfunction
7 (0×7)
Media disconnected
8 (0×8)
Authenticating
9 (0×9)
Authentication succeeded
10 (0xA)
Authentication failed
11 (0xB)
Invalid address
12 (0xC)
Credentials required

So, here is the script which lists the network adapter name and its status.

Script:

function Get-NetworkConnectionStatus {
param(
$ComputerName=$env:computername
)
$statushash = @{
0 = "Disconnected"
1 = "Connecting"
2 = "Connected"
3 = "Disconnecting"
4 = "Hardware not present"
5 = "Hardware disabled"
6 = "Hardware malfunction"
7 = "Media Disconnected"
8 = "Authenticating"
9 = "Authentication Succeeded"
10 = "Authentication Failed"
11 = "Invalid Address"
12 = "Credentials Required"
}
$networks = Gwmi -Class Win32_NetworkAdapter -ComputerName $computername
$networkName = @{name="NetworkName";Expression={$_.Name}}
$networkStatus = @{name="Networkstatus";Expression=` 
{$statushash[[int32]$($_.NetConnectionStatus)]}}
foreach ($network in $networks) {
  $network | select $networkName, $Networkstatus
  }
}

Usage:

Get-NetworkConnectionStatus -ComputerName PC1

Hope this helps…

This post was written by...

– who has written 462 posts on Techibee.com.

Sitaram Pamarthi is a windows administrators with 9+ years of experience.read more

{ 1 comment… read it below or add one }

sunny October 8, 2012 at 8:22 pm

Hi,

This is a good post .Just want to drill further ,
Lets say i have group of computers and all have connection named “local Area Connection” now i want to ,

1.Display a page that shows all computers and their conenction name “local Area Conenction” highlighted in green
2.If any connection name has been changed or missing it displays as red.

- Regards
Sunny

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: