Problem scenario
You are using Windows 7 and PowerShell version 3. You want to test if a port is open to a given IP address. (If you are using a modern version of PowerShell, use the Test-NetConnection command.) How do you find out if a port of an IP address is open or not?
Solution
Use the PowerShell function Test-TCPPortConnection written by Jonathan Medd. Every line of this code except the last line was taken from here.
function Test-TCPPortConnection {
<#
.SYNOPSIS
Test the response of a computer to a specific TCP port
.DESCRIPTION
Test the response of a computer to a specific TCP port
.PARAMETER ComputerName
Name of the computer to test the response for
.PARAMETER Port
TCP Port number(s) to test
.INPUTS
System.String.
System.Int.
.OUTPUTS
None
.EXAMPLE
PS C:\> Test-TCPPortConnection -ComputerName Server01
.EXAMPLE
PS C:\> Get-Content Servers.txt | Test-TCPPortConnection -Port 22,443
#>
[CmdletBinding()][OutputType('System.Management.Automation.PSObject')]
param(
[Parameter(Position=0,Mandatory=$true,HelpMessage="Name of the computer to test",
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
[Alias('CN','__SERVER','IPAddress','Server')]
[String[]]$ComputerName,
[Parameter(Position=1)]
[ValidateRange(1,65535)]
[Int[]]$Port = 3389
)
begin {
$TCPObject = @()
}
process {
foreach ($Computer in $ComputerName) {
foreach ($TCPPort in $Port) {
$Connection = New-Object Net.Sockets.TcpClient
try {
$Connection.Connect($Computer,$TCPPort)
if ($Connection.Connected) {
$Response = “Open"
$Connection.Close()
}
}
catch [System.Management.Automation.MethodInvocationException]
{
$Response = “Closed / Filtered"
}
$Connection = $null
$hash = @{
ComputerName = $Computer
Port = $TCPPort
Response = $Response
}
$Object = New-Object PSObject -Property $hash
$TCPObject += $Object
}
}
}
end {
Write-Output $TCPObject
}
}
Test-TCPPortConnection -ComputerName x.x.x.x -Port 80 # End of code
Replace x.x.x.x with the IP address you want to test. Replace 80 with the given port you want to test. You do not have to save the file to run it in the PowerShell ISE application. Run the code and examine the results. You will see if a given port is open or not (closed or filtered).