3

I'm seeking assistance with enabling the Mobile Hotspot feature on Windows 11 through PowerShell. My Wi-Fi adapter does not support the traditional hostednetwork feature used by the netsh wlan commands, as my system relies on Wi-Fi Direct to provide the Mobile Hotspot functionality.

I'm currently using PowerShell 7.4.1 and have not been able to find a cmdlet that offers the ability to control the Mobile Hotspot feature. The Mobile Hotspot I am referring to is the one that can be manually toggled On or Off in the Windows Settings.

Mobile Hotspot Toggle

Is there a way to enable or disable the Mobile Hotspot feature on Windows using PowerShell?

DavidPostill
  • 162,382
Yox
  • 187

1 Answers1

1

A PowerShell cmdlet for enabling/disabling the Mobile Hotspot is found in the github gist datio/enable-wifi.ps1.

The script defines a PowerShell function and contains at its end the two commands. For safety, the script is included below:

# https://stackoverflow.com/questions/45833873/enable-win10-inbuild-hotspot-by-cmd-batch-powershell/60444585#answer-60444585
[Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime] | Out-Null

Add-Type -AssemblyName System.Runtime.WindowsRuntime $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $.Name -eq 'AsTask' -and $.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0] Function Await($WinRtTask, $ResultType) { $asTask = $asTaskGeneric.MakeGenericMethod($ResultType) $netTask = $asTask.Invoke($null, @($WinRtTask)) $netTask.Wait(-1) | Out-Null $netTask.Result }

https://stackoverflow.com/a/55563418

$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile() $tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)

Be sure to include Ben N.'s await for IAsyncOperation:

https://superuser.com/questions/1341997/using-a-uwp-api-namespace-in-powershell

Check whether Mobile Hotspot is enabled

$tetheringManager.TetheringOperationalState

Start Mobile Hotspot

Await ($tetheringManager.StartTetheringAsync())([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])

Stop Mobile Hotspot

#Await ($tetheringManager.StopTetheringAsync())([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])

harrymc
  • 498,455