a powershell script to set port forwarding to wsl2 and automatically add firewall rules
In an elevated powershell window
set-executionpolicy remotesignedAdding or removing port forwarding and firewall rules requires an elevated PowerShell window. Listing distros, distro addresses, and existing rules can be used without elevation.
Run the script without parameters to open the interactive menu. The menu can list installed WSL distros, show distro IPv4 addresses, add or remove forwarding rules, and show existing portproxy/firewall rules.
.\WSL-PortForwarding.ps1The script discovers IPv4 addresses dynamically from the selected WSL distro. It does not require a specific interface name such as eth0.
.\WSL-PortForwarding.ps1 -DistroName "YourWslDistro" -LocalPort 8080 -WslPort 80You can also provide the target WSL IP address explicitly.
.\WSL-PortForwarding.ps1 -DistroName "YourWslDistro" -WslIp 172.24.32.10 -LocalPort 8080 -WslPort 80.\WSL-PortForwarding.ps1 -DistroName "YourWslDistro" -LocalPort 8080 -WslPort 80 -Purge.\WSL-PortForwarding.ps1 -ShowRules