1

Windows automatically identifies connected networks and names them as Wireless Network, Wired Network 2 or windows.domain.tld in case of domain joined systems. Is there any command to get the string of the connected network that Windows has detected it's on?

UPDATE

After trying @Bob's answer, I've noticed it the output lists a line for every network it's connected to:

> Get-NetConnectionProfle | Select-Object -ExpandProperty Name
Wired Connection
Wireless Connection 3
VPN Name

By default Windows only uses one of the connected networks for all traffic, such as only wired connection even as wireless is connected as well, and only the VPN if connected. What do I do to get only the name string of the default network?

  • There appears to be some confusion about what you are actually expecting as output (so I deleted my answer). It's now undeleted so please check if it meets your needs. It outputs the name of the currently "Connected" network connection. – DavidPostill Oct 11 '15 at 18:33
2

To find default interface, you have to find default route with lowest metric. You can do this with following PowerShell code:

$DefaultIPv4InterfaceIndex=
Get-NetRoute -DestinationPrefix 0.0.0.0/0|
Sort-Object {$_.RouteMetric+(Get-NetIPInterface -AssociatedRoute $_).InterfaceMetric}|
Select-Object -First 1 -ExpandProperty InterfaceIndex

$DefaultIPv6InterfaceIndex=
Get-NetRoute -DestinationPrefix ::/0|
Sort-Object {$_.RouteMetric+(Get-NetIPInterface -AssociatedRoute $_).InterfaceMetric}|
Select-Object -First 1 -ExpandProperty InterfaceIndex

Note, that default interface for IPv4 and for IPv6 can be different. Now, as you have interface index, you can use Get-NetConnectionProfile to get network name:

(Get-NetConnectionProfile -InterfaceIndex $DefaultIPv4InterfaceIndex).Name
(Get-NetConnectionProfile -InterfaceIndex $DefaultIPv6InterfaceIndex).Name
  • Beat me to it! Great answer. Could make it a one-liner with a filter, but that's not too important. – Bob Oct 11 '15 at 10:02
  • I successfully used it in my first PowerShell script ever. Thank you very much for this snippet! – Oxwivi Oct 12 '15 at 8:50
1

If you can use PowerShell, as of Windows Server 2012 R2/Windows 8.1 it has a Get-NetConnectionProfile cmdlet, and you can extract just the network name with the following:

Get-NetConnectionProfile | Select-Object -ExpandProperty Name

You can run this on earlier versions of Windows by installing PowerShell 4.0 (Windows Management Framework 4.0).

You can run this from the legacy Command Prompt by passing it into PowerShell with powershell -c.

You could also access the registry keys directly, though I'm unsure of how you'd figure out which one is the current one: How can I rename a network in Windows 8?

  • I just need to get the name for scripting, that is to say to figure out if the system is connected to my LAN and, if not connected to LAN, connect to the VPN that does. I think either this command or the one you edited out would be enough (provided they work). I'll accept the answer when I get to test it out (not on a Windows system currently). – Oxwivi Oct 10 '15 at 10:13
  • On a related note, I also asked how it is possible to connect to an L2TP/IPsec VPN on cmd.exe for the same script. If you know anything, please weigh in. – Oxwivi Oct 10 '15 at 10:16
  • Bit of a problem your command—it lists a line for every network it's connected to, i.e. one line for wireless, another for wired and a third for VPN. – Oxwivi Oct 11 '15 at 7:20
  • I'm changing the question to the string of default network the system is connected to. Even if both wired and wireless is connected, Windows by default uses only one for all it's connections, and similarly uses only the VPN when connected. – Oxwivi Oct 11 '15 at 7:23
-1

If you go to Controlpanel > Network & Internet > Network & Sharing center it will show active networks.

You can also go from Network & Sharing center and click Edit Settings for the network card. It will show all networks with the relative status.

Note: the setting names can be mistranslated from my Danish PC.

  • I specifically asked for cmd.exe, sorry. – Oxwivi Oct 11 '15 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.