Resolved: How to toggle Wi-Fi in Windows 11 via the command line / powershell?

Frodo Yang 0 Reputation points
2024-09-20T06:44:29.57+00:00

{E26D930C-2172-467D-BF10-0BEDFD10B9AA}{E1AD1027-E1E9-437C-A7F4-5A4EEFDE23A3}image-20240920143244198

I need to programmatically control the state of the WLAN button (either turning it on or off). It doesn't matter whether it’s done through PowerShell or any other programming language, as long as it accomplishes the task.

Before asking this question, I already tried using the PowerShell command:

Disable-NetAdapter -Name "Wi-Fi" -Confirm:$false

However, this command makes the WLAN button invisible, essentially disabling the Wi-Fi adapter at the hardware level.

Another solution from Stack Overflow suggests using:

Set-NetAdapterAdvancedProperty -Name "Wi-Fi" -AllProperties -RegistryKeyword "radioEnable" -RegistryValue "0"

or

Set-NetAdapterAdvancedProperty -Name "Wi-Fi" -AllProperties -RegistryKeyword "SoftwareRadioOff" -RegistryValue "1"

Unfortunately, these two commands only work for Windows 10 because the RegistryKeyword entries radioEnable and SoftwareRadioOff have been removed in Windows 11. Here is the Stack Overflow link: https://stackoverflow.com/questions/43486244/how-to-switch-wifi-state-on-off-with-powershell-windows-10

Afterwards, I tried using the software RegShot to compare the changes in the registry before and after turning off the WLAN button. I compiled a list of seemingly relevant changes and attempted to manually modify these entries in the registry, but it didn’t have any effect. Therefore, I suspect that clicking the button calls a function from a Windows DLL. Later, I found WlanRadioManager.dll and tried to decompile it using Ghidra, but my limited knowledge of C++ made it difficult to understand the code logic.

I would greatly appreciate any insights or assistance with this issue. If anyone has experience or suggestions on how to approach this, your help would be invaluable. Thank you in advance for your time and support!

Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
9,724 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Jing Zhou 7,020 Reputation points Microsoft Vendor
    2024-09-23T01:02:28.8066667+00:00

    Hello,

     

    Thank you for posting in Q&A forum.

    To achieve the purpose, please kindly try below steps:

    1.Open CMD window and run below command to disable the WIFI adapter:

    netsh interface set interface name="Wi-Fi" admin=disable

    Meanwhile you can try below comamnd to enable it after:

    netsh interface set interface name="Wi-Fi" admin=enable

     

    I hope the information above is helpful.

    If you have any questions or concerns, please feel free to let us know.

     

    Best regards,

    Jill Zhou

     


    If the Answer is helpful, please click "Accept Answer" and upvote it.


  2. Frodo Yang 0 Reputation points
    2024-09-24T03:24:28.3333333+00:00

    Finally, I found the solution. This Microsoft document: Windows.Devices.Radios Namespace includes sample code for working with the Radio class: Scenario1_Toggle.xaml.cs.

    Specifically, in a UWP application, you can use this code to turn the Wi-Fi radio on or off:

    await Radio.RequestAccessAsync();1
    
    var radios = await Radio.GetRadiosAsync();
    
    Radio wifiRadio = radios.FirstOrDefault(r => r.Kind == RadioKind.WiFi);
    
    await wifiRadio.SetStateAsync(RadioState.On);
    

  3. Frodo Yang 0 Reputation points
    2024-10-03T10:41:37.98+00:00

    I got inspiration from hereand made some modifications. The source code has been placed on my Github repository,

    # Turn off
    PS D:\path\to\your\folder> .\ToggleWifiRadio.ps1 -Status 'Off'
    
    # Turn on
    PS D:\path\to\your\folder> .\ToggleWifiRadio.ps1 -Status 'On'
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.