win10无网络通过PowerShell脚本强制开启移动热点

前言

win10在使用宽带连接拨号上网时,移动热点功能是灰色的,无法正常开启热点分享网络。主要有两种方法可以开启热点。

  1. 电脑先连接上一个WiFi,开启移动热点,接着断开WiFi,移动热点还是打开状态;win+R 输入 ncpa.cpl 打开 “网络连接” ,右键单击拨号上网的那个连接(现在应该只有这个有网络),选择 “属性” ,选择 “共享” ,勾选 “允许其它网络用户通过此计算机的 Internet 连接来连接(N)” ,下方的 “家庭网络连接(H)” 下拉列表中选择之前打开移动热点时新出现的连接,通常来说是 “本地连接* 一个数字” 的名字格式(不一定是这个,看一下开启热点新增的那个连接名字最靠谱),点击 “确定” ,大功告成!现在尝试用手机连上热点就能上网了。
  2. 使用第三方软件。比如:MyPublicWiFi (https://mypublicwifi.com/publicwifi/ch/index.html) 。安装后,先改成中文界面;在设置中将 “首选 WiFi 技术” 改为 “Moblie HotSpot” (不改的话可能热点无法正常上网),保存更改;首页开启热点就行了。

在使用第三方软件时,可能会影响网络,所以我还是改为了使用第一种不安装第三方软件的方法。但是,需要手机先开启热点仍然是一个头疼的步骤。所以,我借助了 AI 写了个 PowerShell 脚本来实现:在无网络的情况下,强制开启移动热点。

使用方法

脚本如下:

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
137
138
139
140
141
142
143
144
145
146
147
148
149
# PowerShell 脚本:强制激活Windows系统移动热点
# 此脚本用于在Windows 10/11系统中强制启用移动热点功能
# 创建初衷:使用拨号上网无法开热点(开关灰色),先连上wifi打开热点,再断开wifi,将拨号上网(宽带连接)共享到热点适配器即可实现。用此脚本可省略一些步骤。

# 加载必要的Windows运行时类型定义
# 第一行定义网络运营商的热点管理器类型
# [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager] 是热点管理的主要类
# WindowsRuntime表示这是Windows运行时API,不是标准的.NET类
$tetheringManagerType = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager, Windows.Networking.NetworkOperators, ContentType=WindowsRuntime]

# 第二行定义网络连接信息类型
# 用于获取当前的网络连接配置信息
$networkInfoType = [Windows.Networking.Connectivity.NetworkInformation, Windows.Networking.Connectivity, ContentType=WindowsRuntime]

# 显示开始执行信息,使用青色文本
Write-Host "正在强制激活热点服务..." -ForegroundColor Cyan
Write-Host "" # 空行,使输出更清晰

# ==================== 第一步:获取基础网络配置 ====================
Write-Host "步骤1: 获取当前网络连接配置..." -ForegroundColor Gray

# 获取当前计算机的所有网络连接配置
$profiles = $networkInfoType::GetConnectionProfiles()

# 优先选择Wi-Fi连接配置:
# 1. 从所有配置中筛选出Wi-Fi连接配置(IsWlanConnectionProfile为true的配置)
# 2. 选择第一个找到的Wi-Fi配置
$profile = $profiles | Where-Object { $_.IsWlanConnectionProfile } | Select-Object -First 1

# 如果没有找到Wi-Fi配置,则使用第一个可用的连接配置(可能是有线连接)
if ($null -eq $profile) {
Write-Host "未找到Wi-Fi连接,将使用第一个可用连接..." -ForegroundColor Yellow
$profile = $profiles[0]
}

Write-Host "已选择连接配置" -ForegroundColor Gray

# ==================== 第二步:启动移动热点 ====================
Write-Host ""
Write-Host "步骤2: 创建热点管理器并启动热点..." -ForegroundColor Gray

# 使用选定的连接配置创建热点管理器实例
# 这个管理器将控制热点的开启、关闭和配置
$tetheringManager = $tetheringManagerType::CreateFromConnectionProfile($profile)

# 异步启动移动热点
# StartTetheringAsync() 是异步方法,会立即开始启动过程
# $null = 将返回值赋值给空变量,避免返回的ComObject对象导致错误显示
$null = $tetheringManager.StartTetheringAsync()

Write-Host "指令已发送,正在检测硬件响应..." -ForegroundColor Yellow
Write-Host "" # 空行

# ==================== 第三步:检测热点是否成功启动 ====================
Write-Host "步骤3: 检测热点启动状态..." -ForegroundColor Gray
Write-Host "正在等待网络适配器响应" -ForegroundColor Gray

# 初始化成功标志
$isSuccess = $false

# 循环检测最多10秒,每秒检查一次
# 热点启动需要时间,Windows需要创建虚拟网络适配器
for ($i = 1; $i -le 10; $i++) {
# 显示进度点,-NoNewline参数表示不换行
Write-Host "." -NoNewline

# 等待1秒,给系统时间创建适配器
Start-Sleep -Seconds 1

# 检测是否存在热点对应的虚拟网络适配器
# Get-NetAdapter 获取所有网络适配器
# Where-Object 过滤条件:
# 1. 适配器描述包含"Wi-Fi Direct Virtual"(热点虚拟适配器的特征)
# 2. 适配器状态为"Up"(已启用并运行中)
$virtualAdapter = Get-NetAdapter | Where-Object {
$_.InterfaceDescription -like "*Wi-Fi Direct Virtual*" -and $_.Status -eq "Up"
}

# 如果找到运行中的热点虚拟适配器,表示热点启动成功
if ($virtualAdapter) {
$isSuccess = $true
Write-Host " 找到适配器!" -ForegroundColor Green
break # 跳出循环
}
}

# 循环结束后换行
Write-Host ""

# ==================== 第四步:显示最终结果 ====================
if ($isSuccess) {
# 成功情况:显示绿色成功信息
Write-Host "====================================" -ForegroundColor Green
Write-Host "判断结果:热点启动成功!" -ForegroundColor Green
Write-Host "检测到活动适配器: $($virtualAdapter.Name)" -ForegroundColor Gray
Write-Host "====================================" -ForegroundColor Green
Write-Host "" # 空行

# 重要提示:需要手动配置网络共享
Write-Host "重要提示:" -ForegroundColor Yellow
Write-Host "热点已创建,但还需要配置网络共享才能让其他设备上网:" -ForegroundColor Cyan
Write-Host "" # 空行
Write-Host "操作步骤:" -ForegroundColor White
Write-Host "1. 按 Win+R 打开运行对话框" -ForegroundColor Gray
Write-Host "2. 输入 'ncpa.cpl' 并按回车打开网络连接" -ForegroundColor Gray
Write-Host "3. 找到您上网的宽带连接(如以太网)" -ForegroundColor Gray
Write-Host "4. 右键 -> 属性 -> 共享选项卡" -ForegroundColor Gray
Write-Host "5. 勾选'允许其他网络用户...'" -ForegroundColor Gray
Write-Host "6. 选择 '$($virtualAdapter.Name)' 这个适配器" -ForegroundColor Gray
Write-Host "7. 点击确定保存设置" -ForegroundColor Gray
} else {
# 失败情况:显示红色错误信息
Write-Host "====================================" -ForegroundColor Red
Write-Host "判断结果:热点启动可能失败。" -ForegroundColor Red
Write-Host "====================================" -ForegroundColor Red
Write-Host "" # 空行

# 提供可能的故障排除建议
Write-Host "可能的原因和解决方法:" -ForegroundColor Yellow
Write-Host "1. 请确保计算机的 Wi-Fi 功能已手动打开" -ForegroundColor Gray
Write-Host "2. 检查是否有其他程序占用了无线网卡(如虚拟机软件)" -ForegroundColor Gray
Write-Host "3. 尝试在系统设置中手动开启移动热点" -ForegroundColor Gray
Write-Host "4. 重启计算机后重试" -ForegroundColor Gray
Write-Host "5. 确保无线网卡驱动是最新版" -ForegroundColor Gray
Write-Host "" # 空行
Write-Host "注意:即使这里显示失败,热点可能仍在后台启动中" -ForegroundColor Magenta
Write-Host "可以查看系统托盘或系统设置确认热点状态" -ForegroundColor Magenta
}

Write-Host "" # 空行
Write-Host "脚本执行完成" -ForegroundColor Cyan

# 暂停脚本,让用户有时间阅读输出信息
# 按任意键继续后,窗口会保持打开
pause

# 原本的,可删除
# if ($isSuccess) {
# Write-Host "====================================" -ForegroundColor Green
# Write-Host "判断结果:热点启动成功!" -ForegroundColor Green
# Write-Host "检测到活动适配器: $($virtualAdapter.Name)" -ForegroundColor Gray
# Write-Host "====================================" -ForegroundColor Green
# Write-Host "请现在去 'ncpa.cpl' 设置宽带连接的‘共享’到该适配器。" -ForegroundColor Cyan
# } else {
# Write-Host "====================================" -ForegroundColor Red
# Write-Host "判断结果:启动可能失败。" -ForegroundColor Red
# Write-Host "请确保 Wi-Fi 已手动打开,且没有其他程序占用网卡。" -ForegroundColor Yellow
# Write-Host "====================================" -ForegroundColor Red
# }

先新建一个 txt 文件,命名为:~无网络强制启动热点 (名字随意),记事本打开,将上述代码复制进去,保存,在记事本菜单栏的 “文件” 里选择 “另存为…” ,编码 改成 “ANSI” (确保中文不乱码),保存。重命名该文件后缀,由 ~无网络强制启动热点.txt 改为 ~无网络强制启动热点.ps1 。右键该文件,选择 “使用 PowerShell 运行” 即可。

如果出现报错,建议将报错信息复制,询问AI。

附加

  • 如果使用第三方软件后,没有正常退出或者其它原因导致网络不正常,可以尝试在 “网络连接” 中右键 “以太网” -> “属性” -> “网络” -> “此连接使用下列项目” -> “Internet 协议版本 4 (TCP/IPv4)” -> “常规” -> 勾选 “自动获得 IP 地址” 。(此方法不一定有效,如果不行建议询问AI或网络搜索解决办法)