Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
3  
Can't try this just now, but you might want to try running Process Monitor and pointing it at your Registry. See what keys/values change when you toggle & apply the setting, then write your script accordingly. –  Iszi Sep 17 '12 at 20:12
    
Related question/info over on StackOverflow: Enable Internet Connection Sharing programmatically –  Ƭᴇcʜιᴇ007 Mar 17 '13 at 17:35
    
The Microsoft-Windows-SharedAccess Unattended Windows Setup component is so close, but it only works at Windows Setup! technet.microsoft.com/en-us/library/ff715511.aspx –  Jacob Krall Sep 20 '13 at 20:26
    
Seriously, it don't make much sense to do ICS with command lines except WIFI sharing, which is a different story. you didn't need to share and unshare, nor do mass deployment. –  Antony Lee Sep 25 '13 at 12:22

5 Answers 5

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.

  1. Get IPMONTR.DLL and IPPROMON.DLL from 2003 or from XP
  2. Copy them to WINDOWS\SYSTEM32
  3. 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.

share|improve this answer
1  
Is it legal to copy files from XP if I don't have XP license? –  utapyngo Sep 24 '13 at 13:22
1  
Tried this on Windows 7 64bit. Copied the files from XP 64 bit. Running as administrator. Errors: The following helper DLL cannot be loaded: IPMONTR.DLL. The following helper DLL cannot be loaded: IPPROMON.DLL. –  utapyngo Sep 24 '13 at 14:14
1  
I tried it too the "add helper" and I am sorry to confirm that it doesn't work for 64-bit. As regarding the legality of transplanting dlls to which you have right on both OSs, this is unclear. You don't have the right to modify Windows files, but copying them is maybe not ruled against in the XP license (it would astound me that Microsoft could have foreseen this in advance). –  harrymc Sep 24 '13 at 18:30
1  
It should be mentioned in the answer that it does not work for 64-bit Windows. Please also mention that the files should be copied from XP 32-bit. –  utapyngo Sep 25 '13 at 1:35
    
@utapyngo: One cannot be sure that there is really is no way to make this work on 64-bit. According to my studies, the problem is that more dlls than these two should be copied, but analyzing fully the problem requires more time than I can give. –  harrymc Sep 25 '13 at 5:47

I have created a simple command line tool for this.

  1. Download and unzip or git clone git@github.com:utapyngo/icsmanager.git

  2. Build by running build.cmd

  3. Register the HNetCfg COM library: regsvr32 hnetcfg.dll (it is a standard library located at %WINDIR%\System32)

Command-line usage

  1. Open the command line prompt as administrator

    cd to the icsmanager directory (or icsmanager-master if you downloaded zip).

  2. Type icsmanager

    This should display available network connections. Notice the GUID attribute. To use this tool you need to have at least two connections.

  3. Type icsmanager enable {GUID-OF-CONNECTION-TO-SHARE} {GUID-OF-HOME-CONNECTION}

    This should enable ICS.

Powershell usage

  1. Import module:

    Import-Module IcsManager.dll

  2. List network connections:

    Get-NetworkConnections

  3. Start Internet Connection Sharing:

    Enable-ICS "Connection to share" "Home connection"

  4. 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.

share|improve this answer
    
Neat. This uses an external binary called NETCONLib; where did it come from? What does it do? –  Jacob Krall Sep 23 '13 at 15:09
    
Aha. This is a COM class wrapper for the "NetCon 1.0 Type Library" included with Windows. –  Jacob Krall Sep 23 '13 at 15:18
    
@JacobKrall, yes, it is located at C:\Windows\System32\hnetcfg.dll. –  utapyngo Sep 23 '13 at 15:53
    
This looks like it will do exactly what I want - I will try it out. –  Jacob Krall Sep 23 '13 at 16:13
1  
Sorry, I did it several months ago and forgot that HNetCfg should be registered manually with regsvr32 hnetcfg.dll. And I learnt about hnetcfg.dll at msdn.microsoft.com/en-us/library/windows/desktop/… –  utapyngo Sep 24 '13 at 14:16

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.

share|improve this answer
    
Ooh, I didn't know you could do COM interop with PowerShell! I assume you need some [System.Runtime.Interopservices.Marshal]::ReleaseComObject(...) sprinkled in. –  Jacob Krall Sep 23 '13 at 16:19
    
The call to EnableSharing is throwing this exception, even though I'm running PowerShell as Administrator:Exception: Exception calling "EnableSharing" with "1" argument(s): "An event was unable to invoke any of the subscribers (Exception from HRESULT: 0x80040201)" --> Exception has been thrown by the target of an invocation. --> An event was unable to invoke any of the subscribers (Exception from HRESULT: 0x80040201) –  Jacob Krall Sep 23 '13 at 20:36
    
Try running regsvr32 hnetcfg.dll as administrator manually. –  utapyngo Sep 24 '13 at 2:19
    
I wonder: what does "enable the public interface on the adapter" mean? –  Jacob Krall Sep 24 '13 at 16:36
    
I think it means .EnableSharing(0) but you have already figured it out. –  utapyngo Sep 25 '13 at 11:18

A former colleague of mine used to do this via the windows own tool netsh. As i have never done this myself, i can ownly advice you to have a look at the microsoft netsh documentation.

As I can remember it was pretty much a pitty and lots of netsh calls where needed to do so but it worked in the end...

share|improve this answer
5  
It was possible to do with netsh routing in Windows XP but in Windows 7 they have removed that command. That's why I'm only asking about Windows 7. –  utapyngo Sep 18 '12 at 10:10

The following should work

netsh routing ip autodhcp install
netsh routing ip autodhcp set interface name="Local Area Connection(or whereever your internet connection is from)" mode=enable
netsh routing ip autodhcp set global 192.168.0.1 255.255.255.0 11520
share|improve this answer
8  
It was possible to do with netsh routing in Windows XP but in Windows 7 they have removed that command. That's why I'm only asking about Windows 7. –  utapyngo Jan 3 '13 at 4:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.