Windows Firewall policies decide which connections a PC accepts or sends. From an elevated Command Prompt, netsh advfirewall inspects profiles and applies a chosen Windows Firewall policy change.
Firewall settings apply separately to the Domain, Private, and Public network profiles. An inbound allow rule can be limited to the application's port, the intended profile, and the computers that need access, instead of opening the service on every network.
Local changes take effect immediately, but an organization's Group Policy or device-management policy can prevent local rules from applying. Firewall changes can interrupt remote administration or expose listening services, so a policy backup and access to the PC's local console matter before a disruptive change.
Steps to manage Windows Firewall with netsh:
- Open Command Prompt as an administrator.
- Check the Windows Firewall service named MpsSvc.
C:\> sc query mpssvc
The expected service state is 4 RUNNING. The service can be running even when firewall protection is disabled for a profile. Stopping MpsSvc is not a supported way to disable Windows Firewall.
- Inspect the settings for all three firewall profiles.
C:\> netsh advfirewall show allprofiles
The output includes each profile's State, Firewall Policy, and logging settings. This is an inspection command; it changes nothing.
- Identify the active profile for the connection you intend to change.
C:\> netsh advfirewall show currentprofile
The profile headings identify the active profiles. On a PC with multiple active connections, more than one profile can be active. The operations below target privateprofile or profile=private; domainprofile/profile=domain and publicprofile/profile=public select the other profiles. A network should not be reclassified as Private merely to make a rule apply.
- Export the current firewall policy to a new backup file before making any changes.
C:\> netsh advfirewall export "%USERPROFILE%\firewall-before.wfw"
A different filename preserves an older backup. The export is a recovery copy, not a step that applies a new policy.
- Confirm that the backup file exists at the chosen location.
C:\> dir "%USERPROFILE%\firewall-before.wfw"
A successful export reports Ok. and produces a nonempty .wfw file. An export error or missing file means there is no usable backup yet.
- Apply only the operation that matches your intended policy change.
These are alternatives, not a sequence to execute. The Private-profile examples apply only when that is the intended network profile.
Private-profile protection
C:\> netsh advfirewall set privateprofile state on
Inbound TCP access from the local subnet
This adds an enabled rule for TCP port 8080 on Private networks. The rule permits any program listening on that port, so the port must belong to the intended service. A unique rule name avoids ambiguous later management; remoteip=localsubnet limits the allowed source addresses.
C:\> netsh advfirewall firewall add rule name="Allow TCP 8080 from local subnet" dir=in action=allow protocol=TCP localport=8080 profile=private remoteip=localsubnet
Private-profile dropped-packet logging
This changes only the dropped-connection setting. The existing log filename and size remain in effect; each logging setting requires its own invocation. How to access and view Windows Firewall logs covers reading the log.
C:\> netsh advfirewall set privateprofile logging droppedconnections enable
Removal of this specific rule
Deletion removes all rules matching the supplied filters. This alternative closes the access granted by this rule; other matching allow rules may still permit traffic.
C:\> netsh advfirewall firewall delete rule name="Allow TCP 8080 from local subnet" dir=in protocol=TCP localport=8080 profile=private
Disruptive alternatives — only for an explicit maintenance or recovery need
Disabling protection exposes listening services. Microsoft recommends keeping Windows Firewall enabled. The single-profile command below affects Private networks only.
C:\> netsh advfirewall set privateprofile state off
The broader alternative below turns protection off for every profile, including Public networks. It is appropriate only when that full scope is explicitly intended for an isolated diagnostic. The service stays running.
C:\> netsh advfirewall set allprofiles state off
After a temporary diagnostic, protection can be restored on the affected profile with the enable command above; for an intentional return to protection on every profile, the command is:
C:\> netsh advfirewall set allprofiles state on
Import replaces the current local firewall policy with the saved policy, including its rules and profile settings. An unsuitable backup can break remote access or restore unwanted exposure.
C:\> netsh advfirewall import "%USERPROFILE%\firewall-before.wfw"
Reset restores default policy and removes custom rules, which can disconnect remote administration and applications that relied on those rules. It is a recovery operation, not routine cleanup; the exported policy is the rollback copy.
C:\> netsh advfirewall reset
- Inspect the resulting settings for the operation you selected.
For profile state or logging changes, this displays the Private profile's state, policy, and logging values:
C:\> netsh advfirewall show privateprofile
For the named rule, this shows its scope and action, or reports no matching rule after deletion:
C:\> netsh advfirewall firewall show rule name="Allow TCP 8080 from local subnet" verbose
An enabled allow rule should show Enabled: Yes, Direction: In, Profiles: Private, RemoteIP: LocalSubnet, LocalPort: 8080, and Action: Allow. For import, reset, or all-profile changes, the all-profile inspection from earlier covers the affected settings. A setting or rule listing alone does not prove that traffic behaves as intended.
- Test the affected connection from another computer on the intended network.
For the inbound TCP 8080 rule, the application must already be listening on the firewall PC's network address. In a Command Prompt on a second Windows computer in the allowed subnet, a TCP connection test can be run as follows, with 192.0.2.10 replaced by the firewall PC's actual address:
C:\> powershell -NoProfile -Command "Test-NetConnection -ComputerName 192.0.2.10 -Port 8080 -InformationLevel Detailed"
ComputerName : 192.0.2.10 RemoteAddress : 192.0.2.10 RemotePort : 8080 ##### snipped ##### TcpTestSucceeded : True
TcpTestSucceeded: True confirms a TCP connection; the application's normal response confirms the service itself works. A failed TCP test can also mean that nothing is listening, routing is wrong, an explicit block rule takes precedence, or organization policy prevents the local allow rule from applying. For a logging-only change, the expected result is unchanged connectivity plus a new dropped-packet entry after blocked traffic, as described in How to access and view Windows Firewall logs.
- Test a denied connection when your chosen change must restrict access.
For the Private-profile allow rule, the same TCP test from a source outside the allowed subnet, or through an interface using a different profile, should fail when no other rule permits that access.
##### snipped ##### TcpTestSucceeded : False
A False result is meaningful only with a reachable PC and a service that is still listening; ping alone does not test TCP port 8080. Unexpected access means another rule or policy also permits the connection.