【PowerShell】修改Windows网络配置的常用命令

PowerShell(PS)是一种强大的任务自动化和管理框架,具有丰富的命令和语法,可以用于编写脚本来管理Windows操作系统和其他应用程序。它的开放式架构和跨平台支持使得它成为一个灵活和可扩展的工具。

在网络配置方面,PowerShell提供了丰富的命令用于快速进行Windows配置,这里记录几种网络配置常用操作的PS命令,这些命令在Windows11上验证可正确执行,更多命令可从官方文档找到:

Get-NetAdapterAdvancedProperty

Get-NetAdapterAdvancedProperty用于获取网络适配器的高级属性,该命令可用于修改网络适配器的各种高级设置,例如Wake-on-LAN、Flow Control、Interrupt Moderation等。该命令与Set-NetAdapterAdvancedProperty相对应。

复制代码
Get-NetAdapterAdvancedProperty
   [[-Name] <String[]>]
   [-IncludeHidden]
   [-AllProperties]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [<CommonParameters>]

例如,如下命令用于获取以太网的全部高级属性

powershell 复制代码
Get-NetAdapterAdvancedProperty -Name Ethernet -all

使用-DisplayName或者-RegistryKeyword用于获取某个高级属性的值。如下述命令均可用于获取链路速率。

powershell 复制代码
Get-NetAdapterAdvancedProperty -Name Ethernet -RegistryKeyword "*SpeedDuplex"
Get-NetAdapterAdvancedProperty -Name Ethernet -DisplayName "Speed & Duplex"

Set-NetAdapterAdvancedProperty

Set-NetAdapterAdvancedProperty用于配置网络适配器的高级属性,与Get-NetAdapterAdvancedProperty相对应。其格式如下:

复制代码
 Set-NetAdapterAdvancedProperty
   [[-Name] <String[]>]
   [-DisplayName <String[]>]
   [-RegistryKeyword <String[]>]
   [-IncludeHidden]
   [-AllProperties]
   [-DisplayValue <String>]
   [-RegistryValue <String[]>]
   [-NoRestart]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

例如,如下命令用于设置以太网链路速率为100Mbps全双工(100Mbps全双工对应的配置值为4)

powershell 复制代码
Set-NetAdapterAdvancedProperty -Name "Ethernet" -RegistryKeyword "*SpeedDuplex" -RegistryValue "4"
Set-NetAdapterAdvancedProperty -Name "Ethernet" -DisplayName "Speed & Duplex" -DisplayValue "100 Mbps Full Duplex"

Remove-NetIPAddress

Remove-NetIPAddress用于删除一个网络适配器的IP地址配置,在给某个已经分配过IP地址的网络适配器更换IP时需要首先执行这个命令。该命令与New-NetIPAddress对应。

该命令完整格式如下:

复制代码
Remove-NetIPAddress
      [[-IPAddress] <String[]>]
      [-InterfaceIndex <UInt32[]>]
      [-InterfaceAlias <String[]>]
      [-AddressFamily <AddressFamily[]>]
      [-Type <Type[]>]
      [-PrefixLength <Byte[]>]
      [-PrefixOrigin <PrefixOrigin[]>]
      [-SuffixOrigin <SuffixOrigin[]>]
      [-AddressState <AddressState[]>]
      [-ValidLifetime <TimeSpan[]>]
      [-PreferredLifetime <TimeSpan[]>]
      [-SkipAsSource <Boolean[]>]
      [-PolicyStore <String>]
      [-DefaultGateway <String>]
      [-IncludeAllCompartments]
      [-CimSession <CimSession[]>]
      [-ThrottleLimit <Int32>]
      [-AsJob]
      [-PassThru]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]

例如,如下命令用于清除以太网的IP配置,-Confirm:$False参数表示这个命令执行时不需要用户手动确认。

powershell 复制代码
Remove-NetIPAddress -InterfaceAlias "Ethernet" -Confirm:$False

New-NetIPAddress

New-NetIPAddress用于为未分配IP地址的网络接口创建新的IP地址配置。该命令与Remove-NetIPAddress对应。

该命令完整格式如下:

复制代码
New-NetIPAddress
   [-IPAddress] <String>
   -InterfaceAlias <String>
   [-DefaultGateway <String>]
   [-AddressFamily <AddressFamily>]
   [-Type <Type>]
   [-PrefixLength <Byte>]
   [-ValidLifetime <TimeSpan>]
   [-PreferredLifetime <TimeSpan>]
   [-SkipAsSource <Boolean>]
   [-PolicyStore <String>]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

例如下述命令为以太网分配静态IP 192.167.99.11

powershell 复制代码
New-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress 192.167.99.11 -PrefixLength 24
相关推荐
阿白的白日梦11 小时前
winget基础管理---更新/修改源为国内源
windows
埃博拉酱4 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21885 天前
.NET 本地Db数据库-技术方案选型
windows·c#
DianSan_ERP5 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
tryCbest5 天前
Windows环境下配置pip镜像源
windows·pip
呉師傅5 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
百事牛科技5 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
一个人旅程~5 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑
2501_946205525 天前
晶圆机器人双臂怎么选型?适配2-12寸晶圆的末端效应器有哪些?
服务器·网络·机器人