Netsh コマンドの構文、コンテキスト、形式

Windows Server 2022、Windows Server 2019、Windows Server 2016、Azure Stack HCI の 21H2 と 20H2 バージョンに適用します

この記事では、netsh コンテキストとサブコンテキストを入力する方法、netsh 構文とコマンドの書式の概要、ローカル コンピューターとリモート コンピューターで netsh コマンドを実行する方法について説明します。

netsh は、現在実行中のコンピューターのネットワーク構成を表示したり変更したりするためのコマンド ライン スクリプト ユーティリティです。 Netsh コマンドは、netsh シェルでコマンドを入力することで実行できるうえに、バッチ ファイルまたはスクリプトで使うことができます。 リモート コンピューターとローカル コンピューターは、netsh コマンドを使用して構成できます。

また、指定したコンピューターに対して一連のコマンドをバッチ モードで実行できるスクリプト機能も備えています。 netsh では、構成スクリプトをテキスト ファイルに保存して、アーカイブとして保管したり、他のコンピューターの構成に利用したりできます。

構文

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

次のパラメーターは省略可能と見なされます。

パラメーター 説明
-a AliasFile を実行した後で netsh シェルに戻るように指定します。
AliasFile netsh コマンドを 1 つ以上含むテキスト ファイルの名前を指定します。
-c 指定された netsh コンテキストが netsh によって入力されることを指定します。
Context 入力する netsh コンテキストを指定します。
-r コマンドをリモート コンピューターで実行することを指定します。 リモート レジストリ サービスは、リモート コンピューターで実行する必要があります。 実行していない場合、Windows では "ネットワーク パスが見つかりません" というエラー メッセージが表示されます。
RemoteComputer 構成するリモート コンピューターを指定します。
-u ユーザー アカウントで netsh コマンドを実行することを指定します。
DomainName\ ユーザー アカウントが存在するドメインを指定します。 DomainName\ が指定されていない場合の既定値は、ローカル ドメインです。
UserName ユーザー アカウント名を指定します。
-p ユーザー アカウントのパスワードを指定することを指定します。
Password -uUserName で指定したユーザー アカウントのためのパスワードを指定します。
Command 実行する netsh コマンドを指定します。
-f ScriptFile で指定したスクリプトを実行した後に netsh を終了します。
ScriptFile 実行するスクリプトを指定します。

Note

-r に続けて別のコマンドを指定した場合、netsh を使うと、リモート コンピューターでコマンドが実行され、その後コマンド プロンプトに戻ります。 別のコマンドを使用せずに -r を指定すると、netsh がリモート モードで開きます。 このプロセスは、set machine を netsh コマンド シェルで使うのと似ています。 -r を使用するときは、netsh の現在のインスタンスに対してのみターゲット コンピューターを設定します。 netsh を終了して再入力すると、ターゲット コンピューターがローカル コンピューターとして再設定されます。 WINS に格納されているコンピューター名、UNC 名、DNS サーバーによって解決されるインターネット名、または IP アドレスを指定することにより、リモート コンピューター上で netsh コマンドを実行できます。

netsh コマンドの実行

netsh コマンドを実行するには、「netsh」と入力し、Enter キーを押して、コマンド プロンプトまたは PowerShell から netsh を起動する必要があります。 次に、使用するコマンドが含まれているコンテキストに変更できます。 使用できるコンテキストは、インストールしたネットワーク コンポーネントによって異なります。

たとえば、netsh シェルに「dhcp」と入力し、Enter キーを押すと、netsh が DHCP サーバーのコンテキストに変わります。 DHCP サーバー ロールがインストールされていない場合は、次のメッセージが表示されます。

The following command was not found: dhcp.

netsh コンテキスト

netsh は、ダイナミックリンク ライブラリ (DLL) ファイルを使って、他のオペレーティング システムのコンポーネントと相互に作用します。

各 netsh ヘルパー DLL には、ネットワーク サーバーの役割または機能に固有のコマンドのグループである "コンテキスト" と呼ばれるさまざまな機能のセットが用意されています。 これらのコンテキストにより、1 つ以上のサービス、ユーティリティ、またはプロトコルの構成と監視のサポートが提供されるため、netsh の機能が拡張されます。

たとえば、Dhcpmon.dll では netsh に DHCP サーバーを構成して管理するために必要なコンテキストとコマンドのセットが提供されます。

コマンド プロンプトまたは Windows PowerShell を開き、「netsh /?」または「netsh help」と入力し、Enter キーを押すと、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 コンテキストには、コマンドと、"サブコンテキスト" と呼ばれる追加のコンテキストの両方を含めることができます。 たとえば、interface コンテキスト内では、IPv4 サブコンテキストと IPv6 サブコンテキストに変更できます。

コンテキスト内で使うことができるコマンドとサブコンテキストの一覧を表示するには、サブコンテキスト名を入力して、「?」または「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 シェルで使うコマンドのコンテキスト パスを入力します。 たとえば、"NetLAN1" の静的 IPv4 アドレスを追加し、サブネット マスクとゲートウェイの両方を指定するには、次のように入力します。

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

表記規則

netsh シェル、またはバッチ ファイルやスクリプト内でコマンドを実行する場合は、次の表記規則に従って、正しい netsh コマンド構文を解釈して使うことができます。

  • "イタリック" のテキストは、コマンドの入力時に指定する必要がある情報です。 たとえば、コマンドに -UserName という名前のパラメーターがある場合、実際のユーザー名を入力する必要があります。
  • 太字のテキストは、コマンドの入力時に表示されるとおりに入力する必要がある情報です。
  • テキストの後に省略記号 (...) が続くパラメーターは、コマンド ラインで複数回繰り返して実行できます。
  • 角かっこ [ ] の間にあるテキストはオプションの項目です。
  • 中かっこ { } に囲まれて、パイプで区切られた選択肢を持つテキストに対しては、たとえば {enable|disable} のように、選択肢から 1 つのみを選ぶ必要があります。
  • 等幅フォントで書式設定されたテキストは、コードまたはプログラムの出力です。

netsh コマンドの文字列値

文字列値が必要とされるパラメーターが含まれるコマンドが存在します。 文字列値の文字間にスペースが含まれている場合は、文字列値を二重引用符で囲む必要があります。

Wireless Network Connection という文字列値を使って IPv4 ネットワークの 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

関連項目