This PowerShell script, PortForwarder.ps1
, allows you to easily create, manage, and delete port forwarding rules within your Windows environment for accessing services running inside your WSL2 distribution. It leverages the netsh
command to establish port mappings and provides a user-friendly interface for convenient interactions. 🎉
- Add Port Mapping: Create new port forwarding rules, specifying the local listening port, local listening IP address, remote forwarding port, and remote forwarding IP address (defaults to WSL2 IP). 🗺️
- Remove Port Mapping: Delete existing port forwarding rules by selecting the rule from a displayed list. 🗑️
- List Port Mappings: View the current list of established port forwarding rules. 📑
- Retry Port Mapping: Remap a selected port forwarding rule by deleting and re-adding it, providing the opportunity to update forwarding settings. 🔄
- User-Friendly Interface: Interactive menus guide you through the process, requiring minimal input from the user. 🧭
- PowerShell installed. 💻
- PowerShell execution policy set to
RemoteSigned
orUnrestricted
orBypass
. 🔐 - WSL2 distribution installed if you want to forward local port to WSL2. 🐧
- Copy the
PortForwarder.ps1
script to your desired location. 📥 - Open PowerShell as administrator. 🔐
- Navigate to the directory where you saved the script. 📁
- Run the script by typing
.\PortForwarder.ps1
and pressing Enter. 🏃
The script presents a main menu with options for:
- Create new port forward: Add a new port mapping. ➕
- Delete port forward: Remove an existing port mapping. ➖
- List port forward: View all current port mappings. 👀
- Remap port forward: Update the settings of an existing port mapping. 🔁
- Exit: Close the script. 🚪
To forward port 8080
on your Windows machine to port 80
running on your WSL2 distribution, you would:
- Choose option 1 (Create new port forward) from the main menu.
- Enter the local listening port as
8080
. - Enter the local listening IP address (optional, default is
0.0.0.0
). - Enter the remote forwarding port as
80
. - Enter the remote forwarding IP address (optional, default is the WSL2 IP).
- The script assumes your WSL2 distribution is running and has a service listening on the specified port.
- Remember to disable port forwarding rules when you no longer need them.
- For more information on port forwarding in Windows, refer to the official Microsoft documentation. 📖
Contributions to improve this script are welcome. Feel free to fork the repository and submit pull requests. 🙌