Netsh 命令語法、內容,以及格式

適用於:Windows Server 2022、Windows Server 2019、Windows Server 2016、Azure Stack HCI 21H2 和 20H2 版本

您可以使用本文來了解如何進入 netsh 內文和子內文、了解 netsh 語法和命令格式以及如何在本機和遠端電腦上執行 netsh 命令。

Netsh 是命令列指令碼處理公用程式,讓您顯示或修改目前執行中電腦的網路設定。 Netsh 命令可以透過在 netsh shell 中輸入命令來執行,並且可以在批次檔或腳本中使用。 您可以使用 netsh 命令來設定遠端電腦和本機電腦。

Netsh 也提供指令碼處理功能,讓您針對特定電腦以批次模式執行一組命令。 使用 netsh,您可以將設定指令碼儲存在文字檔中做為封存之用,或協助您設定其他電腦。

語法

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

以下參數被認為是可選的。

參數 描述
a- 指定執行 AliasFile 後返回 AliasFile
AliasFile 指定包含一或多個 netsh 命令的文字檔名稱。
-c 指定 netsh 輸入指定的 netsh 內容。
Context 指定您要輸入的 netsh 內容。
-r 指定您想要在遠端電腦上執行命令。 該遠端註冊表服務必須在遠端電腦上執行。 如果它沒有執行,Windows 將顯示「未找到網路路徑」錯誤訊息。
RemoteComputer 指定您要設定的遠端電腦。
-u 指定您要在使用者帳戶下執行 netsh 命令。
DomainName\ 指定使用者帳戶所屬的網域。 如果未指定 DomainName\ 則預設為本機網域。
UserName 指定使用者帳戶名稱。
-p 指定您想要提供使用者帳戶的密碼。
Password 指定使用 -uUserName 指定的使用者帳戶的密碼
Command 指定您要執行的 netsh 命令。
-f 在執行您以 ScriptFile 指定的指令碼之後,結束 netsh
ScriptFile 指定您想要執行的指令碼。

注意

如果指定後面跟著另一個命令,-rnetsh 將在遠端電腦上執行該命令,然後返回命令提示字元。 如果您指定不含另一個命令的 -r,則 netsh 會在遠端模式中開啟。 這個過程類似於在netsh 命令 shell 中使用設置器。 當您使用 -r 時,您只會針對 netsh 的目前執行個體設定目標電腦。 在您結束並重新鍵入 netsh 後,目標電腦會重設為本機電腦。 您可藉由指定存放在 WINS 中的電腦名稱、UNC 名稱、DNS 伺服器所解析的網際網路名稱或 IP 位址,在遠端電腦上執行 netsh 命令。

執行 netsh 命令

若要執行 netsh 命令,必須從命令提示字元或 PowerShell 輸入 netsh,然後點選 ENTER 鍵來啟動 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 中輸入子內文名稱,然後輸入? 或幫助。 例如:

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 時,請在該字串值兩邊使用雙引號。 例如:

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

或者,對於網路介面,可以使用索引號來代替與其關聯的名稱。 例如:

netsh interface ipv4>set address name=15 dhcp

若要尋找已連接介面的名稱或索引,您必須位於 IPv4 或 IPv6 介面子內文中並輸入顯示介面。 例如:

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

另請參閱