PowerShell: Testing the connectivity to the Default Gateway on localhost and Remote Hosts by reading the Routing Table

How to test the connection to the default gateway without knowing the IP-Address of that Gateway?


Imagine you are logged on a Windows Computer Server01. You want to check if Server02 has a connection to it’s gateway. Sure, you could run Test-Connection with the Source Parameter and do a remote ping. But you don’t know the IP-Address of server02’s gateway. There are 2 possibilities: a) Connect to Server02 and run ipconfig to find out Server02’s gateway address or  b) Keep on reading my article  🙂

For checking the reachability of the localhost’s gateway, run the following One-Liner:

Test-Connection (Get-NetRoute -DestinationPrefix | Select-Object -ExpandProperty Nexthop) -Quiet -Count 1


True, everything is fine.

Explanation: The IP-Address of the Default Gateway can be found in the routing table. The route is called Default Route or Gateway of Last Resort. And that route with it´s next hop address is very important for the next step.


Testing the Gateway of Remote Hosts without knowing the IP-Address of the Gateway

One further thought: Why not use the -Source Parameter and combine it with our One-Liner? So I started playing … Is it possible to test the connection to server03’s gateway without knowing the IP-Address of server03’s gateway? Sounds crazy, but: Yes, it is.

I am logged on server dc01 (dc01 and server03 share the same domain) and do the following:

$Name=Read-Host "Computername?"
$Test=Invoke-Command -ComputerName $Name {Get-NetRoute -DestinationPrefix | Select-Object -ExpandProperty Nexthop}
Test-Connection -Source $Name -Destination $Test -Count 1


Nice. The IP-Address of server03’s gateway is And yes, server 03 can reach it’s gateway. I don’t care about the IP-Address. 😉 The default route always gives me the IP-Address of the Default Gateway, regardless of the number of network cards and configured IP-Addresses.

For more about Test-Connection see my article The modern version of ping: Test-Connection.

Have fun playing with Test-Connection and Default Routes!

Categories: PowerShell

Tagged as: ,

7 replies »

  1. Nice idea, but I don’t understand, why you need to include the source parameter for invoking the cmdlets on Server03. You’re already executing it on Server03, aren’t you?


    • Thank you for your kind words. I was logged on server dc01. See “I am logged on server dc01 (dc01 and server03 share the same domain) and do the following: ……..” Regards, P


      • OK, now I understand.
        I thought you would execute “Test-Connection” via Invoke-Command as well. You would even save a few keystrokes:

        $Name=Read-Host “Computername?”
        Invoke-Command -ComputerName $Name {$Test=Get-NetRoute -DestinationPrefix | Select-Object -ExpandProperty Nexthop; Test-Connection -Destination $Test -Count 1}

        Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.