I have a very simple question:

I'm currently setting a specific network adapter to private like this from a batch script:

powershell -command Set-NetConnectionProfile -Name "Network" -NetworkCategory Private 

How can I do this for all network adapters without knowing the names or anything about them?

6

3 Answers

How about this:

Get-NetConnectionProfile -InterfaceAlias "Network" | Set-NetConnectionProfile -NetworkCategory Private -Confirm:$false -PassThru 

Usually (almost every time), where there's a Set-*, there's a Get-*. PowerShell cmdlets output Objects that can be chained (piped) to it's corresponding Verb-Noun cmdlet.

  • Get-NetConnectionProfile in this case, returns the connection profiles that have an interface name of "Network".
  • Set-ConnectionProfile. When piped to this cmdlet, you are able to grab the entirety of the object from Get-NetConnectionProfile, and modify it. I.e: setting it to Private.

Okay, I finally found a way. I couldn't figure out how to iterate over the Names, but I found a way to iterate over the InterfaceAliases. I had to adjust the Set-NetConnectionProfile call accordingly, but now it works like a charm:

for /F "skip=3 tokens=1,2,3* delims= " %%G in ('netsh interface show interface') DO ( echo "Setting %%J to private..." powershell -command Set-NetConnectionProfile -InterfaceAlias "Network" -NetworkCategory Private ) 

See interface loop came from here:

powershell -c "get-netconnectionprofile | foreach{Set-netconnectionprofile -InterfaceIndex $_.InterfaceIndex -NetworkCategory Private}" 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy