New NetNat - PowerShell


Overview

New-NetNat creates a new network address translation (NAT) rule for IPv4 or IPv6 traffic on a local computer. NAT rules allow you to translate a private IP address to a public IP address, enabling you to access the internet from a private network.

Syntax

New-NetNat [-Name] <string> [-InterfaceAlias] <string> [-LocalAddressPrefix] <string> [-LocalPort] <uint16> [-ExternalIPAddress] <string> [-ExternalPort] <uint16> [-Protocol] <string> [-PsDscRunAsCredential] <PSCredential> [-PsDscSkipShutdown] [<SwitchParameter>] [-VMName] <string> [-PassThru] [-Confirm] [-WhatIf] [-OutVariable] <string>

Options/Flags

-Name
The name of the NAT rule.

-InterfaceAlias
The alias of the network interface to which the NAT rule applies.

-LocalAddressPrefix
The local address prefix to which the NAT rule applies.

-LocalPort
The local port to which the NAT rule applies.

-ExternalIPAddress
The external IP address to which the NAT rule translates traffic.

-ExternalPort
The external port to which the NAT rule translates traffic.

-Protocol
The protocol to which the NAT rule applies (e.g., TCP, UDP, ICMP).

-PsDscRunAsCredential
The credentials to use when running the command as part of a Desired State Configuration (DSC) script.

-PsDscSkipShutdown
Skips the shutdown of the system after creating the NAT rule, which can be useful when creating NAT rules as part of a DSC script.

-VMName
The name of the hypervisor that will be managed.

-PassThru
Returns the NAT rule object that was created.

-Confirm
Prompts you to confirm each operation.

-WhatIf
Shows what would happen if the command were to run without actually executing it.

-OutVariable
Stores the result of the command in a variable.

Examples

Create a new NAT rule for IPv4 traffic:

New-NetNat -Name "NATRule1" -InterfaceAlias "Ethernet" -LocalAddressPrefix "192.168.1.0/24" -LocalPort 80 -ExternalIPAddress "10.0.0.1" -ExternalPort 80 -Protocol TCP

Create a new NAT rule for IPv6 traffic:

New-NetNat -Name "NATRule2" -InterfaceAlias "Ethernetv6" -LocalAddressPrefix "fe80::/64" -LocalPort 443 -ExternalIPAddress "2001:db8::1" -ExternalPort 443 -Protocol TCP

Common Issues

  • Ensure that the specified network interface exists and is active.
  • Make sure that the local and external IP addresses are valid.
  • Verify that the specified port numbers are not already in use by other processes.
  • Check that the firewall rules allow traffic on the specified ports.

Integration

New-NetNat can be used in conjunction with other PowerShell commands to manage network configuration. For example, you can use Get-NetNat to view existing NAT rules, and Remove-NetNat to delete them.

  • Get-NetNat
  • Remove-NetNat