Similar Question: How to tell if Remote Desktop service is running and available?

I've searched around for answers to the problem of "How do I ping a specific port?" and found several solutions. Even before searching, I was well aware that "ping" is not even remotely the proper tool for the job, but it's the closest tool in terms of functionality for what I'm looking to do.

Most solutions to the "ping a port" problem rely on nmap or other non-native tools, and generally they're written such that they're only useful for a one-time check. What I want to do is to be able to monitor a port on a remote host continuously, so that I can be alerted to when the service on that port is available. As with the question above, the most common use case I have for this is to check a computer for RDP availability after a reboot (ping alone is not a reliable indicator of whether or not the system is finished booting and starting all services).

The key difference here is that I'm looking to do this with tools that are native to Windows. I never know quite when or where it will be that I'll want to monitor a remote system for service availability, and I don't always have my portable tools handy when it does happen, so it would be ideal if there is a relatively simple one-liner using built-in CMD or PowerShell utilities that could do the job.

I do have some systems with Windows 8 or Server 2012, and PowerShell 4.0, but solutions which are backward-compatible to Windows 7 and Server 2008, with PowerShell 2.0, would be ideal. (Note: PowerShell versions are given for reference only - any command or short script that will work on a fresh install of the given Windows versions, without needing additional software, will do.)


If you are going to do the check interactively, you can use the TELNET client by entering "telnet host port", for example "telnet 80" to access the HTTP port.

Please note that the telnet client is not installed per default since Windows 7 / Windows 2008, so you need to activate the tool in control panel -> Add/Remove Software -> Windows components.

  • This falls under what I referred to as tools that are only useful for a one-time check. I'm looking for a fire-and-forget solution - something that will continuously run and either update me on the status of the port on a regular basis or just let me know when it finally comes back up. – Iszi Aug 27 '14 at 13:23

In Windows 8, you can use Test-NetConnection to gain similar functionality to the classic ping and tracert tools, as well as check the status of a remote system's port. Unfortunately, the options for Test-NetConnection are relatively limited in comparison to the tools it replaces. So, it alone will not serve well for a continuous monitor. Fortunately, PowerShell is very scriptable. Here's a one-liner (technically, several lines condensed to one) which will continuously test a port for availability, and print time-stamped results to the console.

cls;1..8|%{""};for(){$x=tnc -Po 80;"$(Get-Date) $($x.TcpTestSucceeded)"}

Here's a screenshot of the script in action. You can see here why I started the script with cls;1..8|%{""}; even though that's technically not needed. Also, you can see that Test-NetConnection takes about 9-10 seconds to run each time even when you're on a LAN connection.

enter image description here

Here's a commented, multi-line version of the script. This is the same code, just spread out with a walkthrough included.

# CLS is a built-in alias for Clear-Host.
# This clears any pre-existing output from the console so we can start ours from the top.


# This takes the integers from 1 through 8 and pipes them to a ForEach-Object loop.
# (The percent symbol, "%", is a built-in alias for ForEach-Object.)
# Putting just a pair of double-quotes in the script block outputs a single blank line.
# Effectively, this line of script just outputs 8 blank lines.
# While Test-NetConnection runs, it puts a status display on the top 8 lines of the console.
# So, we're using this to make our output start below that level in order to be visible.


# This begins an infinite for loop. It will run until aborted by the user.
# (e.g.: With CTRL+C)
# Note: Due to the way Test-NetConnection operates, the abort may take a few seconds to process.


    # TNC is a built-in alias for Test-NetConnection.
    # -Po is a shorthand for the -Port parameter name.
    # PowerShell allows shortening of parameter names down to as few characters are needed to uniquely identify the parameter.
    # This tests for connectivity to port 80 at and puts the results in $x.

    $x=tnc -Po 80;

    # The last step here is to output a timestamp, and the results.
    # Double-quotes allow for per-processing certain elements before including them in an output string.
    # Encapsulating script blocks with $(), within the double-quotes, lets us put their results directly in the output string.
    # So, the first part gets the date and time for the start of the output.
    # Then, with a space to separate it, the TcpTestSucceeded property of $x is retrieved and put at the end.

    "$(Get-Date) $($x.TcpTestSucceeded)"

It's not quite as clean and simple as I'd like, but it does the job. I definitely don't expect to be memorizing it any time soon, but once you understand the commands and PowerShell fundamentals that make it work, it's relatively easy to re-construct on the fly.

For something a bit simpler, that just keeps checking the port and stops to let you know when it's up, you can use this:

while((tnc -Po 80).TcpTestSucceeded -eq $False){};Get-Date

Here, we're using a while loop to keep re-testing the port for as long as it's down. Once a connection to the port is successful, the while loop exits and Get-Date will report the time. You'll also see some warning messages from Test-NetConnection for as long as the loop is running - this is because it uses the Warning output channel to report when a ping or port connection fails, before it actually sends the full results as normal output.

Unfortunately, Test-NetConnection is not available on systems running less than Windows 8. So, this also lacks the cross-compatibility I was hoping for. Still, it's better than nothing for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.