Checking Network Connections

by Feb 2, 2018

If your machine is connected to the internet (or VPN) via different network connections, depending on where you are, then the following two functions may be useful to you.

Get-ActiveConnection lists the names of all network connections that currently have an IP address assigned. Test-ActiveConnection accepts a keyword and checks to see whether there are currently any active connections with the keyword in their name.

function Get-ActiveConnection
{
    Get-WmiObject -Class Win32_NetworkAdapterConfiguration |
    Where-Object { $_.IPAddress } |
    Select-Object -ExpandProperty Description
}
 
function Test-ActiveConnection
{
  param([Parameter(Mandatory)]$Keyword)
 
 
  @(Get-WmiObject -Class Win32_NetworkAdapterConfiguration |
    Where-Object { $_.IPAddress } |
    Where-Object { $_.Description -like "*$Keyword*" }).Count -gt 0
 
}

Here is a quick example output:

 
PS> Get-ActiveConnection
Dell Wireless 1820A 802.11ac
 
PS> Test-ActiveConnection dell
True
 
PS> if ( (Test-ActiveConnection dell) ) { Write-Warning "Connected via DELL network card" }
WARNING: Connected via DELL network card
 
PS>  

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at www.psconf.eu

Twitter This Tip! ReTweet this Tip!