Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active February 28, 2025 03:03
Show Gist options
  • Save primaryobjects/8b54f7f4219960127f1f620116315a37 to your computer and use it in GitHub Desktop.
Save primaryobjects/8b54f7f4219960127f1f620116315a37 to your computer and use it in GitHub Desktop.
Script to Enable Windows 10 Mobile Hotspot Automatically After Reboot

Enable Windows 10 Mobile Hotspot Automatically After Reboot

❤️ Sponsor This Project

On Windows 10, the Mobile Hotspot feature is automatically disabled when rebooting the machine. Users are required to manually open the Mobile Hotspot settings and toggle the slider for "Share my Internet connection with other devices" in order to enable it.

The included PowerShell script can be added to the Windows Task Scheduler to automatically turn on your Windows 10 Mobile Hotspot upon reboot, login, and unlock of the workstation by any user.

Quick Start

  1. Copy the two script files to a folder on your computer: hotspot.ps1 and hotspot.bat
  2. Open the Windows Task Scheduler.
  3. Right-click on Task Scheduler Library and select Create Task.
    • Enter a Name and Description.
    • Select Run whether user is logged on or not.
    • Checkmark Run with highest privileges.
  4. Click the Triggers tab.
  5. Click New.
    • For Begin the task select At startup.
    • Checkmark Delay task for: 1 minute.
    • Checkmark Stop task if it runs longer than: 30 minutes.
    • Checkmark Enabled.
  6. Click the Conditions tab.
  7. Uncheck the options Stop if the computer switches to battery power and Start the task only if the computer is on AC power.
  8. Click OK.

When saving the Task Scheduler, enter your username (username, ADUser\username, CORP\username, etc.) and your Windows password.

Troubleshooting Hotspot Not Activating After Sleep/Hibernation

If the hotspot enable task is not running after your PC wakes from sleep/hibernation, you can add a trigger to execute the task as soon as possible after waking. Create an additional trigger with the following steps.

  1. Edit the task and click the Triggers tab.
  2. For Begin the task select On a schedule.
  3. Check the radio option Daily.
  4. Enter the earliest Start Time to run. For example, 8:00 AM EST. This computer does not need to be awake during this time, so it is recommended to make this time earlier than you actually need.
  5. Select Recur every 1 day.
  6. Click OK.
  7. Click the Settings tab.
  8. Checkmark the option Run task as soon as possible after a scheduled start is missed.

Troubleshooting Hotspot Disabling Frequently

See also Windows 10 Mobile Hotspot Keep Alive Script.

If the mobile hotspot is turning itself off at random periods, you can try the following settings:

  1. Disable mobile hotspot power saving by opening the Mobile Hotspot settings and disabling When no devices are connected, automatically turn off mobile hotspot.

  2. Set the PeerlessTimeoutEnabled and PublicConnectionTimeout value to a longer duration. This can be done by setting the registry value HKLM\System\ControlSet001\Services\ICSSVC\Settings\PeerlessTimeoutEnabled to 120 (Hexadecimal) and HKLM\System\ControlSet001\Services\ICSSVC\Settings\PublicConnectionTimeout to 60 (Hexadecimal).

    An example registry script is shown below.

    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\icssvc\Settings]
    "PeerlessTimeoutEnabled"=dword:00000120
    "PublicConnectionTimeout"=dword:00000060
    
  3. Run the script hotspot-keep-alive.ps1.

Running the Task When Connecting to the Internet Network

You may optionally want to add a condition to run the task whenever you connect to the Internet. This may be done by adding a new "Trigger" to the task scheduler. Select On an event, for "Log" select Microsoft-Windows-NetworkProfile/Operational, for Source select NetworkProfile, for Event ID enter 10000 (enter 10001 for network disconnect instead of connect). Checkmark Delay task for and select 30 seconds.

Windows 10 Mobile Hotspot Keep Alive Script

An easy script to keep the Windows 10 mobile hotspot turned on even if it becomes disabled.

Windows 10 Mobile Hotspot Keep Alive Script

# https://superuser.com/a/1434648
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
}
Function AwaitAction($WinRtAction) {
$asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
$netTask = $asTask.Invoke($null, @($WinRtAction))
$netTask.Wait(-1) | Out-Null
}
Function Get_TetheringManager() {
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
return $tetheringManager;
}
Function SetHotspot($Enable) {
$tetheringManager = Get_TetheringManager
if ($Enable -eq 1) {
if ($tetheringManager.TetheringOperationalState -eq 1)
{
"Hotspot is already On!"
}
else{
"Hotspot is off! Turning it on"
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
}
else {
if ($tetheringManager.TetheringOperationalState -eq 0)
{
"Hotspot is already Off!"
}
else{
"Hotspot is on! Turning it off"
Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
}
}
# Define a function to check the status of the hotspot
Function Check_HotspotStatus() {
$tetheringManager = Get_TetheringManager
return $tetheringManager.TetheringOperationalState -eq "Off"
}
# Define a function to start the hotspot
Function Start_Hotspot() {
$tetheringManager = Get_TetheringManager
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
$currentDateTime = Get-Date -Format "MM-dd-yyyy HH:mm:ss"
"$currentDateTime Starting hotspot keep-alive."
# Keep alive wifi.
while ($true) {
# Get the current date and time in a specific format
$currentDateTime = Get-Date -Format "MM-dd-yyyy HH:mm:ss"
if (Check_HotspotStatus) {
"$currentDateTime Hotspot is off! Turning it on"
Start_Hotspot
}
Start-Sleep -Seconds 10 # Wait for 10 seconds before checking again
}
PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1
PowerShell C:\Users\YOUR_USERNAME\Desktop\hotspot.ps1 >> "%TEMP%\StartupLog.txt" 2>&1
# https://superuser.com/a/1434648
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
}
Function AwaitAction($WinRtAction) {
$asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
$netTask = $asTask.Invoke($null, @($WinRtAction))
$netTask.Wait(-1) | Out-Null
}
Function SetHotspot($Enable) {
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
if ($Enable -eq 1) {
if ($tetheringManager.TetheringOperationalState -eq 1)
{
"Hotspot is already On!"
}
else{
"Hotspot is off! Turning it on"
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
}
else {
if ($tetheringManager.TetheringOperationalState -eq 0)
{
"Hotspot is already Off!"
}
else{
"Hotspot is on! Turning it off"
Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}
}
}
Function ToggleHotspot($Delay) {
SetHotspot(1)
sleep -seconds $Delay
SetHotspot(0)
sleep -seconds $Delay
SetHotspot(1)
}
ToggleHotspot(3)
@OP-EX
Copy link

OP-EX commented Dec 7, 2024

I'm trying "hotspot-keep-alive.ps1" script. It seems to work most of the time but I let it running over night and it stopped resuming at about 3:15 am. It was just stuck. Any reason?

https://github.com/OP-EX/Automatically-Keep-Mobile-Hotspot-Alive-Windows10-11

@primaryobjects
Copy link
Author

primaryobjects commented Dec 14, 2024

Some PCs require you to actually disable/re-enable your Wi-Fi and then re-enable the hotspot. I've created the script below to do this automatically.

It uses netsh interface set interface name="Wi-Fi" admin=disable to toggle the Wi-fi adapter before turning back on the hotspot.

Enable the Windows 10 Mobile Hotspot Automatically by Toggle Wi-Fi Adapter

@BigMuscle85
Copy link

I discovered different problem on Windows 10 Enterprise LTSB. Every time the WiFi hotspot is enabled (either manually or using the script), all "Internet Connection Sharing" rules in Windows Firewall are multiplied. After some time, there are so many rules that svchost.exe (LocalServiceNoNetworkFirewall) eats 100% CPU and memory usage is slowly increasing. WiFi hotspot is stuck during this time. If it reaches several gigabytes of RAM, the process is automatically killed which kills the whole system.

@BigMuscle85
Copy link

Some PCs require you to actually disable/re-enable your Wi-Fi and then re-enable the hotspot. I've created the script below to do this automatically.

It uses netsh interface set interface name="Wi-Fi" admin=disable to toggle the Wi-fi adapter before turning back on the hotspot.

Enable the Windows 10 Mobile Hotspot Automatically by Toggle Wi-Fi Adapter

Unfortunately, this does not help. The problem above with svchost.exe can be workarounded by disabling Windows Firewall service only. However, there is still some problem that hotspot gets stuck after some time and "$tetheringManager.TetheringOperationalState" just returns "InTransition" forever. Disabling/re-enabling Wi-Fi does not fix the problem.

@OP-EX
Copy link

OP-EX commented Jan 8, 2025

hotspot gets stuck after some time

what you mean
i think i had the same problem

Hotspot is still visible but it is not possible to connect to it. Opening Windows Hotspot settings dialog freezes this dialog forever. Running this script to disable/re-enable the hotspot does nothing and "TetheringOperationalState" returns "InTransition".
I'm trying to use MyPublicWiFi software now to see how it behaves. Now the hotspot runs without disabling for several hours.

are you using a wifi adapter or the onboard one
what is your end goal

It's onboard Wi-Fi of LattePanda V1 (rtl8723bs). The goal is to have headless measuring system where an operator can connect via Wi-Fi hotspot and control the system with VNC (e.g. using the tablet/mobilephone).

ok
try this out https://github.com/OP-EX/Automatically-Keep-Mobile-Hotspot-Alive-Windows10-11

!!!Note!!!
For the people who are having problems with the code please comment in the repo where the code is located

@Suilui17
Copy link

Thanks for the .ps1 skript.
unfortunally the cmd did not work for me at all.
in the end i just ran the powershell script directly in the tasksceduler by enebaling run with highest privileges and in the actions Tab i searched for the powershell.exe and as an argument i gave hime the file location:
-Command "& 'YOUR_FILE\Location\hotspot.ps1'"

(i also deleted the delay at startup and the termination after 30 min)

@Suilui17
Copy link

I ran the Powershell script on its own and it worked. Then I tried the cmd file (with a new file path) and I ran but nothing else happened.
Hotspot was still of and that's it.
Tested it on win 10😇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment