「サーバーやWebサイトにつながらない」
「どこで通信が切れているのかわからない」
「ポートが開いているかどうか確認したい」
ネットワークトラブルに遭遇した時、こんなふうに困ったことはありませんか?
そんな時に便利なのがPowerShellを使った通信確認です。
専用ツールを使わなくても、Windowsに標準で入っているPowerShellだけで、詳細なネットワーク診断ができます。
この記事では、Windows PowerShellを使ってネットワークやサーバーへの通信が正常かどうかを確認する方法を、初心者にもわかりやすく解説します。
PowerShellでできるネットワーク診断

PowerShellを使うメリット
従来の方法:
- コマンドプロンプトでpingコマンド
- 個別のツールを使い分け
- 結果の記録が面倒
PowerShellを使うと:
- より詳細な情報を取得
- 複数ホストの一括チェック
- 結果の自動記録・分析
- スクリプト化で作業効率化
主な通信確認コマンド
コマンド | 用途 | 確認内容 |
---|---|---|
Test-Connection | 基本的な疎通確認 | ping応答・応答時間 |
Test-NetConnection | 詳細な接続確認 | ポート接続・ルート情報 |
Resolve-DnsName | 名前解決確認 | DNS解決・IPアドレス |
Get-NetRoute | ルート情報確認 | ネットワーク経路 |
PowerShellでpingを使った基本の通信確認
Test-Connectionコマンドの基本
PowerShellでの通信確認はTest-Connection
コマンドが基本です。
基本的な使い方
# 基本的なpingテスト
Test-Connection google.com
実行結果例:
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
DESKTOP-ABC google.com 142.250.207.110 32 15
DESKTOP-ABC google.com 142.250.207.110 32 14
DESKTOP-ABC google.com 142.250.207.110 32 13
DESKTOP-ABC google.com 142.250.207.110 32 12
各項目の意味
項目 | 意味 |
---|---|
Source | 送信元コンピュータ名 |
Destination | 宛先ホスト名 |
IPV4Address | 宛先のIPアドレス |
Bytes | 送信データサイズ |
Time(ms) | 応答時間(ミリ秒) |
送信回数や詳細設定
送信回数を指定
# 1回だけ送信
Test-Connection google.com -Count 1
# 10回送信
Test-Connection google.com -Count 10
簡潔な結果(True/False)
# 成功・失敗のみを表示
Test-Connection google.com -Quiet
実行結果:
True
タイムアウト時間の設定
# タイムアウトを5秒に設定
Test-Connection google.com -Count 1 -Timeout 5
複数のホストを一括確認
# 複数ホストを同時にテスト
Test-Connection google.com, yahoo.co.jp, microsoft.com -Count 1
エラーの種類と意味
よくあるエラーメッセージ
1. ホストが見つからない
Test-Connection : Cannot resolve target name 'unknown-host.com'.
原因: DNS解決に失敗(ホスト名が存在しない)
2. タイムアウト
Test-Connection : Testing connection to computer 'timeout-host.com' failed: Request timeout
原因: ネットワークの切断、ファイアウォールによるブロック
3. 宛先ホストに到達できません
Test-Connection : Testing connection to computer '192.168.1.999' failed: Destination host unreachable
原因: ルーティング設定の問題、存在しないIPアドレス
PowerShellでポート通信の確認をする方法

Test-NetConnectionコマンドの活用
特定のポートが開いているかどうかを調べる場合はTest-NetConnection
を使用します。
基本的なポートテスト
# HTTPSポート(443番)の確認
Test-NetConnection google.com -Port 443
実行結果例:
ComputerName : google.com
RemoteAddress : 142.250.207.110
RemotePort : 443
InterfaceAlias : Wi-Fi
SourceAddress : 192.168.1.100
TcpTestSucceeded : True
結果の解読
項目 | 意味 |
---|---|
ComputerName | 接続先ホスト名 |
RemoteAddress | 接続先IPアドレス |
RemotePort | 接続先ポート番号 |
InterfaceAlias | 使用したネットワークインターフェース |
SourceAddress | 送信元IPアドレス |
TcpTestSucceeded | 接続成功・失敗(最重要) |
よく使われるポート番号
ポート番号 | サービス | 用途 |
---|---|---|
80 | HTTP | Webサイト(非暗号化) |
443 | HTTPS | Webサイト(暗号化) |
22 | SSH | リモートログイン |
21 | FTP | ファイル転送 |
25 | SMTP | メール送信 |
110 | POP3 | メール受信 |
143 | IMAP | メール受信 |
3389 | RDP | リモートデスクトップ |
1433 | SQL Server | データベース |
3306 | MySQL | データベース |
実践的なポートチェック例
Webサーバーの動作確認
# HTTPとHTTPSの両方をチェック
Write-Host "=== Webサーバー接続テスト ==="
$result80 = Test-NetConnection example.com -Port 80 -WarningAction SilentlyContinue
$result443 = Test-NetConnection example.com -Port 443 -WarningAction SilentlyContinue
if ($result80.TcpTestSucceeded) {
Write-Host "HTTP (80番ポート): ✅ 接続可能"
} else {
Write-Host "HTTP (80番ポート): ❌ 接続不可"
}
if ($result443.TcpTestSucceeded) {
Write-Host "HTTPS (443番ポート): ✅ 接続可能"
} else {
Write-Host "HTTPS (443番ポート): ❌ 接続不可"
}
データベースサーバーの確認
# SQL Serverへの接続確認
Test-NetConnection dbserver.company.com -Port 1433
メールサーバーの確認
# SMTPサーバーの確認
Test-NetConnection mail.company.com -Port 25
# POP3サーバーの確認
Test-NetConnection mail.company.com -Port 110
ポートが閉じている場合の対処法
1. ファイアウォールの確認
# Windowsファイアウォールのルール確認
Get-NetFirewallRule | Where-Object {$_.DisplayName -like "*80*"}
2. サービスの動作確認
# 特定のサービスが動作しているか確認
Get-Service | Where-Object {$_.DisplayName -like "*IIS*"}
3. リッスンしているポートの確認
# システムでリッスンしているポートを表示
Get-NetTCPConnection -State Listen | Sort-Object LocalPort
DNS解決の確認

Resolve-DnsNameコマンド
名前解決(DNS)の問題を調べる場合に使用します。
基本的なDNS解決
# ホスト名からIPアドレスを取得
Resolve-DnsName google.com
実行結果例:
Name Type TTL Section IPAddress
---- ---- --- ------- ---------
google.com A 300 Answer 142.250.207.110
逆引きDNS(IPアドレスからホスト名)
# IPアドレスからホスト名を取得
Resolve-DnsName 8.8.8.8
特定のレコードタイプを指定
# MXレコード(メールサーバー)の確認
Resolve-DnsName google.com -Type MX
# NSレコード(ネームサーバー)の確認
Resolve-DnsName google.com -Type NS
# CNAMEレコード(別名)の確認
Resolve-DnsName www.google.com -Type CNAME
DNSサーバーの指定
# 特定のDNSサーバーを使用して解決
Resolve-DnsName google.com -Server 8.8.8.8
# 社内DNSサーバーを使用
Resolve-DnsName internal-server.company.com -Server 192.168.1.10
通信確認をスクリプト化して便利に
複数ホストの一括チェックスクリプト
基本的な監視スクリプト
# 監視対象のホストとポートを定義
$targets = @(
@{Host="google.com"; Port=443; Name="Google HTTPS"},
@{Host="microsoft.com"; Port=443; Name="Microsoft HTTPS"},
@{Host="192.168.1.1"; Port=0; Name="ルーター"} # Port=0はpingのみ
)
Write-Host "=== ネットワーク接続チェック開始 ===" -ForegroundColor Yellow
Write-Host "実行時刻: $(Get-Date)" -ForegroundColor Gray
Write-Host ""
foreach ($target in $targets) {
Write-Host "[$($target.Name)] チェック中..." -NoNewline
if ($target.Port -eq 0) {
# pingチェック
$result = Test-Connection $target.Host -Count 1 -Quiet
if ($result) {
Write-Host " ✅ 疎通OK" -ForegroundColor Green
} else {
Write-Host " ❌ 疎通NG" -ForegroundColor Red
}
} else {
# ポートチェック
$result = Test-NetConnection $target.Host -Port $target.Port -WarningAction SilentlyContinue
if ($result.TcpTestSucceeded) {
Write-Host " ✅ 接続OK" -ForegroundColor Green
} else {
Write-Host " ❌ 接続NG" -ForegroundColor Red
}
}
}
Write-Host ""
Write-Host "=== チェック完了 ===" -ForegroundColor Yellow
詳細なレポート生成
# 詳細なネットワークレポートを作成
function Test-NetworkConnectivity {
param(
[string[]]$Hosts = @("google.com", "microsoft.com", "yahoo.co.jp"),
[int[]]$Ports = @(80, 443),
[string]$OutputFile = "network_report.txt"
)
$report = @()
$timestamp = Get-Date
foreach ($host in $Hosts) {
# ping テスト
$pingResult = Test-Connection $host -Count 4 -ErrorAction SilentlyContinue
if ($pingResult) {
$avgTime = ($pingResult | Measure-Object -Property ResponseTime -Average).Average
$maxTime = ($pingResult | Measure-Object -Property ResponseTime -Maximum).Maximum
$minTime = ($pingResult | Measure-Object -Property ResponseTime -Minimum).Minimum
$pingStatus = "成功 (平均: $([math]::Round($avgTime, 2))ms)"
} else {
$pingStatus = "失敗"
}
# ポートテスト
$portResults = @()
foreach ($port in $Ports) {
$portTest = Test-NetConnection $host -Port $port -WarningAction SilentlyContinue
$portResults += [PSCustomObject]@{
Port = $port
Status = if ($portTest.TcpTestSucceeded) { "開放" } else { "閉鎖" }
}
}
# DNS解決
$dnsResult = Resolve-DnsName $host -ErrorAction SilentlyContinue
$ipAddress = if ($dnsResult) { $dnsResult[0].IPAddress } else { "解決失敗" }
$report += [PSCustomObject]@{
ホスト名 = $host
IPアドレス = $ipAddress
Ping結果 = $pingStatus
ポート結果 = ($portResults | ForEach-Object { "$($_.Port):$($_.Status)" }) -join ", "
テスト時刻 = $timestamp
}
}
# 結果を表示
$report | Format-Table -AutoSize
# ファイルに出力
$report | Export-Csv -Path $OutputFile -Encoding UTF8 -NoTypeInformation
Write-Host "レポートを $OutputFile に保存しました。" -ForegroundColor Green
}
# 実行例
Test-NetworkConnectivity
継続監視スクリプト
# 指定した間隔で継続的に監視
function Start-NetworkMonitoring {
param(
[string]$Host = "google.com",
[int]$Port = 443,
[int]$IntervalSeconds = 60,
[string]$LogFile = "network_monitor.log"
)
Write-Host "ネットワーク監視を開始します..." -ForegroundColor Yellow
Write-Host "対象: $Host : $Port" -ForegroundColor Gray
Write-Host "間隔: $IntervalSeconds 秒" -ForegroundColor Gray
Write-Host "Ctrl+C で停止" -ForegroundColor Gray
Write-Host ""
while ($true) {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
try {
if ($Port -eq 0) {
$result = Test-Connection $Host -Count 1 -Quiet
$status = if ($result) { "成功" } else { "失敗" }
$logEntry = "$timestamp [PING] $Host - $status"
} else {
$result = Test-NetConnection $Host -Port $Port -WarningAction SilentlyContinue
$status = if ($result.TcpTestSucceeded) { "成功" } else { "失敗" }
$logEntry = "$timestamp [PORT] $Host : $Port - $status"
}
# 結果表示
$color = if ($status -eq "成功") { "Green" } else { "Red" }
Write-Host $logEntry -ForegroundColor $color
# ログファイルに記録
$logEntry | Out-File -FilePath $LogFile -Append -Encoding UTF8
} catch {
$errorEntry = "$timestamp [ERROR] $Host - $($_.Exception.Message)"
Write-Host $errorEntry -ForegroundColor Red
$errorEntry | Out-File -FilePath $LogFile -Append -Encoding UTF8
}
Start-Sleep -Seconds $IntervalSeconds
}
}
# 使用例
# Start-NetworkMonitoring -Host "google.com" -Port 443 -IntervalSeconds 30
トラブルシューティングの手順

体系的な診断アプローチ
ステップ1:基本的な疎通確認
# 1. ローカルネットワークの確認
Write-Host "=== ステップ1: ローカルネットワーク確認 ==="
Test-Connection 127.0.0.1 -Count 1 # ローカルホスト
Test-Connection (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.IPAddress -like "192.168.*"}).IPAddress[0] -Count 1 # 自分のIP
# 2. デフォルトゲートウェイの確認
$gateway = (Get-NetRoute -DestinationPrefix "0.0.0.0/0").NextHop
Write-Host "デフォルトゲートウェイ: $gateway"
Test-Connection $gateway -Count 1
ステップ2:DNS解決の確認
Write-Host "=== ステップ2: DNS解決確認 ==="
# パブリックDNSサーバーの確認
Test-Connection 8.8.8.8 -Count 1 # Google DNS
Test-Connection 1.1.1.1 -Count 1 # Cloudflare DNS
# DNS解決テスト
Resolve-DnsName google.com
ステップ3:外部サーバーへの接続確認
Write-Host "=== ステップ3: 外部接続確認 ==="
# 複数の有名サイトで確認
$testSites = @("google.com", "microsoft.com", "cloudflare.com")
foreach ($site in $testSites) {
$result = Test-Connection $site -Count 1 -Quiet
$status = if ($result) { "✅" } else { "❌" }
Write-Host "$status $site"
}
詳細診断スクリプト
function Invoke-NetworkDiagnostics {
param([string]$TargetHost = "google.com")
Write-Host "=== ネットワーク診断開始: $TargetHost ===" -ForegroundColor Yellow
# 1. 基本情報収集
Write-Host "`n📋 基本情報:" -ForegroundColor Cyan
$netConfig = Get-NetIPConfiguration | Where-Object {$_.NetAdapter.Status -eq "Up"}
Write-Host "アクティブなネットワークアダプター: $($netConfig.InterfaceAlias)"
Write-Host "IPアドレス: $($netConfig.IPv4Address.IPAddress)"
Write-Host "デフォルトゲートウェイ: $($netConfig.IPv4DefaultGateway.NextHop)"
Write-Host "DNSサーバー: $($netConfig.DNSServer.ServerAddresses -join ', ')"
# 2. DNS解決テスト
Write-Host "`n🔍 DNS解決テスト:" -ForegroundColor Cyan
try {
$dnsResult = Resolve-DnsName $TargetHost -ErrorAction Stop
Write-Host "✅ DNS解決成功: $($dnsResult[0].IPAddress)"
$targetIP = $dnsResult[0].IPAddress
} catch {
Write-Host "❌ DNS解決失敗: $($_.Exception.Message)" -ForegroundColor Red
return
}
# 3. Pingテスト
Write-Host "`n🏓 Ping テスト:" -ForegroundColor Cyan
$pingResult = Test-Connection $TargetHost -Count 4
if ($pingResult) {
$stats = $pingResult | Measure-Object -Property ResponseTime -Average -Maximum -Minimum
Write-Host "✅ Ping成功"
Write-Host " 平均応答時間: $([math]::Round($stats.Average, 2))ms"
Write-Host " 最大応答時間: $($stats.Maximum)ms"
Write-Host " 最小応答時間: $($stats.Minimum)ms"
} else {
Write-Host "❌ Ping失敗" -ForegroundColor Red
}
# 4. ポートテスト
Write-Host "`n🔌 ポートテスト:" -ForegroundColor Cyan
$commonPorts = @(80, 443, 22, 21, 25)
foreach ($port in $commonPorts) {
$portTest = Test-NetConnection $TargetHost -Port $port -WarningAction SilentlyContinue
$status = if ($portTest.TcpTestSucceeded) { "✅ 開放" } else { "❌ 閉鎖" }
Write-Host " ポート $port : $status"
}
# 5. ルート情報
Write-Host "`n🛤️ ルート情報:" -ForegroundColor Cyan
try {
$route = Test-NetConnection $TargetHost -TraceRoute -WarningAction SilentlyContinue
if ($route.TraceRoute) {
Write-Host "traceroute結果:"
$route.TraceRoute | ForEach-Object { Write-Host " $_" }
}
} catch {
Write-Host "traceroute情報を取得できませんでした"
}
Write-Host "`n=== 診断完了 ===" -ForegroundColor Yellow
}
# 使用例
# Invoke-NetworkDiagnostics "microsoft.com"
よくあるエラーと対処法
エラーパターン別の対処法
1. DNS解決エラー
エラー例:
Resolve-DnsName : Cannot resolve host name 'unknown-host.com'
確認ポイント:
# DNSサーバーの確認
Get-DnsClientServerAddress
# 別のDNSサーバーで試す
Resolve-DnsName unknown-host.com -Server 8.8.8.8
# DNSキャッシュのクリア
Clear-DnsClientCache
2. ネットワーク接続エラー
エラー例:
Test-Connection : Network is unreachable
確認ポイント:
# ネットワークアダプターの状態確認
Get-NetAdapter | Where-Object {$_.Status -eq "Up"}
# IPアドレス設定の確認
Get-NetIPAddress -AddressFamily IPv4
# デフォルトゲートウェイの確認
Get-NetRoute -DestinationPrefix "0.0.0.0/0"
3. ファイアウォールによるブロック
確認方法:
# Windowsファイアウォールの状態確認
Get-NetFirewallProfile
# 特定ポートのファイアウォールルール確認
Get-NetFirewallRule | Where-Object {$_.DisplayName -like "*80*"} | Select-Object DisplayName, Enabled, Direction, Action
まとめ:PowerShellでネットワーク診断をマスターしよう
Windows PowerShellを使えば、専用ツールなしでも本格的なネットワーク診断ができます。
今回学んだ重要ポイント
- 基本コマンド:
Test-Connection
でping、Test-NetConnection
でポートチェック - DNS診断:
Resolve-DnsName
で名前解決の確認 - 一括処理: 複数ホストの同時チェック
- 自動化: スクリプト化で定期監視
- 体系的診断: 段階的なトラブルシューティング
コメント