Search code examples
windowspowershellnetwork-programmingwindows-10

How to programmatically set Unidentified Networks to be a Private or Public Network Location on Windows 10?


Changing Ethernet or Wi-Fi connections to Private/Public is something very easy to be done. I can do that either from PowerShell or Registries.

But, is there a way of changing Unidentified Networks or Identifying Networks to Private/Public in a programmatically way on Windows 10 ? I would like to include this step into one of my projects.

I found this answer on the sevenforums.com , but it applies only to Windows 7.


Solution

  • The General Command Is Using Set-NetConnectionProfile Cmdlet

    https://learn.microsoft.com/en-us/powershell/module/netconnection/set-netconnectionprofile?view=windowsserver2019-ps

    #Update Windows Firewall from Public to Private
    Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private
    

    To Run The Set-NetConnectionProfile Cmdlet Command On A Remote Computer

    $server = "servername"
    $RequestingServer = $env:COMPUTERNAME
    
    [STRING] $LocalUser = "Administrator"
    [STRING] $LocalPassword = "Password01"
    $LocalSecurePassword = $LocalPassword | ConvertTo-SecureString -AsPlainText -Force
    $LocalCredentials = New-Object System.Management.Automation.PSCredential -ArgumentList $LocalUser, $LocalSecurePassword
    
    #Update Windows Firewall Remotely
    $LocalSession = New-PSSession -Computername $Server -Credential $LocalCredentials
    Invoke-Command -Session $LocalSession -ScriptBlock {
    
    $AddServer = $Using:RequestingServer
    
        #Update Windows Firewall from Public to Private
        Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private
        
        #Update Windows Firewall to allow remote WMI Access
        netsh advfirewall firewall set rule group="Windows Management Instrumentation (WMI)" new enable=yes
        
        #Update Trusted Hosts is not domain-joined and therefore must be added to the TrustedHosts list 
        Set-Item wsman:\localhost\Client\TrustedHosts -Value $AddServer -Force
        
        #Update Windows Firewall to allow RDP
        Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
        
        #Enable RDP : 1 = Disable ; 0 = Enable
        Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" -Name "fDenyTSConnections" -Value 0
    }