1 如何设置Windows11开机自启动移动热点功能
1.1 需求简述
- 我的平板不能装电话卡,需要连接电脑WiFi,但每次笔记本的热点都得手动打开,很不方便,遂实现此功能。
1.2 实现思路
- 写一个
shell
脚本实现启动热点的功能;
- 写一个
bat
脚本调用shell
脚本,放入自启动文件夹C:\Users\<你的用户名>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
中即可。
1.3 提前准备
- 按下键盘快捷键
Win + X + A
,打开终端管理员,复制下面的命令,粘贴并按下回车键,确保你的PowerShell 可以运行本地创建的脚本文件

1
| Set-Executionpolicy RemoteSigned
|
1.4 创建shell脚本
- 在任意路径下,创建一个txt文档,将下面的内容粘贴进去,并将文件的后缀修改为
.ps1
。【我创建在”D:\Documents”路径下】 【🤔,这一步你需要会修改文件后缀,你会的,对吧~】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.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() | Where-Object { $_.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) { } else { Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult]) } } else { if ($tetheringManager.TetheringOperationalState -eq 1) { Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult]) } else { } } }
Function Check_HotspotStatus() { $tetheringManager = Get_TetheringManager return $tetheringManager.TetheringOperationalState -eq "Off" }
Function Start_Hotspot() { $tetheringManager = Get_TetheringManager Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult]) }
if ($args.Length -eq 0) { while (Check_HotspotStatus) { SetHotspot 1 Start-Sleep -Seconds 2 if (Check_HotspotStatus) { "Failure.Try again in 2s." Start-Sleep -Seconds 2 continue } else { exit 0 } }
}
else { switch ($args[0]) { "0" { SetHotspot 0 break } "1" { SetHotspot 1 break } default { "Invalid parameter, please enter 1 to turn on hotspot, enter 0 to turn off hotspot" exit 1 } } }}
|
1.5 创建bat脚本
win + R
打开命令运行窗口,复制下面的命令并粘贴

- 此时会自动跳转到自启动文件夹,在此文件夹下创建一个txt文件,将下面的命令粘贴进去,并将文件后缀修改为
.bat
- 注意,应将文件路径
"D:\Documents\autohotspot.ps1"
替换为你的shell
脚本所在的路径。

1
| PowerShell.exe -WindowStyle Hidden -File "D:\Documents\autohotspot.ps1"
|
- 可自行关闭电脑热点,双击bat脚本运行,进行测试。
- 至此,大工告成。
1.6 参考


沧浪同学
沧浪の小窝
此文章版权归沧浪同学所有,如有转载,请注明来自原作者