-
-
Save alan23273850/5ea4e94fba86b7fc1d74cf05d7569e56 to your computer and use it in GitHub Desktop.
Enable Windows 10 & 11 WiFi Hotspot - Powershell Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$intervalSeconds = 5 | |
while ($true) { | |
# 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 | |
$currentState = $tetheringManager.TetheringOperationalState | |
# Start Mobile Hotspot | |
if ($currentState -eq 2) { | |
Await ($tetheringManager.StartTetheringAsync())([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult]) | |
} | |
Start-Sleep -Seconds $intervalSeconds | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've changed the script to restart the Mobile Hotspot every 5 seconds if it is not turned on. It is very helpful for sharing the Ethernet to my smartphone.