【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
相关推荐
疋瓞27 分钟前
批处理_基础补充、文件和文件夹处理_02
windows
学编程就要猛1 小时前
JavaEE初阶:网络原理-HTTP(下)
网络·网络协议·http
xiaohe071 小时前
C#数据库操作系列---SqlSugar完结篇
网络·数据库·c#
pc07931 小时前
带网络接口的打印机和USB接口打印机的共享区别
运维·服务器·网络
汤愈韬1 小时前
Full Cone NAT、行为模式
网络·网络协议·网络安全·security
Agent手记1 小时前
首件检验流程繁琐,耗时久还容易出现合规漏洞怎么办?——基于实在Agent的AI+超自动化全流程闭环实战
网络·人工智能·ai·自动化
nudt_qxx1 小时前
Ubuntu 24.04/26.04 与 Windows 10/11 双系统时间不同步终极解决方案
windows·stm32·ubuntu
达不溜的日记1 小时前
PDUR路由基本功能
网络·stm32·单片机·嵌入式硬件·mcu·51单片机·信息与通信
钱塘江渔夫1 小时前
一键式解决Windows访问github.com不了问题
windows·github
蓝天居士2 小时前
Linux网络驱动之Fixed-Link(29)
linux·运维·网络