Is there a simple cmdlet I can run in PowerShell to determine if my Windows machine is connected to the internet through Ethernet or through the wireless adapter? I know you can determine this on the GUI, I just want to know how this can be managed in PowerShell.
4 Answers
The PowerShell cmdlet Get-NetAdapter
can give you a variety of info about your network adapters, including the connection status.
Get-NetAdapter | select Name,Status, LinkSpeed
Name Status LinkSpeed
---- ------ ---------
vEthernet (MeAndMahVMs) Up 10 Gbps
vEthernet (TheOpenRange) Disconnected 100 Mbps
Ethernet Disconnected 0 bps
Wi-Fi 2 Up 217 Mbps
Another option is to run Get-NetAdapterStatistics
which will show you stats only from the currently connected device, so we could use that as a way of knowing who is connected to the web.
Get-NetAdapterStatistics
Name ReceivedBytes ReceivedUnicastPackets SentBytes SentUnicastPackets
---- ------------- ---------------------- --------- ------------------
Wi-Fi 2 272866809 323449 88614123 178277
Better Answer
Did some more research and found that if an adapter has a route to 0.0.0.0, then it's on the web. That leads to this pipeline, which will return only devices connected to the web.
Get-NetRoute | ? DestinationPrefix -eq '0.0.0.0/0' | Get-NetIPInterface | Where ConnectionState -eq 'Connected'
ifIndex InterfaceAlias AddressFamily InterfaceMetric Dhcp ConnectionState
------- -------------- ------------- --------------- ------- ---------------
17 Wi-Fi 2 IPv4 1500 Enabled Connected
Get-NetConnectionProfile
will return the Internet connectivity status for each connected network adapter using the Network Connectivity Status Indicator (the same indicator as used by Windows in the properties of a network device):
Name : <primary DNS suffix>
InterfaceAlias : Ethernet
InterfaceIndex : 9
NetworkCategory : DomainAuthenticated
IPv4Connectivity : Internet
IPv6Connectivity : LocalNetwork
Name : <primary DNS suffix>
InterfaceAlias : WiFi
InterfaceIndex : 12
NetworkCategory : DomainAuthenticated
IPv4Connectivity : Internet
IPv6Connectivity : LocalNetwork
You should be able to use the IPv4Connectivity or IPv6Connectivity to give you a true/false value what you want. The following will check if Windows thinks any network device is connected to the Internet via either IPv4 or IPv6:
$AllNetConnectionProfiles = Get-NetConnectionProfile
$AllNetConnectionProfiles | Where-Object {$_.IPv4Connectivity -eq 'Internet' -or $_.IPv6Connectivity -eq 'Internet'}

- 335
- 2
- 13
I wrote a function that does this. It should work on all versions of PowerShell, but I have not tested it on XP / Server 2003.
function Test-IPv4InternetConnectivity
{
# Returns $true if the computer is attached to a network that has connectivity to the
# Internet over IPv4
#
# Returns $false otherwise
# Get operating system major and minor version
$strOSVersion = (Get-WmiObject -Query "Select Version from Win32_OperatingSystem").Version
$arrStrOSVersion = $strOSVersion.Split(".")
$intOSMajorVersion = [UInt16]$arrStrOSVersion[0]
if ($arrStrOSVersion.Length -ge 2)
{
$intOSMinorVersion = [UInt16]$arrStrOSVersion[1]
} `
else
{
$intOSMinorVersion = [UInt16]0
}
# Determine if attached to IPv4 Internet
if (($intOSMajorVersion -gt 6) -or (($intOSMajorVersion -eq 6) -and ($intOSMinorVersion -gt 1)))
{
# Windows 8 / Windows Server 2012 or Newer
# First, get all Network Connection Profiles, and filter it down to only those that are domain networks
$IPV4ConnectivityInternet = [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetConnectionProfile.IPv4Connectivity]::Internet
$internetNetworks = Get-NetConnectionProfile | Where-Object {$_.IPv4Connectivity -eq $IPV4ConnectivityInternet}
} `
else
{
# Windows Vista, Windows Server 2008, Windows 7, or Windows Server 2008 R2
# (Untested on Windows XP / Windows Server 2003)
# Get-NetConnectionProfile is not available; need to access the Network List Manager COM object
# So, we use the Network List Manager COM object to get a list of all network connections
# Then we check each to see if it's connected to the IPv4 Internet
# The GetConnectivity() method returns an integer result that can be bitwise-enumerated
# to determine connectivity.
# See https://msdn.microsoft.com/en-us/library/windows/desktop/aa370795(v=vs.85).aspx
$internetNetworks = ([Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]"{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"))).GetNetworkConnections() | `
ForEach-Object {$_.GetNetwork().GetConnectivity()} | Where-Object {($_ -band 64) -eq 64}
}
return ($internetNetworks -ne $null)
}

- 560
- 1
- 5
- 18
Test-Connection -ComputerName $servername
Where $servername
is a web address. Use the -Quiet
switch to return true/false.

- 513
- 4
- 7
-
1This is not what the user is asking. – AutomatedOrder Oct 22 '15 at 15:26
-
1You should probably expand this a bit to explain how to make this reliable in the face of possible outages on the other end. – Nathan Tuggy Oct 23 '15 at 03:01
-
1Use `Test-Connection -ComputerName www.google.com -Quiet` to simply and painlessly confirm if you can access Google, and therefore have an "active internet connection", without drilling into details like on a given network adapter. – BuvinJ Jan 12 '21 at 22:19