user3436467
user3436467

Reputation: 1775

powershell command to get network adapter connectivity status

I am trying to find a Powershell command that will give me the Connectivity Status for all Network Adapters, for the ones where Connectivity equal "No network access" disable and re-enable the adapter.

First I need to get the correct status, I have tried the following command but it only shows the physically connection. Note the Status for all of them is Up even if the Connectivity status shows "No network access".

Get-NetAdapter -physical

Name                      InterfaceDescription                    ifIndex Status       MacAddress
----                      --------------------                    ------- ------       ----------
Ethernet 5                Remote NDIS based Internet Sharing...#6      26 Up           XX-XX-XX-XX-X...
Ethernet 11               Remote NDIS based Internet Sharing...#7      23 Up           XX-XX-XX-XX-X...
Ethernet 10               Remote NDIS based Internet Sharing...#8      54 Up           XX-XX-XX-XX-X...
Ethernet 12               Remote NDIS based Internet Sharing...#4      17 Up           XX-XX-XX-XX-X...
Ethernet 6                Remote NDIS based Internet Sharing...#5      15 Up           XX-XX-XX-XX-X...
Ethernet 4                Remote NDIS based Internet Sharing...#3      13 Up           XX-XX-XX-XX-X...
Ethernet 9                Remote NDIS based Internet Sharin...#11      12 Up           XX-XX-XX-XX-X...
Ethernet 3                Remote NDIS based Internet Sharing...#2      10 Up           XX-XX-XX-XX-X...
Ethernet 7                Remote NDIS based Internet Sharing...#9       7 Up           XX-XX-XX-XX-X...
Ethernet                  Intel(R) Ethernet Connection (2) I21...       6 Up           XX-XX-XX-XX-X...
Ethernet 8                Remote NDIS based Internet Sharin...#10       4 Up           XX-XX-XX-XX-X...

However, what I am trying to target is the Connectivity status as shown in the Network section

enter image description here

Upvotes: 3

Views: 9936

Answers (2)

wasif
wasif

Reputation: 15480

Use WMI to do this.

get-wmiobject win32_networkadapter | select netconnectionid, name, InterfaceIndex, netconnectionstatus

Look at netconnectionstatus. You will find a value there. That is the connectivity status.

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

Upvotes: 2

Justin
Justin

Reputation: 1503

expanding on @postanote answer, it is more efficient to call Get-NetConnectionProfile once and store the results. I also used -eq instead of -match to ensure predictable results. Also if it is more than a 1-to-1 relationship the results could be unpredictable. His answer did not work on windows 10.

#get profiles and adapters
$Profiles = Get-NetConnectionProfile
$Adapters = Get-NetAdapter -Physical

#loop on adapters
$Results = $Adapters | ForEach-Object {
    #get the current adapter
    $Adapter = $PSItem
    #find the associated profiles
    $AdapterProfiles = $Profiles | where {$Adapter.ifIndex -eq $_.InterfaceIndex}

    #output the merged results by looping on the profiles
    $AdapterProfiles | 
         Select-Object @{n='Name';e={$Adapter.Name}},
                       @{n='DeviceName';e={$Adapter.DeviceName}},
                       @{n='InterfaceDescription';e={$Adapter.InterfaceDescription}},
                       @{n='Status';e={$adapter.Status}},
                       IPv4Connectivity,
                       NetworkCategory
}

#view the output in a nice interactive table
$results | Out-GridView

alternative output as a table:

$results | Format-Table

output:

Name  DeviceName                                     InterfaceDescription                Status IPv4Connectivity NetworkCategory
----  ----------                                     --------------------                ------ ---------------- ---------------
Wi-Fi \Device\{FC0C77D5-E521-46C9-A11D-A1E018ED8DEA} Intel(R) Dual Band Wireless-AC 7265 Up             Internet         Private

Upvotes: 5

Related Questions