Skip to main content
Applied syntax highlighting and correct markdown for weblinks ; Removed dead link ; Grammatical corrections
Source Link
JW0914
  • 8.2k
  • 7
  • 30
  • 50
 

On earlier OS versions, we had a batch method netsh routing could be used, but this no longer works in W7. Have a look at this powershell snip and see if it can help. Otherwise, I bet somewhere at stackoverflow is a C# exampleWin7.

# 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()

http://earthwithsun.com/questions/470319/how-to-enable-internet-connection-sharing-using-command-line

How to enable Internet Connection Sharing using command line?

  • Have a look at this Powershell snip, otherwise StackOverflow likely has a C# example:
    # 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, only meaningful if SharingEnabled is True
      # 0: Public || 1: Private
      Write-Output $config.SharingType
    
    # Enable sharing
      # 0: Public || 1: Private
      $config.EnableSharing(0)
    
    # Disable sharing
      $config.DisableSharing()
    
  • How to enable Internet Connection Sharing using command line?

On earlier OS, we had a batch method netsh routing but this no longer works in W7. Have a look at this powershell snip and see if it can help. Otherwise, I bet somewhere at stackoverflow is a C# example.

# 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()

http://earthwithsun.com/questions/470319/how-to-enable-internet-connection-sharing-using-command-line

How to enable Internet Connection Sharing using command line?

 

On earlier OS versions, netsh routing could be used, but this no longer works in Win7.

  • Have a look at this Powershell snip, otherwise StackOverflow likely has a C# example:
    # 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, only meaningful if SharingEnabled is True
      # 0: Public || 1: Private
      Write-Output $config.SharingType
    
    # Enable sharing
      # 0: Public || 1: Private
      $config.EnableSharing(0)
    
    # Disable sharing
      $config.DisableSharing()
    
  • How to enable Internet Connection Sharing using command line?
replaced http://superuser.com/ with https://superuser.com/
Source Link

On earlier OS, we had a batch method netsh routing but this no longer works in W7. Have a look at this powershell snip and see if it can help. Otherwise, I bet somewhere at stackoverflow is a C# example.

# 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()

http://earthwithsun.com/questions/470319/how-to-enable-internet-connection-sharing-using-command-line

How to enable Internet Connection Sharing using command line?How to enable Internet Connection Sharing using command line?

On earlier OS, we had a batch method netsh routing but this no longer works in W7. Have a look at this powershell snip and see if it can help. Otherwise, I bet somewhere at stackoverflow is a C# example.

# 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()

http://earthwithsun.com/questions/470319/how-to-enable-internet-connection-sharing-using-command-line

How to enable Internet Connection Sharing using command line?

On earlier OS, we had a batch method netsh routing but this no longer works in W7. Have a look at this powershell snip and see if it can help. Otherwise, I bet somewhere at stackoverflow is a C# example.

# 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()

http://earthwithsun.com/questions/470319/how-to-enable-internet-connection-sharing-using-command-line

How to enable Internet Connection Sharing using command line?

Source Link
Knuckle-Dragger
  • 2.1k
  • 1
  • 15
  • 19

On earlier OS, we had a batch method netsh routing but this no longer works in W7. Have a look at this powershell snip and see if it can help. Otherwise, I bet somewhere at stackoverflow is a C# example.

# 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()

http://earthwithsun.com/questions/470319/how-to-enable-internet-connection-sharing-using-command-line

How to enable Internet Connection Sharing using command line?