付録 B — PowerShell

B.1 概要

PowerShell は,Microsoft によって開発されたコマンドラインシェルです.

B.2 ヘルプ

Name Alias Description Example
Get-Help man Get help Get-Help Set-Location

B.3 ファイルシステム

Name Alias Description Example
Set-Location cd, chdir, sl Change directory cd C:\
Get-Location pwd, gl Print working directory pwd
Get-ChildItem dir, ls List directory contents dir
New-Item ni Create new item ni file.txt
Rename-Item ren Rename item ren file.txt newfile.txt
Copy-Item copy Copy item copy file.txt C:\
Move-Item move Move item move file.txt C:\
Get-Content gc Get item content gc file.txt
Remove-Item del Remove item del file.txt

B.3.1 Set-Location

Set-Location コマンドレットは,カレントディレクトリを変更します.

Set-Location C:\

Set-Location .. コマンドを使用して,親ディレクトリに移動します.

Set-Location ..

B.3.2 Get-Location

Get-Location コマンドレットは,カレントディレクトリを取得します.

Get-Location

B.3.3 Get-ChildItem

Get-ChildItem コマンドレットは,指定されたディレクトリ内のファイルとサブディレクトリのリストを取得します.

Get-ChildItem

-Path パラメーターを使用して,指定されたディレクトリ内のファイルとサブディレクトリのリストを取得します.

Get-ChildItem -Path C:\

-Recurse パラメーターを使用すると,全てのサブディレクトリを再帰的に取得します.-Depth パラメーターを使用して,再帰の深さを指定できます.ここでは,1 つのサブディレクトリまで取得します.

Get-ChildItem -Recurse -Depth 1

-Force パラメーターを使用して,非表示の項目を含むすべての項目を取得します.

Get-ChildItem -Force

-Filter パラメーターを使用して,指定されたフィルターに一致する項目のみを取得します.次の例では,拡張子が .txt のファイルのみを取得します.

Get-ChildItem -Filter *.txt

B.3.4 New-Item

New-Item コマンドレットは,新しいファイル,ディレクトリを作成します.

以下の例は,現在のディレクトリにファイル test.txt を作成します.

New-Item test.txt

-ItemType パラメーターを使用して,新しい項目の種類を指定できます.File はファイル,Directory はディレクトリを指定します.

New-Item test -ItemType Directory

-Path パラメーターを使用して,新しい項目の場所を指定できます.-Name パラメーターを使用して,新しい項目の名前を指定できます.-Value パラメーターを使用して,新しいファイルの内容を指定できます.

以下の例は,C:\test ディレクトリにファイル test.txt を作成し,内容を Hello, World! とします.

New-Item -Path C:\test -Name test.txt -ItemType File -Value "Hello, World!"

B.3.5 Rename-Item

Rename-Item コマンドレットは,ファイルまたはディレクトリの名前を変更します.

以下の例は,test.txt ファイルを newtest.txt にリネームします.

Rename-Item -Path "C:\test\test.txt" -NewName "newtest.txt"

ここで,test.txt ファイルが存在しない場合,次のエラーメッセージが表示されます.C:\test\test.txt が存在しないため,リネームできません.

Rename-Item : Cannot rename because item at 'C:\test\test.txt' does not exist.

B.3.6 Copy-Item

Copy-Item コマンドレットは,ファイルまたはディレクトリを別の場所にコピーします.

以下の例は,test.txt ファイルを C:\test2 ディレクトリにコピーします.

Copy-Item -Path "C:\test\test.txt" -Destination "C:\test2"

B.3.7 Move-Item

Move-Item コマンドレットは,ファイルまたはディレクトリを別の場所に移動します.

以下の例は,test.txt ファイルを C:\test2 ディレクトリに移動します.

Move-Item -Path "C:\test\test.txt" -Destination "C:\test2"

B.3.8 Get-Content

Get-Content コマンドレットは,ファイルの内容を取得します.

以下の例は,test.txt ファイルの内容を取得します.

Get-Content -Path C:\test\test.txt

B.3.9 Remove-Item

Remove-Item コマンドレットは,ファイルまたはディレクトリを削除します.

以下の例は,test.txt ファイルを削除します.

Remove-Item -Path C:\test\test.txt

B.4 ネットワーク

Name Description Example
Test-Connection Test network connection Test-Connection www.example.com
Test-NetConnection Test network connection Test-NetConnection
Resolve-DnsName Resolve DNS name Resolve-DnsName -name www.example.com
Get-NetIPConfiguration Get IP configuration Get-NetIPConfiguration
Get-NetTCPConnection Get TCP connection Get-NetTCPConnection
Get-NetAdapter Get network adapter Get-NetAdapter

B.4.1 Test-Connection

Test-Connection コマンドレットは,ICMPエコー要求パケット(Ping)を1つ以上のコンピューターに送信します.特定のコンピューターにIPネットワーク経由で接続できるかどうかを判断できます.

Test-Connection www.example.com

-Count パラメーターを使用して,送信するエコー要求パケットの数を指定できます.

Test-Connection www.example.com -Count 4

-Quiet パラメーターを使用して,テストの結果をブール値で返します.

Test-Connection www.example.com -Quiet

B.4.2 Test-NetConnection

Test-NetConnection コマンドレットは,ネットワーク接続の診断情報を提供します.

下記の例は,デフォルトのサーバーに対してPingテストを実行します.

Test-NetConnection

-Port パラメーターを使用して,デフォルトのサーバーに対して特定のポートに接続テストを実行します.

Test-NetConnection -Port 80

-CommonTCPPort パラメーターを使用して,デフォルトのサーバーに対して一般的なTCPポートに接続テストを実行します.

Test-NetConnection -CommonTCPPort HTTP

-ComputerName パラメーターを使用して,リモートコンピューターに対してPingテストを実行します.

Test-NetConnection -ComputerName www.example.com

-TraceRoute パラメーターを使用して,パケットがネットワーク上でどのように移動するかを示す情報を提供します.

Test-NetConnection -ComputerName www.example.com -TraceRoute

B.4.3 Resolve-DnsName

Resolve-DnsName コマンドレットは,名前解決を実行します.

以下の例は,www.example.com のIPアドレスを求めます.

Resolve-DnsName -name www.example.com

-Type パラメーターを使用して,特定のDNSレコードタイプを指定できます.A はIPv4 アドレスを指定,AAAA はIPv6 アドレスを指定します.

Resolve-DnsName -name www.example.com -Type A

B.4.4 Get-NetIPConfiguration

Get-NetIPConfiguration コマンドレットは,ネットワークアダプターのIP設定情報を取得します.

Get-NetIPConfiguration

B.4.5 Get-NetTCPConnection

Get-NetTCPConnection コマンドレットは,TCP接続の情報を取得します.

Get-NetTCPConnection

-State パラメーターをEstablished に設定して,確立されたTCP接続の情報を取得します.

Get-NetTCPConnection -State Established

B.4.6 Get-NetAdapter

Get-NetAdapter コマンドレットは,ネットワークアダプターの情報を取得します.Get-NetAdapter コマンドレットを使用して,ネットワークアダプターの名前,MACアドレス,LinkSpeed,状態などの情報を取得できます.

Get-NetAdapter