この記事では、netsh コンテキストとサブコンテキストを入力する方法、netsh 構文とコマンドの書式の概要、ローカル コンピューターとリモート コンピューターで netsh コマンドを実行する方法について説明します。
netsh は、現在実行中のコンピューターのネットワーク構成を表示したり変更したりするためのコマンド ライン スクリプト ユーティリティです。 Netsh コマンドは、netsh シェルでコマンドを入力することで実行できるうえに、バッチ ファイルまたはスクリプトで使うことができます。 リモート コンピューターとローカル コンピューターは、netsh コマンドを使用して構成できます。
また、指定したコンピューターに対して一連のコマンドをバッチ モードで実行できるスクリプト機能も備えています。 netsh では、構成スクリプトをテキスト ファイルに保存して、アーカイブとして保管したり、他のコンピューターの構成に利用したりできます。
Syntax
netsh
netsh [-a AliasFile] [-c Context] [-r RemoteMachine] [-u [DomainName\]UserName] [-p Password | *] [Command | -f ScriptFile]
次のパラメーターは省略可能と見なされます。
Parameter | Description |
---|---|
-a | Specifies that you're returned to the netsh shell after running AliasFile. |
AliasFile |
Specifies the name of the text file that contains one or more netsh commands. |
-c | Specifies that netsh enters the specified netsh context. |
Context |
Specifies the netsh context that you want to enter. |
-r | コマンドをリモート コンピューターで実行することを指定します。 The Remote Registry service must be running on the remote computer. 実行されていない場合は、"ネットワーク パスが見つかりません" というエラー メッセージが表示されます。 |
RemoteComputer |
構成するリモート コンピューターを指定します。 |
-u | ユーザー アカウントで netsh コマンドを実行することを指定します。 |
DomainName\ |
ユーザー アカウントが存在するドメインを指定します。 The default is the local domain if DomainName\ isn't specified. |
UserName |
ユーザー アカウント名を指定します。 |
-p | ユーザー アカウントのパスワードを指定することを指定します。 |
Password |
Specifies the password for the user account that you specified with -uUserName. |
Command |
Specifies the netsh command that you want to run. |
-f | Exits netsh after running the script that you designate with ScriptFile. |
ScriptFile |
実行するスクリプトを指定します。 |
Note
If you specify -r
followed by another command, netsh runs the command on the remote computer and then returns to the command prompt. If you specify -r
without another command, netsh opens in remote mode. The process is similar to using set machine at the netsh command shell. When you use -r
, you set the target computer for the current instance of netsh only. After you exit and reenter netsh, the target computer is reset as the local computer. 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 コマンドの実行
To run a netsh command, you must start netsh from the command prompt or PowerShell by typing netsh and then pressing ENTER. 次に、使用するコマンドが含まれているコンテキストに変更できます。 使用できるコンテキストは、インストールされているネットワーク コンポーネントによって異なります。
For example, if you type dhcp in the netsh shell and press ENTER, netsh changes to the DHCP server context. DHCP サーバー ロールがインストールされていない場合は、次のメッセージが表示されます。
The following command was not found: dhcp.
Netsh contexts
netsh は、ダイナミックリンク ライブラリ (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. これらのコンテキストにより、1 つ以上のサービス、ユーティリティ、またはプロトコルの構成と監視のサポートが提供されるため、netsh の機能が拡張されます。
たとえば、Dhcpmon.dll では netsh に DHCP サーバーを構成して管理するために必要なコンテキストとコマンドのセットが提供されます。
netsh コンテキストの一覧を取得するには、コマンド プロンプトまたは Windows PowerShell を開き、「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 subcontexts
Netsh contexts can contain both commands and additional contexts, called subcontexts. For example, within the interface context, you can change to the IPv4 and IPv6 subcontexts.
To display a list of commands and subcontexts that you can use within a context, at the netsh shell, type the subcontext name, and then type either ? or help. For example:
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>
Formatting legend
netsh シェル、またはバッチ ファイルやスクリプト内でコマンドを実行する場合は、次の表記規則に従って、正しい netsh コマンド構文を解釈して使うことができます。
- Text in Italic is information that you must supply while you type the command. 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.
- 角かっこ [ ] の間にあるテキストはオプションの項目です。
- 中かっこ { } に囲まれて、パイプで区切られた選択肢を持つテキストに対しては、たとえば
{enable|disable}
のように、選択肢から 1 つのみを選ぶ必要があります。 - 等幅フォントで書式設定されたテキストは、コードまたはプログラムの出力です。
netsh コマンドの文字列値
文字列値が必要とされるパラメーターが含まれるコマンドが存在します。 文字列値に文字間のスペースが含まれている場合は、文字列値を二重引用符で囲む必要があります。
When setting the interface parameter for IPv4 networks with a string value of Wireless Network Connection to DHCP, use double quotation marks around the string value. For example:
netsh interface ipv4>set address name="Wireless Network Connection" dhcp
または、ネットワーク インターフェイスの場合は、それに関連付けられている名前の代わりにインデックス番号を使うことができます。 For example:
netsh interface ipv4>set address name=15 dhcp
To find the name or index of the connected interfaces, you must be in the IPv4 or IPv6 interface subcontext and type show interfaces. For example:
netsh interface ipv4>show interfaces
netsh interface ipv6>show interfaces