Route certain traffic via WiFi in Windows – Powershell

Disclaimer:

I WOULD NOT RECOMMEND USING THIS CODE TO CIRCUMVENT FIREWALLS ETC AT YOUR PLACE OF WORK. THE SAMPLE CODE ON BLOG.MONOTOK.ORG IS PROVIDED “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MONOTOK OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Problem:

Sometimes you might need to send certain traffic destined to a FQDN via the WiFi network while the rest of the traffic goes via the Ethernet. An example could be that a certain destination is only reachable via the Ethernet or WiFi but you want the rest of the traffic to go via the other interface.

Windows routing table uses several metrics to decide which interface traffic takes. Normally when both the Ethernet and WiFi are connected, the Ethernet will be preferred over the WiFi; this is decided via the Metric number. This is automatically generated by Windows unless changed, for example the Ethernet will be 10 and the WiFi will be 30. The lower number is preferred. You can also assign a metric to a static route inserted into the routing table however the routes metric is added to the interface metric. This prevents the route overriding the Ethernet even if you make the interfaces the same metric and then remove the WiFi default route (Windows kept inserting it again anyway).

The solution is to manually change the metrics of the interfaces. Change the Ethernet and WiFi metric to 5 and remove the default static routes. Then enter new static routes, WiFi route metric of 5 and Ethernet metric of 3. Then insert the new static route to the specific destination with a metric of 1.

This will give you the follow route metrics:

  • Ethernet 8
  • WiFi 10
  • Specific WiFi Route 6

Obviously this is not quick and simple to configure so I have written a Powershell script. Just open it in the Windows PSE or run it from the Powershell. There are two scripts, the first changes the routing table while the second will revert all changes. Make sure to leave the WiFi and Ethernet connected before running the undo script.

Configure Routes Script

Undo Code

 

Hopefully you will find this code useful.