3

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
  • Use a power shell command or netsh wlan command to first list all the wireless profiles and go from there. netsh wlan show profiles
    – anon
    Commented Aug 22, 2021 at 21:08
  • This is not about wireless profiles.
    – Forivin
    Commented Aug 22, 2021 at 22:30
  • Wired profiles (any time I have looked) are set to Private by default.
    – anon
    Commented Aug 22, 2021 at 22:33
  • 1
    I've set up about 100 VMs with Windows 10 this month. The wired profiles were public by default every time. Windows does however ask if you want to make it private on the first boot.
    – Forivin
    Commented Aug 22, 2021 at 22:39
  • I'm not sure. I have VMware here and wired profiles for virtual machines are private by default. So I do not know in your situation.
    – anon
    Commented Aug 22, 2021 at 22:42

4 Answers 4

3

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.
3
  • 1
    This will only change category for the one interface labeled "Network", not all.
    – Dennis
    Commented Jul 17, 2022 at 11:54
  • @Dennis, Ok? Remove the parameter and get all profiles. Commented Jul 17, 2022 at 16:16
  • 1
    @'Abraham Zinala', Yes, that would be an answer to the question...
    – Dennis
    Commented Jul 18, 2022 at 10:12
0

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: https://stackoverflow.com/a/42654438/2879085

0
powershell -c "get-netconnectionprofile | foreach{Set-netconnectionprofile -InterfaceIndex $_.InterfaceIndex -NetworkCategory Private}"
1
  • 4
    Can you elaborate on this a little more?
    – Toto
    Commented Apr 10, 2022 at 21:18
0

Hope this helps:

(Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles").Name | foreach{REG ADD "$_" /v "Category" /t REG_DWORD /d 1 /f}
1
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Aug 22, 2023 at 17:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .