PowerShell – Check TCP Port

Problem

  • You want to check if a TCP port is open.
  • You want to use the PowerShell.

Solution

This solution checks, if port 80 on 192.168.2.1 is open.

try{
    $tcp=new-object System.Net.Sockets.TcpClient
    $tcp.Connect("192.168.2.1",80)
    If($tcp.Connected)
    {
        Write-Host "It works"
    }
    else
    {
        Write-Host "It doesnt work"
    }
    $tcp.Close()
}
catch {
  Write-Host "Error"  
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s