PowerShell是Windows下批量管理网络适配器最可靠灵活的方式,支持物理网卡筛选、IP/DNS批量配置、跃点数优先级调整及安全执行留痕。
powershell 是 windows 下批量管理网络适配器最可靠、最灵活的方式,不依赖第三方工具,原生支持、可验证、可回滚。关键在于用对命令、选准目标适配器、控制执行顺序。
精准识别并筛选目标适配器
批量操作前必须准确锁定要处理的网卡,避免误改虚拟网卡或蓝牙接口:
- 用 Get-NetAdapter -Physical 获取真实物理网卡,自动排除 Hyper-V、VMware vEthernet、Bluetooth PAN 等虚拟接口
- 按状态筛选活跃网卡:Get-NetAdapter | Where-Object {$_.Status -eq "Up"}
- 按名称关键词匹配(如“以太网”“Wi-Fi”):Get-NetAdapter | Where-Object {$_.Name -like "以太网*"}
- 导出清单便于核对:Get-NetAdapter | Select-Object Name, InterfaceDescription, Status | Export-Csv .\adapter-list.csv -NoTypeInformation
批量设置IP与DNS(静态/动态切换)
一条命令完成 DHCP 关闭、IP 地址添加、网关设定和 DNS 配置,适合多台设备统一部署:
直播话术巡检系统(Windows版) — 实时语音转写、关键词检测、场景自动切换、告警推送、大屏可视化。支持 Windows VB-Cable 虚拟声卡音频采集,阿里云 ASR 实时识别。一键启动脚本,自动安装环境。
- 先禁用 DHCP:Set-NetIPInterface -InterfaceAlias "以太网" -Dhcp Disabled
- 再配置 IPv4 地址:New-NetIPAddress -InterfaceAlias "以太网" -IPAddress 192.168.10.50 -PrefixLength 24 -DefaultGateway 192.168.10.1
- 最后设 DNS:Set-DnsClientServerAddress -InterfaceAlias "以太网" -ServerAddresses 192.168.10.1,8.8.8.8
- 批量应用到所有匹配网卡:把上面三步封装进 ForEach-Object 循环,例如对所有“以太网*”适配器执行
统一调整网络优先级(跃点数)
当电脑连着 Wi-Fi 和有线网时,系统默认可能优先走无线;用跃点数(InterfaceMetric)强制指定路由优先级:
- 查看当前跃点值:Get-NetIPInterface | Where-Object {$_.AddressFamily -eq "IPv4"} | Format-Table IfIndex, InterfaceAlias, InterfaceMetric
- 将以太网设为高优(数值越小越优先):Set-NetIPInterface -InterfaceIndex 12 -InterfaceMetric 10
- 将 Wi-Fi 设为次优:Set-NetIPInterface -InterfaceIndex 5 -InterfaceMetric 30
- 恢复自动跃点(交还系统决策权):Set-NetIPInterface -InterfaceIndex 12 -AutomaticMetric Enabled
安全执行与操作留痕
生产环境操作需防误、可追溯、能回退:
- 脚本开头加入提权检查:if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Administrators")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
- 执行前备份当前配置:Export-NetIPConfiguration -Path "backup_$(Get-Date -Format 'yyyyMMdd_HHmm').xml"
- 每步操作后加日志:"$(Get-Date): 已为 $($adapter.Name) 设置静态IP" | Out-File -Append .\net-config-log.txt
- 错误处理建议加 -ErrorAction Stop 并配合 try/catch,确保某张网卡失败不影响整体流程








