PowerShell Function: Testing the Default Gateway with Test-Gateway

Sometimes it could happen that your computer lost connection to other hosts or the internet. What is the first you should do? For example, you could try to reach other hosts or your default gateway. Sure, you could use the bottom up method by climbing up the ladder of the OSI Model and check your network cable (Layer 1), climb up again (Layer 2) and check your network card or your switch or you could use the other way and climb down.

In this blog post we climb down and check the Default Gateway by using a PowerShell function. I’ve created a small function which reads the IP-Address of the Default Gateway from the routing table of the localhost. Let’s start.

Gathering Default Gateway’s IP-Address

The following gives me the IP of my Default Gateway:

Get-NetRoute -DestinationPrefix


Note, that it always gives me the IP of the Default Gateway, regardless of how many network cards are installed and configured. It’s called Default Route ( or Last Gateway of Resort. Well, my Default Gateway’s IP is Let’s proceed to the next step.

Discovering Test-Connection

In my article The modern version of ping: Test-Connection I’ve played with the cmdlet Test-Connection. Test-Connection is like ping.  For the following we need Test-Connection to test the Gateway’s IP. First, we could try to ping our gateway.

Test-Connection -ComputerName


Nice. But that is not what we are looking for. We want to call the IP from the Routing Table. For this, we must expand the property of NextHop to get just the IP-Address.

Get-NetRoute -DestinationPrefix | Select-Object -ExpandProperty NextHop


So, we know that we can insert this command into Test-Connection.

Test-Connection -ComputerName (Get-NetRoute -DestinationPrefix | Select-Object -ExpandProperty NextHop)


Why did I put Get-NetRoute in round brackets? Commands in round brackets are executed immediately. Therefore, when ping starts, the Gateway’s IP-Address is already there!

Great! Let’s start writing the function.

Creating a function to test the connectivity to the Default Gateway

Ok, my goal is to create a command Test-Gateway. Let’s create a function that will match my plan. I also want that the output shows only true or false.

function Test-Gateway {Test-Connection -ComputerName (Get-NetRoute -DestinationPrefix | Select-Object -ExpandProperty NextHop) -Quiet}


Now the exciting moment is coming …



Hooray! It’s working.

Making it permanent

Functions you’ve created are only available in your current PowerShell Session, which means that they are gone when you close PowerShell.

If you like my function, simply insert it in your PowerShell profile. Then it will be available every time you start the best command line on this planet. Copy the following code in PowerShell to allow PowerShell running your profile file, to create the profile and to put the function in it.

Set-ExecutionPolicy remotesigned -force
New-Item $profile -Force | Out-Null
Add-Content $profile -Value {function Test-Gateway {Test-Connection -ComputerName (Get-NetRoute -DestinationPrefix | Select-Object -ExpandProperty NextHop) -Quiet}}

The command has now become permanent. It’s included in your profile which will load every time you start PowerShell.

notepad $profile


Close PowerShell and re-open it. Run Test-Gateway. Have fun!

Testing the Gateway on Remote Hosts

In one of my other articles I’ve shown an advanced function with remote action in it. It connects to other hosts, checks the routing table of that host and then checks if this host can reach it’s Default Gateway.

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

PowerShell Module

You can also save this function as a PowerShell Module which I’ve described in a previous blog post: PowerShell Functions: How to create your first PowerShell Module Command

See also

How to create PowerShell Profiles

Ping: Request timed out vs. Destination Host unreachable

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 )

Twitter picture

You are commenting using your Twitter 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.