PowerShell – Checking whether a TCP port is open using PowerShell

Sometimes when working with customers you need to validate if a particular port has been opened on their Firewall. I am unable to count how many times I’ve had a discussion where a port hasnt been opened. So for simplicity the following PowerShell function allows you to check instantly;

Function Get-CheckPort{
Param (
# End of Parameters
  $Date = Get-Date
  $MyDate =  " " + $Date.Day + "." + $Date.Month + "." + $Date.Year + " " + $date.Hour + ":" + $date.Minute
     $connection = $null
     $connection = New-Object System.Net.Sockets.TcpClient($ipaddress, $port)

       Write-host "Warning: Port $port is closed - $MyDate" -ForegroundColor Red
  if ($connection.Connected){
     Write-Host "Info: Port $port is open - $MyDate" -ForegroundColor Green
Get-CheckPort -ipaddress '' -port ''

As you can see from the test above, I am to validate that port 80 to the teams.microsoft.com is open

The Author – Blogabout.Cloud

Leave a Reply

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