This article explains how Powershell script to check a port is open or not on any windows computers or server operating system to make sure the port network traffic is flowing or application to work as expected.
If you are working as a system admin for a large IT enterprise you may come across to test a port is open or not on a server or multiple remote computers. Generally, some applications depend on the port, and their required port should be opened on the computer to establish an outgoing communication.
we can check any port number status as opened or not using Powershell script, here is the simple script that pulls the port number status.
we are going to use a Powershell command Test-NetConnection to test the port number status.
Example 1
Checking the website opening status on a local machine
Test-NetConnection -ComputerName "techdiip.com" -InformationLevel "Detailed"
Here is the output of this single line of code, it says the website techdiip.com is accessible from the local machine.
PS C:\Windows\system32> Test-NetConnection -ComputerName "techdiip.com" -InformationLevel "Detailed" ComputerName : techdiip.com RemoteAddress : 45.87.80.88 NameResolutionResults : 45.87.80.88 InterfaceAlias : Wi-Fi SourceAddress : 192.168.1.10 NetRoute (NextHop) : 192.168.1.1 PingSucceeded : True PingReplyDetails (RTT) : 146 ms
Let’s test a port number status.
Example 2
Test-NetConnection -ComputerName "techdiip.com" -Port 80
Here is the output and it says, the port 80 is opened and accessible. Basically I’m checking the parameter value TcpTestSucceeded is True or not.
S C:\Windows\system32> Test-NetConnection -ComputerName "techdiip.com" -Port 80 ComputerName : techdiip.com RemoteAddress : 45.87.80.88 RemotePort : 80 InterfaceAlias : Wi-Fi SourceAddress : 192.168.1.6 TcpTestSucceeded : True
We can also test port status using the parameter -commonTCPPort, here is the output.
Test-NetConnection -ComputerName "techdiip.com" -CommonTCPPort http
PS C:\Windows\system32> Test-NetConnection -ComputerName "techdiip.com" -CommonTCPPort http ComputerName : techdiip.com RemoteAddress : 45.87.80.88 RemotePort : 80 InterfaceAlias : Wi-Fi SourceAddress : 192.168.1.6 TcpTestSucceeded : True
Powershell check port is open or not on remote computer
We need to create a text file for remote computers list that we are going to query the port status, here I have created a text file named as allservers.txt and saved it in location C\temp\allservers.txt
$Allservers = Get-Content "C:\temp\Allservers.txt"
#test the port number 80
foreach($Server in $Allservers)
{
$porttest = Invoke-Command -ComputerName $server -ScriptBlock { Test-NetConnection -port 80}
Write-Host "The port Status on" $Server "Computer" $portest.tcptestsucceeded
}
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
I hope you find this article useful, please comment for any questions.
Leave a Reply Cancel reply