22

How can I force Windows to re-scan for available wireless networks from the command line?

(i.e. I'm looking for the command-line equivalent of pressing F5 on the GUI.)

4
  • Does typing the command and pressing the up button + enter not work, or are you using it as part of a larger command?
    – cutrightjm
    Mar 14, 2015 at 2:11
  • 2
    @ekaj: Typing "the command"? What command?
    – user541686
    Mar 14, 2015 at 2:32
  • netsh wlan show networks... add mode=bssid on the end to show the channels, encryption types, and rates
    – cutrightjm
    Mar 14, 2015 at 3:00
  • 3
    @ekaj: That doesn't refresh anything, it just shows the cached data.
    – user541686
    Mar 14, 2015 at 3:03

8 Answers 8

12

I've been looking for the same thing for a while now, unfortunately I can't seem to find anything in the netsh utility to do this. The best solution I was able to come up with was to disable then re-enable the interface through the netsh commands. There's a small delay (1-2 seconds) after enabling the interface where it won't list any networks, but that shouldn't be too hard to workaround.

I'm using the following commands:

netsh interface set interface name="<NIC name>" admin=disabled
netsh interface set interface name="<NIC name>" admin=enabled
netsh wlan show networks

Edit: How to get then the "<NIC name>", i.e., the Network Interface Card name.

First of all use the command netsh wlan show networks

If the wlan is enabled you will get a message like this

C:\Users\user1>netsh wlan show networks

Interface name : WiFi
There are 2 networks currently visible.

SSID 1 : Cross
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP

SSID 2 : WLAN-0A8E41
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP

So you see, in this case, the interface name is WiFi Now, you can proceed as explained above, for example to disable the wlan card:

netsh interface set interface name="WiFi" admin=disabled

And so on. :)

12

I wrote a small (~2 KiB) WlanScan.exe program to do this.

Here's the base64 (32-bit version):

TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAADJCgvEjWtll41rZZeNa2WXjWtkl4RrZZdOZDiXi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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=

You can save it to a file from PowerShell like this (save the Base64 to WlanScan.txt first):

Set-Content -Path "WlanScan.exe" $([Convert]::FromBase64String($(Get-Content -Path "WlanScan.txt"))) -Encoding Byte

Here's the source code:

#include <tchar.h>
#include <Windows.h>

#ifndef __CRT_STRINGIZE
#define __CRT_STRINGIZE(Value) #Value
#endif
#ifndef _CRT_STRINGIZE
#define _CRT_STRINGIZE(Value) __CRT_STRINGIZE(Value)
#endif

enum { WLAN_NOTIFICATION_SOURCE_ACM = 0x00000008 };
typedef enum _WLAN_NOTIFICATION_ACM { wlan_notification_acm_start, wlan_notification_acm_autoconf_enabled, wlan_notification_acm_autoconf_disabled, wlan_notification_acm_background_scan_enabled, wlan_notification_acm_background_scan_disabled, wlan_notification_acm_bss_type_change, wlan_notification_acm_power_setting_change, wlan_notification_acm_scan_complete, wlan_notification_acm_scan_fail, wlan_notification_acm_connection_start, wlan_notification_acm_connection_complete, wlan_notification_acm_connection_attempt_fail, wlan_notification_acm_filter_list_change, wlan_notification_acm_interface_arrival, wlan_notification_acm_interface_removal, wlan_notification_acm_profile_change, wlan_notification_acm_profile_name_change, wlan_notification_acm_profiles_exhausted, wlan_notification_acm_network_not_available, wlan_notification_acm_network_available, wlan_notification_acm_disconnecting, wlan_notification_acm_disconnected, wlan_notification_acm_adhoc_network_state_change, wlan_notification_acm_profile_unblocked, wlan_notification_acm_screen_power_change, wlan_notification_acm_profile_blocked, wlan_notification_acm_scan_list_refresh, wlan_notification_acm_end } WLAN_NOTIFICATION_ACM, *PWLAN_NOTIFICATION_ACM;
typedef enum _WLAN_INTERFACE_STATE { wlan_interface_state_not_ready, wlan_interface_state_connected, wlan_interface_state_ad_hoc_network_formed, wlan_interface_state_disconnecting, wlan_interface_state_disconnected, wlan_interface_state_associating, wlan_interface_state_discovering, wlan_interface_state_authenticating } WLAN_INTERFACE_STATE, *PWLAN_INTERFACE_STATE;
typedef struct _WLAN_INTERFACE_INFO { GUID InterfaceGuid; WCHAR strInterfaceDescription[256]; WLAN_INTERFACE_STATE isState; } WLAN_INTERFACE_INFO;
typedef struct _WLAN_INTERFACE_INFO_LIST { DWORD dwNumberOfItems; DWORD dwIndex; WLAN_INTERFACE_INFO InterfaceInfo[1]; } WLAN_INTERFACE_INFO_LIST;
typedef struct _WLAN_NOTIFICATION_DATA { DWORD NotificationSource; DWORD NotificationCode; GUID InterfaceGuid; DWORD dwDataSize; void *pData; } WLAN_NOTIFICATION_DATA, *PWLAN_NOTIFICATION_DATA;
typedef void WINAPI WLAN_NOTIFICATION_CALLBACK(WLAN_NOTIFICATION_DATA *, void *);

typedef struct wlan_scan_finished_context { WLAN_INTERFACE_INFO_LIST *interface_list; HANDLE semaphore; } wlan_scan_finished_context;
static void WINAPI wlan_notification_callback(WLAN_NOTIFICATION_DATA *data, void *context)
{
    if ((data->NotificationSource | WLAN_NOTIFICATION_SOURCE_ACM) == WLAN_NOTIFICATION_SOURCE_ACM)
    {
        if (data->NotificationCode == wlan_notification_acm_power_setting_change || data->NotificationCode == wlan_notification_acm_scan_complete)
        {
            wlan_scan_finished_context *const ctx = (wlan_scan_finished_context *)context;
            if (ctx)
            {
                for (unsigned int i = 0; i != (ctx->interface_list ? ctx->interface_list->dwNumberOfItems : 0); ++i)
                {
                    if (memcmp(&ctx->interface_list->InterfaceInfo[i].InterfaceGuid, &data->InterfaceGuid, sizeof(data->InterfaceGuid)) == 0)
                    {
                        ctx->interface_list->InterfaceInfo[i].isState = (WLAN_INTERFACE_STATE)(ctx->interface_list->InterfaceInfo[i].isState | (data->NotificationCode << 16));
                    }
                }
                if (ctx->semaphore) { long prev; ReleaseSemaphore(ctx->semaphore, 1, &prev); }
            }
        }
    }
}

int _tmain(int argc, TCHAR *argv[])
{
    (void)argv;
    unsigned int result;
    if (argc > 1) { result = ERROR_INVALID_PARAMETER; }
    else
    {
        HMODULE const wlanapi = LoadLibrary(TEXT("wlanapi.dll"));
        if (wlanapi)
        {
#define X(Module, Return, Name, Params) typedef Return Name##_t Params; Name##_t *Name = (Name##_t *)GetProcAddress(Module, __CRT_STRINGIZE(Name));
            X(wlanapi, DWORD WINAPI, WlanCloseHandle, (HANDLE hClientHandle, void *pReserved));
            X(wlanapi, DWORD WINAPI, WlanEnumInterfaces, (HANDLE hClientHandle, void *pReserved, struct _WLAN_INTERFACE_INFO_LIST **ppInterfaceList));
            X(wlanapi, void WINAPI, WlanFreeMemory, (void *pMemory));
            X(wlanapi, DWORD WINAPI, WlanOpenHandle, (DWORD dwClientVersion, void *pReserved, DWORD *pdwNegotiatedVersion, HANDLE *phClientHandle));
            X(wlanapi, DWORD WINAPI, WlanRegisterNotification, (HANDLE hClientHandle, DWORD dwNotifSource, BOOL bIgnoreDuplicate, WLAN_NOTIFICATION_CALLBACK funcCallback, void *pCallbackContext, void *pReserved, DWORD *pdwPrevNotifSource));
            X(wlanapi, DWORD WINAPI, WlanScan, (HANDLE hClientHandle, const GUID *pInterfaceGuid, const struct _DOT11_SSID *pDot11Ssid, const struct _WLAN_RAW_DATA *pIeData, void *pReserved));
#undef X
            DWORD version;
            HANDLE handle = NULL;
            result = WlanOpenHandle ? WlanOpenHandle(1, NULL, &version, &handle) : ERROR_PROC_NOT_FOUND;
            if (result == ERROR_SUCCESS)
            {
                WLAN_INTERFACE_INFO_LIST *interface_list = NULL;
                result = WlanEnumInterfaces ? WlanEnumInterfaces(handle, NULL, &interface_list) : ERROR_PROC_NOT_FOUND;
                if (result == ERROR_SUCCESS)
                {
                    wlan_scan_finished_context context = { interface_list, CreateSemaphore(NULL, 0, (LONG)interface_list->dwNumberOfItems, NULL) };
                    DWORD prev;
                    unsigned int nwait = 0;
                    unsigned long const register_notification_result = WlanRegisterNotification ? WlanRegisterNotification(handle, WLAN_NOTIFICATION_SOURCE_ACM, FALSE, wlan_notification_callback, &context, NULL, &prev) : ERROR_PROC_NOT_FOUND;
                    for (unsigned int i = 0; i != interface_list->dwNumberOfItems; ++i)
                    {
                        unsigned int const result_i = WlanScan ? WlanScan(handle, &interface_list->InterfaceInfo[i].InterfaceGuid, NULL, NULL, NULL) : ERROR_PROC_NOT_FOUND;
                        interface_list->InterfaceInfo[i].isState = (WLAN_INTERFACE_STATE)(interface_list->InterfaceInfo[i].isState & 0xFFFF);
                        if (result_i == ERROR_SUCCESS) { ++nwait; }
                        else { interface_list->InterfaceInfo[i].isState = (WLAN_INTERFACE_STATE)(interface_list->InterfaceInfo[i].isState | (result_i << 16)); }
                    }
                    while (nwait > 0)
                    {
                        WaitForSingleObject(context.semaphore, INFINITE);
                        --nwait;
                    }
                    for (unsigned int i = 0; i != interface_list->dwNumberOfItems; ++i)
                    {
                        unsigned int const result_i = interface_list->InterfaceInfo[i].isState >> 16;
                        if (register_notification_result == ERROR_SUCCESS && result_i != ERROR_SUCCESS)
                        { result = result_i; }
                    }
                    if (result == ERROR_SUCCESS && register_notification_result != ERROR_SUCCESS) { result = ERROR_IO_PENDING; }
                    if (context.semaphore) { CloseHandle(context.semaphore); }
                    WlanFreeMemory ? WlanFreeMemory(interface_list) : ERROR_PROC_NOT_FOUND;
                }
                WlanCloseHandle ? WlanCloseHandle(handle, NULL) : ERROR_PROC_NOT_FOUND;
            }
        }
        else { result = (unsigned int)GetLastError(); }
    }
    return (int)result;
}
8
  • 1
    Can't believe no one had even upvoted this answer! Yes it requires a C++ compiler, like Visual Studio 2015's free Community Edition. But it compiled without a hitch and does exactly what the OP asked for. Well done Mehrdad! Jun 2, 2019 at 17:58
  • 2
    @GabeHalsmer: Thanks! It does exactly what the OP asked because... I'm the OP ;) I only posted this 2-3 weeks ago so I guess nobody's seen it yet.
    – user541686
    Jun 2, 2019 at 20:45
  • I see that you attached the Base64 encoding of the binary code, may I ask you how to create the executable file on Windows by saving this Base64 code? Can I do it from terminal, for example using the PowerShell? Or do you suggest some special editor to do this? Aug 26, 2019 at 17:48
  • 1
    @Kubuntuer82: From within PowerShell, you can do Set-Content -Path "WlanScan.exe" $([Convert]::FromBase64String($(Get-Content -Path "WlanScan.txt"))) -Encoding Byte. From outside PowerShell, you can also use piping to call PowerShell if that's easier: powershell -command "$b = [Convert]::FromBase64String([Console]::In.ReadToEnd()); $s = [Console]::OpenStandardOutput(); try { $s.Write($b, 0, $b.Length); } finally { $s.Close(); }" < WlanScan.txt > WLANScan.exe. I'll add it to the answer.
    – user541686
    Aug 26, 2019 at 20:19
  • 1
    on my system (windows 10 education 1909) the executable returns nothing.
    – Zibri
    Feb 18, 2020 at 16:01
5

I know I am a little late to the party, but another way of doing this is to restart the wlansvc service.

Get-Service wlansvc | Restart-Service

Unfortunately, this can only be done from an elevated prompt though.

1
  • no code/binary download needed. I did however need to add -Force to the end of the command as I was getting an error that there were conflicting "dependent services" (unimportant in my case)
    – t.j.
    Jul 14, 2021 at 4:59
4

The networks are updated only when a scan is completed by your WiFi card. The NETSH command does not request a scan; it only displays the cached results of the last scan.

Opening the Windows network list from the taskbar updates the results because the taskbar network tool happens to request a scan when it's opened.

There is no NETSH command to request a scan like this. You would have to write some code, leveraging either the Win32 WlanScan function (C# wrappers exist if you prefer that), or the WinRT ScanAsync function.

(Adapted/updated from the source.)


Related:

1

I'm currently running Windows 10 Home version 20H2, and I was able to force a rescan by opening the available networks list from the taskbar using this command:

explorer.exe ms-availablenetworks:

I have a batch that connects to a specific network; but sometimes, a rescan is required to cause that network to show up as an available network. In the past when a rescan was necessary, my batch would fail to connect to the network. I could manually open the available networks list and then execute the batch with success.

Now with the command above included, my batch will open the available networks list thereby forcing a rescan before attempting the connection. I haven't tested it extensively. I imagine a wait after opening the list but before attempting the connection may be necessary.

I found this command here.

0

Extending this answer, here's a batch script to automate:

@ECHO OFF
for /f "tokens=*" %%a in ('netsh wlan show networks ^| grep -oP "Interface\s+name\s*:\s*\K(.*)"') do set "interface_name=%%a"
netsh interface set interface name=%interface_name% admin=disabled
netsh interface set interface name=%interface_name% admin=enabled
netsh wlan show networks

make sure to run it as administrator.

0
0

I took the code from user541686's answer and put it into a small python script with DLL's. You can get it here and run python scan.py -once -logger-level=quiet to force a re-scan :).

EDIT

I didn't realize that there's already a python package that does this exact thing...

Apparently with winwifi you can just do wifi scan and it will do a force re-scan and then call netsh

0

Another method is to use a utility like nirsoft WifiInfoView as follows:

WifiInfoView.exe /scomma nul

Or in WSL:

WifiInfoView.exe /scomma /dev/null

If you have access to the GUI, you can use the ms-availablenetworks: shortcut previously mentioned, and close it immediately with:

explorer.exe ms-availablenetworks:; explorer.exe ms-clock:; taskkill.exe /f /im Time.exe

This will open and close the Clock app in order to close the network list.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .