Sign up ×
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
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! – 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. – user218473 Sep 25 '13 at 12:22

7 Answers 7

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)

# Disable sharing

See also this question at

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

I have created a simple command line tool for this.

  1. Download and unzip or git clone

  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.


    This should enable ICS.

Powershell usage

  1. Import module:

    Import-Module IcsManager.dll

  2. List network connections:


  3. Start Internet Connection Sharing:

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

  4. Stop Internet Connection Sharing:


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
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… – utapyngo Sep 24 '13 at 14:16

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
Is it legal to copy files from XP if I don't have XP license? – utapyngo Sep 24 '13 at 13:22
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
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
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

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
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 11520
share|improve this answer
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

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 = sPrivateConnectionName Then
            bFound = True
            MsgBox("Starting Internet Sharing For: " &
            If bEnable Then
            End If
        End If
    oConnectionCollection = oNetSharingManager.EnumEveryConnection
    For Each oItem In oConnectionCollection
        EveryConnection = oNetSharingManager.INetSharingConfigurationForINetConnection(oItem)
        objNCProps = oNetSharingManager.NetConnectionProps(oItem)
        If = sPublicConnectionName Then
            bFound = True
            MsgBox("Internet Sharing Success For: " &
            If bEnable Then
            End If
        End If
    Return Nothing 'bEnable & bFound
End Function  

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End Sub

Please note that """" is required. Have fun.

share|improve this answer

Based on what I have read, if those that have posted said netsh doesn't work starting at 7 and up- that is incorrect. Now if it's strictly about "netsh routing", I guess you could be right, but this does work- I am about to show the contents of a batch file I have created that workson Windows 8.1. Instead of getting the usual comments and pieces of information, I am going to try and help those with the full information.

First, you need to make sure the connection you will be sharing is set to actually share the connection. This link here should get you going for that:

  1. Open Network Connections by clicking the Start button Picture of the Start button, and then clicking Control Panel. In the search box, type adapter, and then, under Network and Sharing Center, click View network connections.

  2. Right-click the connection that you want to share, and then click Properties. Administrator permission required If you're prompted for an administrator password or confirmation, type the password or provide confirmation.

  3. Click the Sharing tab, and then select the Allow other network users to connect through this computer’s Internet connection check box.

After you've followed the steps above to set up ICS on the host computer, make the following changes on all of the other computers (but not on the host computer).

  1. Open Internet Options by clicking the Start button Picture of the Start button, clicking Control Panel, clicking Network and Internet, and then clicking Internet Options.

  2. Click the Connections tab, and then click Never dial a connection.

  3. Click LAN Settings.

  4. In the Local Area Network (LAN) Settings dialog box, Under Automatic configuration, clear the Automatically detect settings and Use automatic configuration script check boxes.

  5. Under Proxy server, clear the Use a proxy server for your LAN check box, and then click OK.

To my knowledge, I think this should work for both Windows 7 and 8.

Now since the topic was about a command line solution, this is the batch file contents of how I get a virtual wireless adapter configured and ready to go.

Once it's created, you may have to use the instructions above and make sure you are sharing the source connection with the newly created virtual adapter that will be seen by your wireless devices.

Connection sharing .bat file:

@echo off
set _my_datetime=%date%_%time%
set _my_datetime=%_my_datetime: =_%
set _my_datetime=%_my_datetime::=%
set _my_datetime=%_my_datetime:/=_%
set _my_datetime=%_my_datetime:.=_%

    if NOT EXIST "C:\TEMP\switch.txt" (
        GOTO :START
    ) ELSE (
        GOTO :STOP

REM Create Temp File for On and Off switch.
ECHO WOOHOO >"C:\TEMP\switch.txt"

REM -- Output everything that is happening into a file called wifi.txt.
REM -- Start out with a timestamp at the top to show when it was done.
REM -- All 'netsh' commands are for setting up the SSID and starting the    sharing.
REM -- I stop and start when starting the service just for prosperity.

echo _%_my_datetime% >"C:\TEMP\wifi.txt"
netsh wlan set hostednetwork mode=allow ssid=ITWORKS key=111222333 >>    "C:\TEMP\wifi.txt"
netsh wlan stop hostednetwork >>"C:\TEMP\wifi.txt"
netsh wlan start hostednetwork >>"C:\TEMP\wifi.txt"
echo MSGBOX "Wifi Sharing Started!" > %temp%\TEMPmessage.vbs
call %temp%\TEMPmessage.vbs
del %temp%\TEMPmessage.vbs /f /q

REM -- This will turn ICS off and give a prompt via VBS that you're turned off.
REM -- I timestamp when the service is turned off in the output file.
REM -- I delete the switch file to let the code know to turn it on when
REM -- when fired off again.  Tempmessage is the msgbox used to show the service
REM -- has been turned off.  Same for the msgbox above when it's on.

echo OFF AT _%_my_datetime% >>"C:\TEMP\wifi.txt"
netsh wlan stop hostednetwork >>"C:\TEMP\wifi.txt"
DEL /Q "C:\TEMP\switch.txt"
echo MSGBOX "Wifi Sharing Stopped!" > %temp%\TEMPmessage.vbs
call %temp%\TEMPmessage.vbs
del %temp%\TEMPmessage.vbs /f /q


I'll be more than happy to answer questions about this because there is going to be some unique situations and I'd like to help since I had to piece together what I found above.

But to bring this to perspective, this works on Windows 8.1 using an Ethernet connection into a laptop sharing its connection to the virtual adapter. It may work just as well if you are trying to share a source Wireless connection.

share|improve this answer
Welcome to Super User! Thanks for the detailed answer, I have edited to your question to streamline some of the text and to put your bat file contents in a code block. You can see other formatting instructions if there are any problems with it- please check to make sure I haven't altered the meaning of the code. – bertieb Aug 2 at 18:43
Yeah that's fine and thanks. I knew I should have done a better job on the formatting. Nice touch on the MS link to prevent from having to actually go there. – user2562950 Aug 3 at 19:05
Don't worry about it, you'll pick up the markdown syntax quickly enough :) Including the link content is part of a policy here on answering questions - links can go stale, change or disappear. This is less likely in the case of Microsoft, but still very possible. It is good practice to put everything necessary for a solution in the answer itself. Look forward to seeing more good answers from you! – bertieb Aug 3 at 20:42

Your Answer


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.