How to enable Internet Connection Sharing using command line?
I can do it manually by right-clicking on a network connection, opening the Sharing tab and clicking on the "Allow other network users to connect through this computer's Internet connection" check box.
Now I need to automate this task. Is there a command-line tool or a Powershell cmdlet to accomplish this?
Solution 1:
Here is a pure PowerShell solution (should be run with administrative privileges):
# Register the HNetCfg library (once)
regsvr32 hnetcfg.dll
# Create a NetSharingManager object
$m = New-Object -ComObject HNetCfg.HNetShare
# List connections
$m.EnumEveryConnection |% { $m.NetConnectionProps.Invoke($_) }
# Find connection
$c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Name -eq "Ethernet" }
# Get sharing configuration
$config = $m.INetSharingConfigurationForINetConnection.Invoke($c)
# See if sharing is enabled
Write-Output $config.SharingEnabled
# See the role of connection in sharing
# 0 - public, 1 - private
# Only meaningful if SharingEnabled is True
Write-Output $config.SharingType
# Enable sharing (0 - public, 1 - private)
$config.EnableSharing(0)
# Disable sharing
$config.DisableSharing()
See also this question at social.msdn.microsoft.com:
You have to enable the public interface on the adapter you are connecting to and enable sharing on the private interface for the adapter you want to be able to use for the network.
Solution 2:
I have created a simple command line tool for this.
Download and unzip or
git clone [email protected]:utapyngo/icsmanager.git
Build by running
build.cmd
Register the
HNetCfg
COM library:regsvr32 hnetcfg.dll
(it is a standard library located at%WINDIR%\System32
)
Command-line usage
-
Open the command line prompt as administrator
cd
to theicsmanager
directory (oricsmanager-master
if you downloaded zip). -
Type
icsmanager
This should display available network connections. Notice the GUID attribute. To use this tool you need to have at least two connections.
-
Type
icsmanager enable {GUID-OF-CONNECTION-TO-SHARE} {GUID-OF-HOME-CONNECTION}
This should enable ICS.
Powershell usage
-
Import module:
Import-Module IcsManager.dll
-
List network connections:
Get-NetworkConnections
-
Start Internet Connection Sharing:
Enable-ICS "Connection to share" "Home connection"
-
Stop Internet Connection Sharing:
Disable-ICS
Disclaimer: I did not test the tool yet. Use it at your own risk. Feel free to open an issue at GitHub if something does not work. Pull requests are also welcome.
Solution 3:
By my understanding, routing capability was removed from Windows since Vista and is only available now in Windows Server.
The following trick can be found on the Internet to re-enable netsh routing
, which you can try at your own risk.
I suggest first the usual precautions, including creating a system restore point.
- Get IPMONTR.DLL and IPPROMON.DLL from 2003 or from XP
- Copy them to WINDOWS\SYSTEM32
-
Run in Command Prompt (cmd) as administrator :
netsh add helper ipmontr.dll
netsh add helper ippromon.dll
You might also need to set the Routing and Remote Access Service to Automatic startup.
Reboot before trying anything.
Solution 4:
Unfortunately there is no such cmd command for Windows 7 or more, so I used this Visual Basic function to get it done:
Private Function EnableDisableICS(ByVal sPublicConnectionName As String, ByVal sPrivateConnectionName As String, ByVal bEnable As Boolean)
Dim bFound As Boolean
Dim oNetSharingManager, oConnectionCollection, oItem, EveryConnection, objNCProps
oNetSharingManager = CreateObject("HNetCfg.HNetShare.1")
oConnectionCollection = oNetSharingManager.EnumEveryConnection
For Each oItem In oConnectionCollection
EveryConnection = oNetSharingManager.INetSharingConfigurationForINetConnection(oItem)
objNCProps = oNetSharingManager.NetConnectionProps(oItem)
If objNCProps.name = sPrivateConnectionName Then
bFound = True
MsgBox("Starting Internet Sharing For: " & objNCProps.name)
If bEnable Then
EveryConnection.EnableSharing(1)
Else
EveryConnection.DisableSharing()
End If
End If
Next
oConnectionCollection = oNetSharingManager.EnumEveryConnection
For Each oItem In oConnectionCollection
EveryConnection = oNetSharingManager.INetSharingConfigurationForINetConnection(oItem)
objNCProps = oNetSharingManager.NetConnectionProps(oItem)
If objNCProps.name = sPublicConnectionName Then
bFound = True
MsgBox("Internet Sharing Success For: " & objNCProps.name)
If bEnable Then
EveryConnection.EnableSharing(0)
Else
EveryConnection.DisableSharing()
End If
End If
Next
Return Nothing 'bEnable & bFound
End Function
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
EnableDisableICS("YOUR ACTIVE NETWORK", "YOUR ADAPTOR TO SHARE", True)
End Sub
Please note that """" is required. Have fun.