A PowerShell script that automatically enables Internet Connection Sharing (ICS) from a TUN network adapter (such as VPN or proxy) to another network interface (such as Ethernet or VMnet).
This script automates the process of sharing an internet connection from a TUN adapter to another network adapter on Windows. It's particularly useful for:
- Sharing VPN/proxy connections created by tools like sing-box, Clash, or v2ray
- Providing internet access to virtual machines through VMnet adapters
- Creating a shared network connection without manual ICS configuration
- Automatically recovering ICS configuration after system restarts
- β Automatic Detection: Waits for and detects TUN adapter automatically
- β Reliable Configuration: Uses Windows COM API for proper ICS setup
- β Clean Startup: Disables conflicting ICS configurations before setup
- β Status Verification: Confirms ICS is properly enabled after configuration
- β Error Handling: Comprehensive error checking and user-friendly messages
- β Customizable: Supports command-line parameters for different adapters
- Operating System: Windows 10/11
- PowerShell: Version 5.1 or higher (pre-installed on Windows)
- Privileges: Administrator rights (required for network configuration)
- Service: SharedAccess service must be available (enabled by default on Windows)
- Download the
autoICS.ps1script - Place it in a convenient location (e.g.,
C:\Scripts\) - (Optional) Unblock the script if downloaded from the internet:
Unblock-File -Path "C:\Scripts\autoICS.ps1"
Run with default settings (shares singbox_tun to Ethernet1):
# Right-click PowerShell β Run as Administrator
.\autoICS.ps1Specify your own adapter names:
.\autoICS.ps1 -tunName "Clash" -shareTo "VMware Network Adapter VMnet8"To find the exact names of your network adapters:
Get-NetAdapter | Select-Object Name, Status, InterfaceDescription| Parameter | Type | Default | Description |
|---|---|---|---|
-tunName |
String | singbox_tun |
Name (or partial name) of the TUN adapter to share from |
-shareTo |
String | Ethernet1 |
Name (or partial name) of the adapter to share to |
Note: The script uses wildcard matching, so you can use partial names. For example, Ethernet will match Ethernet1, Ethernet 2, etc.
To automatically enable ICS when your computer starts:
- Open Task Scheduler (
taskschd.msc) - Click "Create Task" (not "Create Basic Task")
- General tab:
- Name:
AutoICS - Check "Run with highest privileges"
- Configure for: Windows 10/11
- Name:
- Triggers tab:
- New β Begin the task: "At startup"
- Delay task for: 30 seconds (gives time for network adapters to initialize)
- Actions tab:
- New β Action: "Start a program"
- Program/script:
powershell.exe - Arguments:
-ExecutionPolicy Bypass -File "C:\Scripts\autoICS.ps1" - Start in:
C:\Scripts\
- Conditions tab:
- Uncheck "Start the task only if the computer is on AC power"
- Click OK
- Create a batch file
run_autoics.bat:@echo off powershell.exe -ExecutionPolicy Bypass -Command "Start-Process powershell -ArgumentList '-ExecutionPolicy Bypass -File C:\Scripts\autoICS.ps1' -Verb RunAs"
- Press
Win + R, typeshell:startup, press Enter - Place the batch file in the Startup folder
$scriptPath = "C:\Scripts\autoICS.ps1"
$regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
Set-ItemProperty -Path $regPath -Name "AutoICS" -Value "powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$scriptPath`""- Wait for TUN Adapter: Continuously checks for the TUN adapter until it's available and active
- Locate Adapters: Identifies both source and destination network adapters
- Clean Existing Configuration: Disables any existing ICS configurations to prevent conflicts
- Enable IP Forwarding: Configures both adapters to forward IPv4 packets
- Start SharedAccess Service: Ensures Windows ICS service is running
- Configure ICS via COM API:
- Sets TUN adapter as PUBLIC (internet source)
- Sets target adapter as PRIVATE (internet destination)
- Verify Configuration: Confirms ICS is properly enabled on both adapters
Internet Connection (VPN/Proxy)
β
[TUN Adapter] (PUBLIC - Source)
β
Windows ICS Service
β
[Target Adapter] (PRIVATE - Destination)
β
Connected Devices (VMs, etc.)
Problem: "Cannot be loaded because running scripts is disabled"
Solution:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserProblem: Script can't find one or both adapters
Solutions:
- Check adapter names:
Get-NetAdapter - Ensure TUN adapter is active (Status: Up)
- Use partial names that match your adapters
- Check for typos in parameter values
Problem: Script completes but internet sharing doesn't work
Solutions:
- Check Windows Firewall settings
- Verify target adapter is set to obtain IP automatically (DHCP)
- Restart the SharedAccess service manually:
Restart-Service SharedAccess -Force
- Check if antivirus is blocking ICS
- Manually disable ICS on all adapters, then run script again
Problem: Insufficient privileges
Solution: Always run PowerShell as Administrator
Problem: Script waits forever for TUN adapter
Solutions:
- Ensure your VPN/proxy application is running and connected
- Check if TUN adapter is created:
Get-NetAdapter - Verify the TUN adapter name matches the
-tunNameparameter - Some applications create adapters with different names (check documentation)
Problem: Script can't disable existing ICS configuration
Solution: Manually disable ICS:
- Open Network Connections (
ncpa.cpl) - Right-click each adapter β Properties β Sharing tab
- Uncheck "Allow other network users to connect"
- Apply and run script again
Share sing-box TUN connection to VMware virtual machine:
.\autoICS.ps1 -tunName "singbox" -shareTo "VMnet8"Share Clash proxy to physical Ethernet port:
.\autoICS.ps1 -tunName "Clash" -shareTo "Ethernet"Share v2ray connection to Wi-Fi adapter for mobile hotspot:
.\autoICS.ps1 -tunName "v2ray" -shareTo "Wi-Fi"- Administrator Rights: The script MUST be run as Administrator
- Adapter Status: TUN adapter must be connected and active (Status: Up)
- Single ICS: Windows only supports one ICS configuration at a time
- IP Address Changes: The PRIVATE adapter will be automatically assigned IP
192.168.137.1 - DHCP: Connected devices should be set to obtain IP automatically
- Firewall: Windows Firewall rules for ICS are created automatically
- Service Dependency: Requires SharedAccess service (enabled by default)
- ICS exposes your internet connection to other devices/VMs
- Ensure only trusted devices are connected to the shared network
- Consider implementing additional firewall rules if needed
- The TUN adapter's security settings apply to all shared connections
To see detailed information about what's happening:
.\autoICS.ps1 -VerboseCheck COM object status manually:
$netShare = New-Object -ComObject HNetCfg.HNetShare
$connections = $netShare.EnumEveryConnection
foreach ($conn in $connections) {
$props = $netShare.NetConnectionProps($conn)
$config = $netShare.INetSharingConfigurationForINetConnection($conn)
Write-Host "$($props.Name): Sharing=$($config.SharingEnabled), Type=$($config.SharingConnectionType)"
}This script is provided "as-is" without warranty of any kind. Use at your own risk.
Feel free to submit issues, suggestions, or improvements!
If you encounter issues:
- Check the Troubleshooting section above
- Verify all requirements are met
- Run the debug commands to gather information
- Check Windows Event Viewer for SharedAccess service errors
Compatibility: Windows 10/11, PowerShell 5.1+