Netsh 命令语法、上下文和格式设置Netsh Command Syntax, Contexts, and Formatting

适用于:Windows Server(半年频道)、Windows Server 2016Applies to: Windows Server (Semi-Annual Channel), Windows Server 2016

你可以使用本主题来了解如何进入 netsh 上下文和子上下文、了解 netsh 语法和命令格式,以及如何在本地和远程计算机上运行 netsh 命令。You can use this topic to learn how to enter netsh contexts and subcontexts, understand netsh syntax and command formatting, and how to run netsh commands on local and remote computers.

Netsh 是命令行脚本实用工具,可用于显示或修改当前正在运行的计算机的网络配置。Netsh is a command-line scripting utility that allows you to display or modify the network configuration of a computer that is currently running. Netsh 命令可以通过在 netsh 提示符下键入命令来运行,并且可以在批处理文件或脚本中使用。Netsh commands can be run by typing commands at the netsh prompt and they can be used in batch files or scripts. 可以使用 netsh 命令来配置远程计算机和本地计算机。Remote computers and the local computer can be configured by using netsh commands.

Netsh 还提供脚本功能,可让你在批处理模式下对指定的计算机运行一组命令。Netsh also provides a scripting feature that allows you to run a group of commands in batch mode against a specified computer. 你可以使用 Netsh 将配置脚本保存在文本文件中,以便存档或者帮助你配置其他计算机。With netsh, you can save a configuration script in a text file for archival purposes or to help you configure other computers.

Netsh 上下文Netsh contexts

Netsh 通过使用动态-链接库 (DLL) 文件与其他操作系统组件进行交互。Netsh interacts with other operating system components by using dynamic-link library (DLL) files.

每个 netsh helper DLL 提供一组称为 "上下文" 的功能,这是一组特定于网络服务器角色或功能的命令。Each netsh helper DLL provides an extensive set of features called a context, which is a group of commands specific to a networking server role or feature. 这些上下文通过为一个或多个服务、实用工具或协议提供配置和监视支持来扩展 netsh 功能。These contexts extend the functionality of netsh by providing configuration and monitoring support for one or more services, utilities, or protocols. 例如,Dhcpmon 向 netsh 提供配置和管理 DHCP 服务器所需的上下文和命令集。For example, Dhcpmon.dll provides netsh with the context and set of commands necessary to configure and manage DHCP servers.

获取上下文列表Obtain a list of contexts

可以通过在运行 Windows Server 2016 或 Windows 10 的计算机上打开命令提示符或 Windows PowerShell 来获取 netsh 上下文的列表。You can obtain a list of netsh contexts by opening either command prompt or Windows PowerShell on a computer running Windows Server 2016 or Windows 10. 键入命令netsh ,然后按 enter。Type the command netsh and press ENTER. 键入 /? ,然后按 enter。Type /?, and then press ENTER.

下面是运行 Windows Server 2016 Datacenter 的计算机上的这些命令的示例输出。Following is example output for these commands on a computer running Windows Server 2016 Datacenter.

PS C:\Windows\system32> netsh

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 contexts can contain both commands and additional contexts, called subcontexts. 例如,在路由上下文中,可以更改为 IP 和 IPv6 子上下文。For example, within the Routing context, you can change to the IP and IPv6 subcontexts.

若要显示可以在上下文中使用的命令和子上下文列表,请在 netsh 提示符下键入上下文名称,然后键入 /?To display a list of commands and subcontexts that you can use within a context, at the netsh prompt, type the context name, and then type either /? 或 "帮助"。or help. 例如,若要显示可以在路由上下文中使用的子上下文和命令的列表,请在 netsh 提示符 (即netsh> )下键入以下命令之一:For example, to display a list of subcontexts and commands that you can use in the Routing context, at the netsh prompt (that is, netsh>), type one of the following:

路由/?routing /?

路由帮助routing help

若要在不更改当前上下文的情况下在另一个上下文中执行任务,请在 netsh 提示符下键入要使用的命令的上下文路径。To perform tasks in another context without changing from your current context, type the context path of the command you want to use at the netsh prompt. 例如,若要在 IGMP 上下文中添加名为 "本地连接" 的接口,而无需首先更改 IGMP 上下文,请在 netsh 提示符下键入:For example, to add an interface named "Local Area Connection" in the IGMP context without first changing to the IGMP context, at the netsh prompt, type:

路由 ip igmp 添加接口 "本地区域连接" startupqueryinterval = 21routing ip igmp add interface "Local Area Connection" startupqueryinterval=21

运行 netsh 命令Running netsh commands

若要运行 netsh 命令,必须在命令提示符下键入netsh ,然后按 enter 启动 netsh。To run a netsh command, you must start netsh from the command prompt by typing netsh and then pressing ENTER. 接下来,可以更改为包含要使用的命令的上下文。Next, you can change to the context that contains the command you want to use. 可用的上下文取决于已安装的网络组件。The contexts that are available depend on the networking components that you have installed. 例如,如果在 netsh 提示符下键入dhcp并按 enter,则 netsh 会更改为 dhcp 服务器的上下文。For example, if you type dhcp at the netsh prompt and press ENTER, netsh changes to the DHCP server context. 但是,如果未安装 DHCP,则会显示以下消息:If you do not have DHCP installed, however, the following message appears:

找不到以下命令: dhcp。The following command was not found: dhcp.

格式化图例Formatting Legend

当你在 netsh 提示符下或在批处理文件或脚本中运行命令时,可以使用以下格式设置图例来解释和使用正确的 netsh 命令语法。You can use the following formatting legend to interpret and use correct netsh command syntax when you run the command at the netsh prompt or in a batch file or script.

  • 斜体文本是键入命令时必须提供的信息。Text in Italic is information that you must supply while you type the command. 例如,如果命令有一个名为 "UserName" 的参数,则必须键入实际的用户名。For example, if a command has a parameter named -UserName, you must type the actual user name.
  • 粗体文本是你键入命令时必须完全按所示键入的信息。Text in Bold is information that you must type exactly as shown while you type the command.
  • 后跟省略号 (的文本 ...) 是可在命令行中重复多次的参数。Text followed by an ellipsis (...) is a parameter that can be repeated several times in a command line.
  • 方括号 [ ] 之间的文本是一个可选项。Text that is between brackets [ ] is an optional item.
  • 具有由管道分隔的选项的大括号 { } 之间的文本提供了一组选项,您只能从中选择一个选项,如 {enable|disable}Text that is between braces { } with choices separated by a pipe provides a set of choices from which you must select only one, such as {enable|disable}.
  • 用 "宋体" 设置格式的文本为代码或程序输出。Text that is formatted with the Courier font is code or program output.

在命令提示符或 Windows PowerShell 中运行 Netsh 命令Running Netsh commands from the command prompt or Windows PowerShell

若要在命令提示符处或在 Windows PowerShell 中启动网络 Shell 并输入 netsh,可以使用以下命令。To start Network Shell and enter netsh at the command prompt or in Windows PowerShell, you can use the following command.


Netsh 是命令行脚本实用工具,可让你以本地或远程方式显示或修改当前正在运行的计算机的网络配置。Netsh is a command-line scripting utility that allows you to, either locally or remotely, display or modify the network configuration of a currently running computer. 使用不带参数的netsh会打开 dism.exe 命令提示符 (即netsh> )。Used without parameters, netsh opens the Netsh.exe command prompt (that is, netsh>).


netsh[ -a AliasFile] [ -c 上下文 ] [ ] [ [\ ] ] [   * | ** ] [ |   密码 {NetshCommand ScriptFile}]netsh[ -a AliasFile] [ -c Context ] [-r RemoteComputer] [ -u [ DomainName\ ] UserName ] [ -p Password | *] [{NetshCommand | -f ScriptFile}]



可选。Optional. 指定在运行AliasFile后返回到netsh提示符。Specifies that you are returned to the netsh prompt after running AliasFile.


可选。Optional. 指定包含一个或多个netsh命令的文本文件的名称。Specifies the name of the text file that contains one or more netsh commands.


可选。Optional. 指定 netsh 输入指定的netsh上下文。Specifies that netsh enters the specified netsh context.


可选。Optional. 指定要输入的netsh上下文。Specifies the netsh context that you want to enter.


可选。Optional. 指定您希望命令在远程计算机上运行。Specifies that you want the command to run on a remote computer.


使用netsh – r参数在另一台计算机上远程使用某些 netsh 命令时,远程计算机上必须运行远程注册表服务。When you use some netsh commands remotely on another computer with the netsh –r parameter, the Remote Registry service must be running on the remote computer. 如果未运行,Windows 将显示 "找不到网络路径" 错误消息。If it is not running, Windows displays a “Network Path Not Found” error message.


可选。Optional. 指定要配置的远程计算机。Specifies the remote computer that you want to configure.


可选。Optional. 指定要在用户帐户下运行 netsh 命令。Specifies that you want to run the netsh command under a user account.


可选。Optional. 指定用户帐户所在的域。Specifies the domain where the user account is located. 如果未指定DomainName\ ,则默认为本地域。The default is the local domain if DomainName\ is not specified.


可选。Optional. 指定用户帐户名称。Specifies the user account name.


可选。Optional. 指定您要为该用户帐户提供密码。Specifies that you want to provide a password for the user account.


可选。Optional. 指定用 -u 用户名指定的用户帐户的密码。Specifies the password for the user account that you specified with -u UserName.


可选。Optional. 指定要运行的netsh命令。Specifies the netsh command that you want to run.


可选。Optional. 运行在ScriptFile中指定的脚本后退出netshExits netsh after running the script that you designate with ScriptFile.


可选。Optional. 指定要运行的脚本。Specifies the script that you want to run.


可选。Optional. 在 netsh 提示符下显示帮助。Displays help at the netsh prompt.


如果指定 -r 后跟另一个命令,则netsh会在远程计算机上运行该命令,然后返回到 cmd.exe 命令提示符。If you specify -r followed by another command, netsh runs the command on the remote computer and then returns to the Cmd.exe command prompt. 如果指定 -r 而不指定其他命令,则netsh将在远程模式下打开。If you specify -r without another command, netsh opens in remote mode. 此过程类似于在 Netsh 命令提示符下使用 "设置计算机"。The process is similar to using set machine at the Netsh command prompt. 使用 -r 时,只会为当前netsh实例设置目标计算机。When you use -r, you set the target computer for the current instance of netsh only. 退出并重新输入netsh后,目标计算机将重置为本地计算机。After you exit and reenter netsh, the target computer is reset as the local computer. 可以通过指定存储在 WINS 中的计算机名、UNC 名称、DNS 服务器要解析的 Internet 名称或 IP 地址,在远程计算机上运行netsh命令。You can run netsh commands on a remote computer by specifying a computer name stored in WINS, a UNC name, an Internet name to be resolved by the DNS server, or an IP address.

键入 netsh 命令的参数字符串值Typing parameter string values for netsh commands

在 Netsh 命令引用中,有一些命令包含需要字符串值的参数。Throughout the Netsh command reference there are commands that contain parameters for which a string value is required.

如果字符串值包含字符之间的空格(如包含多个单词的字符串值),则需要将字符串值括在引号中。In the case where a string value contains spaces between characters, such as string values that consist of more than one word, it is required that you enclose the string value in quotation marks. 例如,对于具有 "无线网络连接" 字符串值的名为interface的参数,请使用引号将字符串值括起来:For example, for a parameter named interface with a string value of Wireless Network Connection, use quotation marks around the string value:

interface="Wireless Network Connection"