[Powershell] – TCP connexion with Net.TCPClient

Click to rate this post!
[Total: 0 Average: 0]

Powershell – how to make a TCP connection ?
How to use System.Net.Sockets.TCPClient ?

Here a powershell script to connect to a TCP port on a host (localhost or remote). 

Useful for checking the proper functioning of a firewall. the connection script on port tcp in question uses the object System.Net.Sockets.TcpClient

$ip = “”
$port = “80”
$connection = New-Object -TypeName System.Net.Sockets.TcpClient -ArgumentList $ip,$port $connection.Connected
if ($connection.Connected) {Write-Host$ip : $port connected” -ForegroundColor Green}
else {Write-Host “$ip : $port not connected” -ForegroundColor Red}

never forget to close your connection 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *