Detect VPN Connection State with PowerShell

I wrote this script because I had a need to detect the current state of a VPN connection.  With Windows 10, you can use the “Get-VpnConnection” cmdlet.

However, if you are working in an environment where that commandlet is not available you can use a WMI query to determine the VPN Connection State.

# Written By: Harry Caskey (harrycaskey@gmail.com)
# In this example, I used "AnyConnect", "Juniper" or "VPN" as the connection name's, but you can change this to whatever fits your environment.
$vpnCheck = Get-WmiObject -Query "Select Name,NetEnabled from Win32_NetworkAdapter where (Name like '%AnyConnect%' or Name like '%Juniper%' or Name like '%VPN%') and NetEnabled='True'"

# Set this value to Boolean if it returns a value it's true, if it does not return a value it's false.
$vpnCheck = [bool]$vpnCheck

# Check if $vpnCheck is true or false.
if ($vpnCheck) {
    return $vpnCheck
    exit(0)
}
else {
    return $vpnCheck
    exit(1)
}

One Reply to “Detect VPN Connection State with PowerShell”

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.