Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find which network profile(s) are active via cmd or batch script

In the windows registry here:

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\

we can see a list of network IDs with Network name like usually the word Network plus a number eg Network 1 for wired networks.

When Windows first detects a new network you get a pop up asking you if this a Work, Home or Public location. The choice you make changes the Category and Category type in the above mentioned registry entry for that network ID.

I want to retrieve the Category and Category type for the currently connected network profile. Essentially telling me if the network I'm connected to right now is Work, Home or Public.

I know how to do this with a reg query already but I don't know how to find out which of the many network profiles in the above registry path is the active one without looking through them all and matching the network name to the current network.

I looked into using wmic path Win32_NetworkAdapter and I thought I found the way, see here:

for /f "tokens=*" %%f in ('wmic /node:MYPC path WIN32_NetworkAdapter where "GUID is not null" get GUID /value ^| find "="') do set "%%f"

reg query "\\MYPC\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\%GUID%" /v Category 

I though I found the ID of the current active network by using wmic, using wmic path WIN32_NetworkAdapter as a key called GUID as it was the exact same format as the network IDs you see in the registry path specified above.

As I only had one active network and the above wmic command returned only one non null GUID. But I did not find the returned GUID in the network profile list...

If I can find a way to determine which subkey / network ID in that registry path corresponds to my active network then I can get my desired result.

How can I find out which network profile is being used currently?

like image 252
Nolanr Avatar asked Oct 15 '22 14:10

Nolanr


1 Answers

Refer to Extracting Network Profiles and displaying only Network Profile description in txt file

You can try something like that :

@echo off
Title GET GUID NETWORK PROFILES
Mode 70,5 & color 0A
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
REM  --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO              **************************************
ECHO               Running Admin shell... Please wait...
ECHO              **************************************

    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
::::::::::::::::::::::::::::
::          START         ::
::::::::::::::::::::::::::::
Mode 90,20 & color 0A
SetLocal EnableExtensions DisableDelayedExpansion
(Set k=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles)
For /F "Delims==" %%A In ('Set GUID[ 2^>Nul') Do Set "%%A="
Set "i=101"
For /F "Tokens=1,2*" %%A In ('Reg Query "%k%" /S /V Description') Do (
    If "%%~nB" NEQ "%%~B" (
        Call Set "GUID[%%i:*1=%%]=%%~nB"
    ) Else (
        Call Call Set GUID[%%i:*1=%%]="%%%%GUID[%%i:*1=%%]%%%%","%%C"
        Set/A i+=1
    )
)
If %i% NEQ 101 (Set GUID[) |findstr /r "{[ABCDEF0123456789][ABCDEF0123456789-][ABCDEF0123456789-]*" 
If %i% NEQ 101 (>"%~dp0NetProfs.txt" 2>Nul Set GUID[ |findstr /r "{[ABCDEF0123456789][ABCDEF0123456789-][ABCDEF0123456789-]*")
EndLocal
pause & exit
like image 114
Hackoo Avatar answered Oct 18 '22 21:10

Hackoo