Create a NAT Network On Hyper-V and Windows 10 Using PowerShell

In this blog post, I will show you how to create a NAT network on a Hyper-V host that is running on a Windows 10 machine using PowerShell.

NAT

Before we start with the PowerShell cmdlets, let’s first understand why we need a NAT network in the first place.

The reason we need a NAT network is that we need to isolate the Virtual Machines that are running on our host and at the same time give them access to the internet (most important).

Without a NAT network, we will need to put our VMs on the same network out Hyper-V host is running and use the same subnet and default gateway.

With a NAT we can use a totally different network on our VMs without worrying about having the VMs on the same Hyper-V host network.

Demo and Code

In my case, I am going to use the following in order to achieve my goal of setting up a NAT network with access to the internet.

  1. Create an internal VM Virtual Switch called NAT.
  2. Get the interface index number of the new switch (NAT)
  3. Create and an assign an IP address to the new network and default gateway of the new NAT network (192.168.0.1)  and 192.168.0.0/24
  4. Configure NAT network with subnet details
  5. Change VMs on the Hyper-V host to use new NAT network

Notes: In my case, the NAT network will be 192.168.0.0/24 and the gateway will be 192.168.0.1 so the VMs will need to set up with an IP address in that network and point to 192.168.0.1 as their GW.  Step 6 is optional and you can also use the Hyper-V console to change the VM network.

#Create switch
New-VMSwitch –SwitchName “NAT” –SwitchType Internal –Verbose
# Get ifindex of new switch
Get-NetAdapter
#Create gateway
New-NetIPAddress –IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex 10 –Verbose
#Create NAT Network
New-NetNat –Name NATNetwork –InternalIPInterfaceAddressPrefix 192.168.1.0/24 –Verbose
#Change VMs to use new NAT switch
Get-VM | Get-VMNetworkAdapter | Connect-VMNetworkAdapter –SwitchName “NAT"

Processing…
Success! You're on the list.

Leave a Reply

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