PowerShell script to change network interface

This seems like a very likely frequent objective:
Run a PowerShell script that will do the following:
Find the name (or .... ?) of the currently connected NIC.
Local Computer Policy / Computer Configuration / Windows Settings / Security Settings / Network List Manager Policies / Network / Network Location / Location Type

Everything that I've found requires a manual step to find the connected interface name first.
Then, gives a command that can be used, using the name.

I should think that this would be more useful as a script:

[get the name of the connected interface] THEN [change the interface Location type of the named interface]

LVL 27
Fred MarshallPrincipalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
$profiles = Get-NetConnectionProfile 
foreach($profile in $profiles){
If (!($profile.ipv4Connectivity -like "Disconnected"))
  Set-NetConnectionProfile -interfaceIndex $profile.InterfaceIndex -NetworkCategory Private

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hello ThereSystem AdministratorCommented:
We use this one:
Get-NetConnectionProfile |
  Where{ $_.NetWorkCategory -ne 'Private'} |
  ForEach {
    $_|Set-NetConnectionProfile -NetWorkCategory Private -Confirm

Open in new window


Get-NetConnectionProfile | % { Set-NetConnectionProfile -InterfaceIndex $_.InterfaceIndex -NetworkCategory Private }

Open in new window

Fred MarshallPrincipalAuthor Commented:
I think these are setting the Network Category and not the Network Location \ Location Type.Network location type
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

David Johnson, CD, MVPOwnerCommented:
What you believe is incorrect, you have been given many examples that do what you want.
Fred MarshallPrincipalAuthor Commented:
I have no problem with the examples.  But I don't see the setting change.  Perhaps I reached an erroneous conclusion.
Fred MarshallPrincipalAuthor Commented:
I did a few experiments:
It appears that the setting I targeted and those in the same dialogs don't change anything for Windows 10 Pro at least.
The Setting is still "Make this PC Discoverable" and the network type remains Private even though I set those dialogs at Public and rebooted.

Now I'm wondering if what I need is even possible.  Too bad if not.

What I really need is this:
When something happens to the Ethernet connection on a Windows 10 Pro computer, when the network comes back up, I need it to be Private by preference and not by manually resetting it.
Recently I had a bunch of them come up Public - perhaps due to a network interruption.
While this doesn't happen very often, I need to  prevent that and I'd like to have a script that will set the preference to Private and not just the setting.

Or, does one have to run a script at boot time?
Fred MarshallPrincipalAuthor Commented:
Any suggestions?  I do know that the commands provided will change the network type in real time.  What I'm looking for is to make the network type permanent.
Fred MarshallPrincipalAuthor Commented:
I'm confused.  David Johnson says that the question has been answered and I have posted a suggestion that says it's a different question/answer.  No clarification...?
David Johnson, CD, MVPOwnerCommented:
It should always be permanent having the connection type change is a problem with your networking. You will have to investigate why the connection type is changing. The cause could be a slow network changing the Network Location Awareness Startup from Automatic to Automatic (delayed start) will give your networking components time to setup and configure

Network Location Awareness Documentation
Fred MarshallPrincipalAuthor Commented:
David Johnson:  Thanks.  Well, it's not so much that "it's changing" but that many computers "did once change" and I'm trying to prevent that wholesale action from happening again.  It's tedious to fix.  
With that in mind, I agree that there could be something "wrong" or intermittent with the network but I'm hard pressed to find it as it seems to be working fine except in this one rather glaring case.
We have seen computers switch from "private" to "public" one-off now and then and would like to prevent that as well.  There has been no common characteristic that would narrow the computers down to a physical zone in the network.
It's really the same issue I should think.

My hypothesis has been:
"This computer was disconnected from the network.  When it was reconnected, the network was deemed "new" or "unknown" and made Public."
And, the opportunity to investigate "why the connection type is changing" is too limited to be practical.
For example:
Someone will call that their network access is broken in some fashion.
Upon investigating, I find that their network type is Public when it should be Private.
I have little or no foreknowledge regarding "what happened to that computer leading up to the unwanted change".
Because the computers are in a fixed network situation (except a few laptops) I'd like to make them permanently Private.

I can make the Network Location Awareness service set to Automatic (delayed start).  But that seems to put the cart before the horse in that the interface will come up in *some* state and, if unknown, it may well be Public if there is no other choice forced.  
Is that right?  
I'd rather have the initial state be Private once and for all.
Perhaps I'm puzzled by the implication and the STATE of the setting:
Network List Manager Properties \ Network Location \ Location Type \ Private.
I'm not pushing that as a solution because it doesn't seem to work.  I'm just asking.
Fred MarshallPrincipalAuthor Commented:
I really appreciate the help and want to close the question myself at least.....
Fred MarshallPrincipalAuthor Commented:
Thanks again!  I still haven't figured out how to assure that connections will stay Private but at least I have some ideas.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.