Results 1 to 10 of 10
  1. #1
    Administrator Rick Corbett's Avatar
    Join Date
    Dec 2009
    Location
    South Glos., UK
    Posts
    3,462
    Thanks
    153
    Thanked 941 Times in 756 Posts

    Change network connection from Public to Private

    Just recently a WSL member had a problem changing a Windows 10 network connection from the default Public to Private when the GUI settings to do this were missing.

    Further research shows that there's a documented method of enumerating network connections. This can be used via COM to iterate through each network connection, changing the value for 'Category' to 1, i.e. Private.

    I'm not at all familiar with PowerShell but, using AutoHotkey, this can be accomplished by the following:

    Code:
    #NoEnv
    #NoTrayIcon
    SetBatchLines, -1
    
    NetworkListManager := ComObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")
    IEnumNetworkConnections := NetworkListManager.GetNetworks(NLM_ENUM_NETWORK_ALL := 3)
    for INetwork in IEnumNetworkConnections
            INetwork.SetCategory(1) ; NLM_NETWORK_CATEGORY_PRIVATE
    This is better than changing the 'Category' value via the registry, not least of which is that it can be carried out 'on-the-fly' to a currently-connected network.

    I've added this because it should be possible to help people in future like the OP who have a similar issue... and 'cos I've just discovered the method.

    Hope this helps...
    Last edited by Rick Corbett; 2017-05-30 at 12:38.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,970
    Thanks
    424
    Thanked 1,600 Times in 1,450 Posts
    Rick,

    Here's the PowerShell equivalent. Note that this will change ALL ACTIVE connections!

    Code:
    Param (
      [Parameter(Mandatory=$True)]
        [String] $NetType
    )
    
    $NConnections = Get-NetConnectionProfile
    
    ForEach ( $nc in $NConnections) {
    
       $Args = @{InterfaceIndex = $nc.InterfaceIndex
                 NetworkCategory = $NetType
                }
       
       Set-NetConnectionProfile @Args
    
    }
    Copy above text into a text editor, e.g NotPad. Then save it as Set-NetConnectionType.ps1 to your Default Powershell Scripts Folder.

    Sample Run from an Elevated PowerShell command window:

    Code:
    PS> .\Set-NetConnectionType.ps1 -NetType "Public"
    
    PS> Get-NetConnectionProfile
    
    
    Name             : ComputerMentor
    InterfaceAlias   : Wi-Fi 2
    InterfaceIndex   : 15
    NetworkCategory  : Public
    IPv4Connectivity : Internet
    IPv6Connectivity : Internet
    
    Name             : ComputerMentor
    InterfaceAlias   : Ethernet 2
    InterfaceIndex   : 18
    NetworkCategory  : Public
    IPv4Connectivity : Internet
    IPv6Connectivity : Internet
    
     
    PS> .\Set-NetConnectionType.ps1 -NetType "Private"
    
    PS> Get-NetConnectionProfile
    
    
    Name             : ComputerMentor
    InterfaceAlias   : Wi-Fi 2
    InterfaceIndex   : 15
    NetworkCategory  : Private
    IPv4Connectivity : Internet
    IPv6Connectivity : Internet
    
    Name             : ComputerMentor
    InterfaceAlias   : Ethernet 2
    InterfaceIndex   : 18
    NetworkCategory  : Private
    IPv4Connectivity : Internet
    IPv6Connectivity : Internet
    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Administrator Rick Corbett's Avatar
    Join Date
    Dec 2009
    Location
    South Glos., UK
    Posts
    3,462
    Thanks
    153
    Thanked 941 Times in 756 Posts
    Quote Originally Posted by RetiredGeek
    Here's the PowerShell equivalent. Note that this will change ALL ACTIVE connections!
    Thank you, RG. That's very neat!

  4. #4
    Super Moderator
    Join Date
    Aug 2012
    Location
    Durham UK
    Posts
    7,467
    Thanks
    181
    Thanked 984 Times in 936 Posts
    @ RG - Wouldn't you need to run this cmd to determine the Idx Nos on each computer before running that script as they can differ from machine to machine ?

    netsh interface ipv4 show interfaces

    Also for ipv6.
    Last edited by Sudo; 2017-05-31 at 05:01.

  5. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,970
    Thanks
    424
    Thanked 1,600 Times in 1,450 Posts
    Quote Originally Posted by Sudo View Post
    @ RG - Wouldn't you need to run this cmd to determine the Idx Nos on each computer before running that script as they can differ from machine to machine ?

    netsh interface ipv4 show interfaces

    Also for ipv6.
    Sudo,

    No that is not necessary as the first command in the script retrieves all the active connections on the machine then iterates through them setting them to the value in the passed argument.

    I just realizes I forgot to validate the parameter value, my bad. When I get back to my computer I'll post a revised script.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  6. #6
    Super Moderator
    Join Date
    Aug 2012
    Location
    Durham UK
    Posts
    7,467
    Thanks
    181
    Thanked 984 Times in 936 Posts
    It's just that your script has specific Index Nos so I was wondering if it was a one size fits all.

    Is it necessary to include ipv6 as ISPs tend not to support that at the moment and they have different Index Nos to ipv4.

    Microsoft Windows [Version 10.0.15063]
    (c) 2017 Microsoft Corporation. All rights reserved.

    C:\WINDOWS\system32>netsh interface ipv4 show interfaces

    Idx Met MTU State Name
    --- ---------- ---------- ------------ ---------------------------
    3 50 1500 connected Wi-Fi
    12 25 1500 disconnected Local Area Connection* 9
    13 5 1500 disconnected Ethernet
    1 75 4294967295 connected Loopback Pseudo-Interface 1


    C:\WINDOWS\system32>netsh interface ipv6 show interfaces

    Idx Met MTU State Name
    --- ---------- ---------- ------------ ---------------------------
    12 25 1500 disconnected Local Area Connection* 9
    11 75 1280 connected Teredo Tunneling Pseudo-Interface
    1 75 4294967295 connected Loopback Pseudo-Interface 1


    C:\WINDOWS\system32>

  7. #7
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,970
    Thanks
    424
    Thanked 1,600 Times in 1,450 Posts
    Sudo,

    I think you're confusing the script with the sample run which in the second code block.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  8. #8
    Super Moderator
    Join Date
    Aug 2012
    Location
    Durham UK
    Posts
    7,467
    Thanks
    181
    Thanked 984 Times in 936 Posts
    Ah - I'm with you.

  9. #9
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,970
    Thanks
    424
    Thanked 1,600 Times in 1,450 Posts
    Hey Y'all,

    Here's the update I promised you. Now you can't enter a bad network type, but you can still enter the wrong network type!

    Code:
    Param (
      [Parameter(Mandatory=$True)]
      [ValidateSet("Private","Public","Domain")] 
        [String] $NetType
    )
    
    $NConnections = Get-NetConnectionProfile
    
    ForEach ( $nc in $NConnections) {
    
       $Args = @{InterfaceIndex = $nc.InterfaceIndex
                 NetworkCategory = $NetType
                }
       
       Set-NetConnectionProfile @Args
    
    }
    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  10. #10
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,571
    Thanks
    35
    Thanked 217 Times in 193 Posts
    Quote Originally Posted by Sudo View Post
    C:\WINDOWS\system32>netsh interface ipv4 show interfaces

    Idx Met MTU State Name
    --- ---------- ---------- ------------ ---------------------------
    3 50 1500 connected Wi-Fi
    12 25 1500 disconnected Local Area Connection* 9
    13 5 1500 disconnected Ethernet
    1 75 4294967295 connected Loopback Pseudo-Interface 1
    Code:
    C:\WINDOWS\system32>netsh interface ipv4 show interfaces
    
    Idx     Met         MTU          State                Name
    ---  ----------  ----------  ------------  ---------------------------
      3          50        1500  connected     Wi-Fi
     12          25        1500  disconnected  Local Area Connection* 9
     13           5        1500  disconnected  Ethernet
      1          75  4294967295  connected     Loopback Pseudo-Interface 1
    Needs the 'code' BBS tag! HTML strips multiple blanks from a fixed-pitch font like Courier New or Lucida Console...
    BATcher

    Talking to laser printers can be very effective.
    You just need the right toner voice...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •