Netsh 命令语法、上下文和格式

适用于:Windows Server 2022、Windows Server 2019、Windows Server 2016,Azure Stack HCI 版本 21H2 和 20H2

你可以使用本文来了解如何输入 netsh 上下文和子上下文、了解 netsh 语法和命令格式,以及如何在本地和远程计算机上运行 netsh 命令。

Netsh 是命令行脚本实用工具,可让你显示或修改当前正在运行的计算机的网络配置。 可以通过在 netsh shell 下键入命令来运行 Netsh 命令,并且可以在批处理文件或脚本中使用 Netsh 命令。 可以使用 netsh 命令来配置远程计算机和本地计算机。

Netsh 还提供脚本功能,可让你在批处理模式下对指定的计算机运行一组命令。 你可以使用 Netsh 将配置脚本保存在文本文件中,以便存档或者帮助你配置其他计算机。

语法

netsh
netsh [-a AliasFile] [-c Context] [-r RemoteMachine] [-u [DomainName\]UserName] [-p Password | *] [Command | -f ScriptFile]

以下参数被视为可选参数。

参数 说明
-a 指定在运行 AliasFile 后系统会将你返回到 netsh shell。
AliasFile 指定包含一个或多个 netsh 命令的文本文件的名称
-c 指定 netsh 输入指定的 netsh 上下文
Context 指定要输入的 netsh 上下文
-r 指定希望命令在远程计算机上运行。 远程注册表服务必须运行在远程计算机上。 如果未运行,Windows 将显示“找不到网络路径”错误消息。
RemoteComputer 指定要配置的远程计算机。
-u 指定要在用户帐户下运行 netsh 命令。
DomainName\ 指定用户帐户所在的域。 如果未指定 DomainName,则默认值为本地域。
UserName 制定用户帐户名称。
-p 指定要为该用户帐户提供密码。
Password 指定通过 -u UserName 指定的用户帐户的密码。
Command 指定要运行的 netsh 命令
-f 运行用 ScriptFile 指定的脚本之后退出 netsh
ScriptFile 指定要运行的脚本。

注意

如果指定 -r 后跟另一个命令,则 netsh 会在远程计算机上运行该命令,然后返回到命令提示符。 如果你指定 -r 且不带其他命令,则 netsh 在远程模式下打开。 此过程类似于在 Netsh 命令 shell 下使用 set machine。 使用 -r 时,只会为 netsh 的当前实例设置目标计算机。 退出并重新输入 netsh 后,目标计算机将重置为本地计算机。 通过指定存储在 WINS 中的计算机名称、UNC 名称、DNS 服务器要解析的 Internet 名称或 IP 地址,可以在远程计算机上运行 netsh 命令

运行 netsh 命令

要运行 netsh 命令,必须通过键入 netsh,然后按 Enter 从命令提示符或 PowerShell 中启动 netsh。 接下来可以更改为包含要使用的命令的上下文。 可用的上下文取决于已安装的网络组件。

例如,如果在 netsh shell 中键入 dhcp 并按 Enter,则 netsh 将更改为 DHCP 服务器上下文。 如果没有安装 DHCP 服务器角色,则将显示以下消息:

The following command was not found: dhcp.

Netsh 上下文

Netsh 使用动态链接库 (DLL) 文件与其他操作系统组件进行交互。

每个 netsh 帮助程序 DLL 提供一组称为“上下文”的广泛功能,这是一组特定于网络服务器角色或功能的命令。 这些上下文通过为一个或多个服务、实用工具或协议提供配置和监视支持来扩展 netsh 的功能。

例如,Dhcpmon.dll 向 netsh 提供配置和管理 DHCP 服务器所需的上下文和命令集。

可以通过打开命令提示符或 Windows PowerShell 来获取 netsh 上下文列表,然后键入 netsh /?netsh help 并按 Enter。

下面是一个示例输出:

The following commands are available:

Commands in this context:
..            - Goes up one context level.
?             - Displays a list of commands.
abort         - Discards changes made while in offline mode.
add           - Adds a configuration entry to a list of entries.
advfirewall   - Changes to the `netsh advfirewall' context.
alias         - Adds an alias.
branchcache   - Changes to the `netsh branchcache' context.
bridge        - Changes to the `netsh bridge' context.
bye           - Exits the program.
commit        - Commits changes made while in offline mode.
delete        - Deletes a configuration entry from a list of entries.
dhcpclient    - Changes to the `netsh dhcpclient' context.
dnsclient     - Changes to the `netsh dnsclient' context.
dump          - Displays a configuration script.
exec          - Runs a script file.
exit          - Exits the program.
firewall      - Changes to the `netsh firewall' context.
help          - Displays a list of commands.
http          - Changes to the `netsh http' context.
interface     - Changes to the `netsh interface' context.
ipsec         - Changes to the `netsh ipsec' context.
ipsecdosprotection - Changes to the `netsh ipsecdosprotection' context.
lan           - Changes to the `netsh lan' context.
namespace     - Changes to the `netsh namespace' context.
netio         - Changes to the `netsh netio' context.
offline       - Sets the current mode to offline.
online        - Sets the current mode to online.
popd          - Pops a context from the stack.
pushd         - Pushes current context on stack.
quit          - Exits the program.
ras           - Changes to the `netsh ras' context.
rpc           - Changes to the `netsh rpc' context.
set           - Updates configuration settings.
show          - Displays information.
trace         - Changes to the `netsh trace' context.
unalias       - Deletes an alias.
wfp           - Changes to the `netsh wfp' context.
winhttp       - Changes to the `netsh winhttp' context.
winsock       - Changes to the `netsh winsock' context.

The following sub-contexts are available:
advfirewall branchcache bridge dhcpclient dnsclient firewall http interface ipsec ipsecdosprotection lan namespace netio ras rpc trace wfp winhttp winsock

To view help for a command, type the command, followed by a space, and then type ?.

Netsh 子上下文

Netsh 上下文可以同时包含命令和其他上下文(称为“子上下文”)。 例如,在 interface 上下文中,可以更改为 IPv4 和 IPv6 子上下文。

要显示可在上下文中使用的命令和子上下文列表,请在 netsh shell 下键入子上下文名称,然后键入 ?help。 例如:

netsh> interface help

The following commands are available:

Commands in this context:
6to4           - Changes to the `netsh interface 6to4' context.
?              - Displays a list of commands.
dump           - Displays a configuration script.
help           - Displays a list of commands.
httpstunnel    - Changes to the `netsh interface httpstunnel' context.
ipv4           - Changes to the `netsh interface ipv4' context.
ipv6           - Changes to the `netsh interface ipv6' context.
isatap         - Changes to the `netsh interface isatap' context.
portproxy      - Changes to the `netsh interface portproxy' context.
set            - Sets configuration information.
show           - Displays information.
tcp            - Changes to the `netsh interface tcp' context.
teredo         - Changes to the `netsh interface teredo' context.
udp            - Changes to the `netsh interface udp' context.

The following sub-contexts are available:
6to4 httpstunnel ipv4 ipv6 isatap portproxy tcp teredo udp

To view help for a command, type the command, followed by a space, and then type ?.

要在不更改当前上下文的情况下在另一个上下文中执行任务,请在 netsh shell 处键入要使用的命令的上下文路径。 例如,要为“NetLAN1”添加静态 IPv4 地址并指定子网掩码和网关,请键入:

netsh interface ipv4 set address "NetLAN1" static <IP_Address> <Subnet_Mask> <Gateway>

格式设置图例

当在 netsh shell 下、批处理文件或脚本中运行命令时,可以使用以下格式设置图例来解释和使用正确的 netsh 命令语法。

  • 斜体文本是键入命令时必须提供的信息。 例如,如果命令有一个名为 -UserName 的参数,则必须键入实际的用户名。
  • 粗体文本是键入命令时必须完全按所示内容键入的信息
  • 后跟省略号(...)的文本是可在命令行中多次重复的参数。
  • 方括号 [ ] 中间的文本是可选项。
  • 大括号 { } 中间用竖线分隔的选项文本提供了一组选项,你只能从中选择一个选项,如 {enable|disable}
  • 字体格式为 Courier 的文本是代码或程序输出。

netsh 命令的字符串值

一些命令包含需要字符串值的参数。 如果字符串值在字符之间包含空格,则需要将字符串值括在双引号中。

在为 IPv4 网络设置具有与 DHCP 的无线网络连接的字符串值的 Interface 参数时,请在字符串值两侧使用双引号。 例如:

netsh interface ipv4>set address name="Wireless Network Connection" dhcp

或者,对于网络接口,可以使用索引号代替与其关联的名称。 例如:

netsh interface ipv4>set address name=15 dhcp

要查找连接的接口的名称或索引,必须位于 IPv4 或 IPv6 接口子上下文中,并键入 show interfaces。 例如:

netsh interface ipv4>show interfaces
netsh interface ipv6>show interfaces

另请参阅