≡ Menu

PowerShell: Get network adapter connection status

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 (0x0)
Disconnected
1 (0x1)
Connecting
2 (0x2)
Connected
3 (0x3)
Disconnecting
4 (0x4)
Hardware not present
5 (0x5)
Hardware disabled
6 (0x6)
Hardware malfunction
7 (0x7)
Media disconnected
8 (0x8)
Authenticating
9 (0x9)
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…

Comments on this entry are closed.

  • sunny October 8, 2012, 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