Change network connection category using PowerShell

Note: This blogpost is also posted on the peppercrew website.

refwin-advfirewall-img4

I recently came across an issue with PSRemoting to a Windows 7 XenDesktop VM. I discovered remoting was disabled on the remote system because one of the network connections was a Public connection (it was the PVS connection). To change this using PowerShell is quite easy in Windows 8 or higher: the Set-NetConnectionProfile command is available to do just that. However, in Windows 7 this is not so easy. Even though PowerShell 4 was installed, the command was absent. So I decided to create my own functions to facilitate this, based on this script by Microsoft: LINK. This script utilizes the Network List Manager to make the change. 

I created two functions named after the ones in Windows 8 and higher:

  • Get-NetConnectionProfile
  • Set-NetConnectionProfile

Here are some examples of how to use these (also included in the help):

# This will get the category for all network connections:
PS\> Get-NetConnectionProfile

IsConnectedToInternet : False
Category              : Public
Description           : Unknown network
Name                  : Unknown network
IsConnected           : True

IsConnectedToInternet : True
Category              : Domain
Description           : DOMAIN.LOCAL
Name                  : DOMAIN
IsConnected           : True

# This will get the category for all public network connections:
PS\> Get-NetConnectionProfile -NetworkCategory Public

IsConnectedToInternet : False
Category              : Public
Description           : Unknown network
Name                  : Unknown network
IsConnected           : True

# This sets the network category of the 'LAN1' connection to Private:
PS\> Set-NetConnectionProfile -Name 'LAN1' -NetworkCategory Private

IsConnectedToInternet : True
Category              : Private
Description           : LAN1
Name                  : LAN1
IsConnected           : True

# This sets the network category for all Public connections to Private
PS\> Get-NetConnectionProfile -NetworkCategory Public | Set-NetConnectionProfile -NetworkCategory Private

IsConnectedToInternet : False
Category              : Private
Description           : Unknown network
Name                  : Unknown network
IsConnected           : True

You can download or view the functions here: LINK

Hope it helps!

MicaH

Advertisements

About MicaH

I'm a Technical Specialist at PepperByte BV (the Netherlands).
This entry was posted in Powershell and tagged , , , , , , , , , . Bookmark the permalink.

2 Responses to Change network connection category using PowerShell

  1. Adam Mnich says:

    Great job!
    One problem I had is that your functions show by default only active (connected) profiles.
    I made a small modification by changing in Set-NetConnectionProfile
    from :
    $AllNetworks = $NetworkListManager.GetNetworks(1)
    to:
    $AllNetworks = $NetworkListManager.GetNetworks(3)
    so you can modify all profiles regardless of the connection state.

    I added a parameter to Get-NetConnectionProfile
    [ValidateSet(‘All’,’Connected’,’Disconnected’)]
    $ConnectionState = “All”
    some code in script
    added hash table with values
    $ConnectedType = @{
    ‘Connected’ = 1
    ‘Disconnected’ = 2
    ‘All’ = 3
    }

    and changed
    from:
    $Networks = $NetworkListManager.GetNetworks(1)
    to:
    $Networks = $NetworkListManager.GetNetworks($ConnectedType[$ConnectionState])

    Now you can also display not connected profiles and change them.

    • MicaH says:

      Nice! I hadn’t considered there could be a need for this functionality, so nice catch! With your permission I’d like to incorporate your solution into my script.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s