Admin Power Level: Intermediate Chapter 13: Control Who Gets In and Out

Create, modify, and enable firewall rules

New-NetFirewallRule creates a rule. Set-NetFirewallRule changes one, and Enable-NetFirewallRule turns a disabled rule on. Each rule should be as narrow as possible: one program or port, one direction, one profile.

Lesson 2 of 2 in Chapter 13: Control Who Gets In and Out. Previous step in this path: Firewall profiles, rules, and connection profiles. 3 learning objectives, 8 examples, and a knowledge check.

Start: what you will learn

New-NetFirewallRule creates a rule. Set-NetFirewallRule changes one, and Enable-NetFirewallRule turns a disabled rule on. Each rule should be as narrow as possible: one program or port, one direction, one profile.

Learning objectives

  • Create a narrow firewall rule
  • Enable a disabled rule
  • Modify a rule without recreating it

Concept in simple words

What it is: a firewall rule is one narrow decision: allow or block, in one direction, for one program or port, on chosen profiles. New-NetFirewallRule creates a rule, Set-NetFirewallRule changes one in place, and Enable-NetFirewallRule turns a disabled rule back on. Disable-NetFirewallRule does the reverse: it turns a rule off without deleting it, so you can bring it back later. Remove-NetFirewallRule deletes a rule for good, so use it only when you are sure, and only for a rule you created for testing or no longer need.

Why it works

Why narrow wins: a firewall rule is a hole in a wall that is otherwise closed for incoming traffic. A small hole lets one guest through one door. A large hole lets many strangers through many doors. A narrow rule opens one small, named hole, while everything else stays closed. A wide rule, for all profiles and all ports, can undo a lot of careful work in one line. Narrow also makes later reading easy: when you see the rule in six months, you can tell in one glance what it was for, and whether it is still needed.

Safety first, before any rule change: work only on your own computer and in your own lab. Rule changes need an elevated PowerShell session, so if you get an access problem, stop and check that you are elevated, rather than looking for a way around the protection. Never turn the whole firewall off to fix a problem, even briefly, because that removes protection for every program at once, not just for the one service you are testing. Instead, make one narrow rule, test it, and remove or disable it when you are done. Before you change a rule that already exists, note its current values: Name, DisplayName, Enabled, Direction, Action, Profile, and its port or program. Those notes are your way back.

Test with a harmless rule first: when you are learning, start with a test rule on a port you control, such as 8080, on Private only, and create it disabled first. Read it back while it is disabled, check every field, and only then enable it. Test the traffic you meant to allow, and also test traffic you meant to block, so you know the rule does what you think it does. When the test is done, disable or remove the test rule, so no practice hole stays open. This habit, disabled first, read back, enable, test, clean up, keeps every later change safe.

How New-NetFirewallRule works, in depth: -DisplayName is required, because every rule needs a readable name. -Name is optional, but you should give it anyway, because if you leave it out, Windows assigns a long technical name that is hard to use later. A clear Name, such as LabWeb8080, makes enable, change, and remove steps easy and safe. -Direction is Inbound or Outbound. -Action is Allow or Block. -Protocol, -LocalPort, -RemotePort, -Program, -RemoteAddress, and -Profile are the narrowing conditions. Each condition you add shrinks the set of traffic that can match. Each condition you leave out widens it, so leave a condition out only when you mean to.

Direction, in depth: Inbound means traffic coming into your computer from another computer. Outbound means traffic leaving your computer to another computer. This matters more than it looks, because an allow rule in the wrong direction does nothing for your problem, even when every other field is perfect. If a service on your computer needs to receive connections, you need an Inbound rule. If a program on your computer needs to send traffic out and something is blocking it, you need to look at Outbound rules. Always say the direction out loud before you create the rule: who starts the connection, and in which direction does it travel.

Ports and protocols, in depth: a port is a numbered door for one kind of service on a computer. Port 8080 in this chapter is a practice web port, not a magic number. -LocalPort is the port on your own computer. For an inbound rule for a service that listens on your computer, LocalPort is the listening port, which is why the lab web example uses LocalPort 8080. -Protocol says which kind of traffic the rule is about, such as TCP or UDP. TCP and UDP are different kinds of transport, and a service uses one of them, sometimes both. If you open TCP when the service needs UDP, the rule will not help. Check which protocol your service really uses, and do not open both just in case, because that doubles the hole.

Programs, in depth: -Program points at one program file, by its full path, such as a test program in Program Files. A program rule follows that program, wherever it sends or receives traffic, instead of opening one port for every program. The path must be exact, including spaces, which is why the path sits inside double quotes in the example. If the program moves, is renamed, or is updated to a new folder, the rule may stop matching, and traffic may be blocked again. That is not a firewall fault. It is a sign that the path in the rule no longer points at the real program, so check the path first.

Profiles and addresses, in depth: -Profile limits the rule to one trust level. -Profile Private means the rule only applies when the live profile is Private. -Profile Any means the rule applies on Domain, Private, and Public, which is the widest choice, so use Any only when you have a clear reason. -RemoteAddress narrows the other side of the connection. The keyword LocalSubnet means only computers on your local subnet, which is the directly attached local network. A single address narrows it even further, to one computer only. Each narrowing step trades a little convenience for a lot of safety, and for a lab service, Private plus LocalSubnet is often narrow enough and still usable.

How it works, step by step

How it works, step by step: First, list the rules and find the exact technical Name of the rule you want, because Enable and Set work most safely with Name. Second, if you need a new rule, give it a clear DisplayName and a clear Name, a Direction of Inbound or Outbound, an Action of Allow or Block, and one narrow match, such as one TCP port or one program. Third, limit the rule with -Profile when you can, for example Private only, and with -RemoteAddress LocalSubnet when the service only needs local computers. Fourth, create the rule disabled when you are testing, read it back, and check every field. Fifth, enable it, test both the traffic you meant to allow and the traffic you meant to block, and note the result. Sixth, clean up test rules when you are done.

Let us walk through the inbound web example, piece by piece: New-NetFirewallRule creates the rule. -DisplayName "Allow lab web" is the readable name you will see in lists. -Direction Inbound means traffic coming into your computer. -Protocol TCP says the traffic kind is TCP. -LocalPort 8080 narrows the rule to one port on your computer. -Action Allow permits traffic that matches all of those conditions. -Profile Private limits the rule to trusted networks. Change any one piece and the rule changes: change Inbound to Outbound and the rule no longer helps a service that listens on your computer. Change Allow to Block and the same traffic is stopped. Change Private to Any and the rule also applies on Public networks. Change 8080 to another port and the old port closes again.

Let us walk through the named test rule example, piece by piece: -Name LabWeb8080 gives the rule a short technical name you can type later without copying a long code. -DisplayName is still the readable name. -Enabled False creates the rule in a disabled state, so it exists but does nothing yet. That is the safe way to test: you can read the rule back, check its direction, action, profile, and port, and fix a mistake before any traffic can use the rule. When the read back looks right, Enable-NetFirewallRule -Name LabWeb8080 turns it on. If the read back looks wrong, you can fix it or remove the test rule while it is still disabled, with no traffic affected.

Let us walk through the enable and read back example, piece by piece: Enable-NetFirewallRule -Name "AllowLabWeb" turns on the rule with that technical Name. The second line, Get-NetFirewallRule -Name "AllowLabWeb", reads the same rule back. Select-Object shows DisplayName and Enabled. In the illustrative output, Enabled shows True, which confirms the rule is now live. This read back step is not optional decoration. It proves that you enabled the rule you meant, not a different rule with a similar readable name. If the read back shows False, the enable step did not take effect for that rule, so check the Name again.

Let us walk through the Set example, piece by piece: Set-NetFirewallRule changes a rule in place, so the rule keeps its identity and its Name. -Name LabWeb8080 picks the exact rule. -LocalPort 8081 changes the port the rule is about, from 8080 to 8081. Nothing else in the rule changes, unless you also name another parameter. That is the advantage over delete and recreate: you do not lose track of which rule you meant, and you do not risk creating a duplicate with a similar readable name. After a Set step, always read the rule back and follow it to its port filter, because the main rule list does not show the port, and you need to confirm the new port really landed.

Let us walk through the narrowing example, piece by piece: Set-NetFirewallRule -Name LabWeb8080 -RemoteAddress LocalSubnet keeps the same rule, but adds a source limit. Before this step, the rule allowed matching traffic from any remote address. After this step, it allows matching traffic only from the local subnet. The port, direction, action, and profile stay the same, because Set only changes the parameters you name. This is how you tighten a rule that started too wide: change one parameter, read it back, test again, and only then decide whether another tightening step is needed. Tighten one step at a time, so you know which change fixed or broke the test.

Let us walk through the outbound program example, piece by piece: New-NetFirewallRule creates the rule. -DisplayName "Block test program outbound" is the readable name. -Direction Outbound means traffic leaving your computer. -Program points at one program, using a generic example path that you must replace with your real program path, inside double quotes because the path contains spaces. -Action Block stops traffic from that program that matches the rule. -Profile Any means the rule applies on every profile. Notice how wide Any is here: for a block rule, wide can be what you want, because you want the program blocked everywhere. For an allow rule, the same Any choice would be much riskier, so always ask whether wide is safe for this action.

Let us walk through the safe cleanup example, piece by piece: Disable-NetFirewallRule -Name LabWeb8080 turns the test rule off without deleting it. The rule still exists, so you can enable it again later if you need the same test. Get-NetFirewallRule then reads it back, and Enabled should show False. Remove-NetFirewallRule is different: it deletes the rule for good. The example shows Remove with -WhatIf first, which previews what would be removed without removing anything. Only remove a rule when you have checked the Name, you have noted its values, and you are sure you no longer need it. For a test rule, disable first, and remove only when you are done testing.

What changes if you change one part: this is the fastest way to build real understanding, so try each change in your head before you try it on a machine. Change Direction, and the rule checks the opposite flow, so an inbound service stays blocked. Change Action, and the same matching traffic flips from allowed to blocked, or the reverse. Change Profile from Private to Any, and the rule starts applying on networks you do not trust. Change LocalPort, and the old port closes while the new port opens. Change Program to a wrong path, and the rule matches nothing, so traffic behaves as if the rule did not exist. Change Enabled to False, and the rule pauses without disappearing. Each field is one lever, and good firewall work is moving one lever at a time.

Edge cases you will meet: two rules can share a DisplayName, so a command that uses DisplayName alone can match more than one rule, or the wrong one. That is why Name is the safer key for Enable, Set, Disable, and Remove. A rule can exist but be disabled, so a service stays blocked even though the rule looks right in a name search. A rule can be enabled but on the wrong profile, so it does nothing on the network you are actually on. A program rule can stop matching after an update moves the program. A port rule can look right while the service itself is not running, so no traffic arrives at that port at all. In each case, read the rule back, check the live profile, and check whether the service is running, before you widen the rule.

When a service is still blocked after an allow rule: do not widen the rule in a panic. Work through the checklist in order. Is the rule enabled. Is the direction Inbound for a listening service. Is the action Allow. Does the profile include your live profile. Does the port filter show the right protocol and port. Does the program path still point at the real program. Is the service itself running and listening. Is there another block rule for the same traffic. Only when you have checked those steps should you change the rule, and then change one field only, read it back, and test again. Widening without checking is how a small lab problem becomes a large open hole.

When to use it

When to use a new rule, and when not to: use a new rule when a program or service needs specific traffic and you can name that traffic exactly: one direction, one action, one port or program, and one profile where you can. Do not use a wide rule for all profiles and all ports just to save time, because that one rule can undo a lot of careful work. Do not delete and recreate a rule just to change one setting when Set-NetFirewallRule can change it in place. Do not enable a rule you have not read, because an old disabled rule may allow more than you remember. Do not copy a rule from an example without replacing the example port, program path, and profile with your own real values.

A final habit for real work: every rule you create should be explainable in one sentence. For example: this rule allows inbound TCP port 8080 on Private networks for lab web testing. If you cannot say that sentence, the rule is probably too wide, or you do not yet understand what it does, and you should not enable it yet. Keep a short note for each rule you change: the old values, the new values, and why you changed them. That note is what lets you undo a change cleanly, explain it later, and clean up test rules without fear of breaking a service you forgot about.

Worked examples

Read each example in order. Each one explains its parts, then shows what to look for in the output.

Syntax

Syntax
New-NetFirewallRule -DisplayName <Name> -Direction <In|Out> -Action <Allow|Block>

Example 1

Allows inbound TCP port 8080, only on Private networks. Narrow is safer than wide: one direction, one protocol, one port, one profile. Change Private to Any and the rule would also apply on Public networks, which is wider and riskier for an allow rule.

Example 1
New-NetFirewallRule -DisplayName "Allow lab web" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow -Profile Private

Illustrative output: Rule Allow lab web created.

Example 2

Creates the same kind of rule, but disabled first, so it does nothing yet. -Name LabWeb8080 gives you a short technical name for later steps. -Enabled False is the safe test pattern: read the rule back, check every field, and only then enable it.

Example 2
New-NetFirewallRule -Name LabWeb8080 -DisplayName "Allow lab web" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow -Profile Private -Enabled False

Illustrative output: Rule Allow lab web created.

Example 3

Enables a rule by its technical name, then reads it back to confirm. Use Name, not only DisplayName, because DisplayName can repeat across rules. In the read back, Enabled should show True for the rule you meant.

Example 3
Enable-NetFirewallRule -Name "AllowLabWeb"
Get-NetFirewallRule -Name "AllowLabWeb" | Select-Object DisplayName, Enabled

Illustrative output: DisplayName Enabled ----------- ------- Allow lab web True

Example 4

Reads one rule back before you change it, and notes its current values. This is your backup in plain words: if the change goes wrong, you know the old Enabled, Direction, Action, and Profile values to put back. Do this before every Set, Enable, Disable, or Remove step.

Example 4
Get-NetFirewallRule -Name LabWeb8080 | Select-Object DisplayName, Enabled, Direction, Action, Profile

Illustrative output: DisplayName Enabled Direction Action Profile ----------- ------- --------- ------ ------- Allow lab web False Inbound Allow Private

Example 5

Changes the port in place, without deleting and recreating the rule. -Name LabWeb8080 picks the exact rule, and -LocalPort 8081 is the only field changed. The second line follows the rule to its port filter, because the main rule list does not show ports, and confirms the new port really landed.

Example 5
Set-NetFirewallRule -Name LabWeb8080 -LocalPort 8081
Get-NetFirewallRule -Name LabWeb8080 | Get-NetFirewallPortFilter | Select-Object Protocol, LocalPort

Illustrative output: Protocol LocalPort -------- --------- TCP 8081

Example 6

Narrows the same rule to local computers only. -RemoteAddress LocalSubnet means only computers on the local subnet can match the rule. The port, direction, action, and profile stay the same, because Set only changes the parameters you name. Tighten one field at a time, then read back and test again.

Example 6
Set-NetFirewallRule -Name LabWeb8080 -RemoteAddress LocalSubnet

Illustrative output: Rule LabWeb8080 updated.

Example 7

New-NetFirewallRule creates a rule. -DisplayName is the readable name, -Direction Outbound means traffic leaving your computer, and -Program points at one program, using a generic example path that you must replace with your real program path. -Action Block stops that traffic, and -Profile Any means the rule applies on every profile. For a block rule, wide can be intended, but for an allow rule, Any is much riskier.

Example 7
New-NetFirewallRule -DisplayName "Block test program outbound" -Direction Outbound -Program "C:\Program Files\Test\test.exe" -Action Block -Profile Any

Illustrative output: Rule Block test program outbound created.

Example 8

Safe cleanup in order. Disable turns the test rule off without deleting it, and the read back should show Enabled as False. Remove with -WhatIf only previews what would be deleted, so you can confirm the Name before you delete anything for good. Remove the test rule only when you are done testing and you have noted its values.

Example 8
Disable-NetFirewallRule -Name LabWeb8080
Get-NetFirewallRule -Name LabWeb8080 | Select-Object DisplayName, Enabled
Remove-NetFirewallRule -Name LabWeb8080 -WhatIf

Illustrative output: DisplayName Enabled ----------- ------- Allow lab web False

Nothing on this page executes PowerShell code. The outputs above are illustrative only.

Diagram

An incoming connection is checked against a rule, one check at a time.ProfilePrivate→DirectionInbound→Port443→ActionAllowAll checks matchtraffic is allowedAny check failstraffic is blockedA rule allows traffic only when profile, direction, and port all match.

Choose a stage above to see what it does.

Common mistakes and troubleshooting

Read these before you try the examples, so you can spot the same problems in your own work.

  • Creating a rule for all profiles and all ports when only one port on one profile was needed.
  • Enabling a rule by DisplayName when the cmdlet expects the technical Name. List rules first and copy the Name exactly.
  • Deleting a rule and recreating it just to change one port or one profile, instead of using Set-NetFirewallRule to change that one setting in place.
  • Enabling a rule you have not read. An old disabled rule may allow more than you remember, so read Enabled, Direction, Action, Profile, and the port or program first.
  • Using DisplayName alone when two rules share that readable name. The command can then match the wrong rule, so use Name for Enable, Set, Disable, and Remove.
  • Changing a live rule without noting its old values first. Without those notes, you cannot put the rule back cleanly if the change breaks a service.
  • Leaving a test rule enabled after practice is over. A practice hole that stays open is no longer practice, so disable or remove test rules when you are done.
  • Typing a program path with a mistake, or using an old path after an update moved the program. The rule then matches nothing, and the service stays blocked while the rule looks correct.
  • Widening a rule in a panic when a service is still blocked. Check enabled state, direction, profile, port filter, program path, and whether the service is running first, and change one field at a time.

Knowledge check

Knowledge check

Which cmdlet creates a new firewall rule?

Pick one answer. Feedback appears right away, and you can try again.

Remember this and next step

Remember this: narrow is safe, readable is safe, and checked is safe. Give each rule one job, one direction, and one profile where you can, and give it a clear Name as well as a clear DisplayName. Create test rules disabled first, read them back, enable them only when the read back is right, test both allowed and blocked traffic, and clean up when you are done. Never turn the whole firewall off as a fix, never enable a rule you have not read, and never leave a practice rule open after the practice is over. That discipline is what turns firewall commands from risky guesses into careful, reversible admin work.

Next step: Get a GPO and read its reports