Managing Windows Firewall Rules with PowerShell: A Comprehensive Guide

Edward Zhou
Edward Zhou

CEO & Founder

 
July 16, 2025 3 min read

Managing Windows Firewall Rules with PowerShell: Part 2 – Overcoming Cmdlet Limitations

wall between a computer monitor and a shield

Image courtesy of ITPro Today

In this section, we will discuss how to extract critical firewall rule details that the standard Get-NetFirewallRule cmdlet can't provide on its own. The Get-NetFirewallRule cmdlet retrieves general-purpose metadata for Windows firewall rules, but it does not return specific details such as local port numbers, remote port numbers, or protocols.

Retrieving Local and Remote Port and Protocol

To retrieve the protocol, local port number, and remote port used by a specific firewall rule, you can use the Get-NetFirewallPortFilter cmdlet. For example, if you want to find this information for a firewall rule named "My Example Rule," use the following command:

Get-NetFirewallRule -DisplayName "My Example Rule" | Get-NetFirewallPortFilter | Select-Object Name, Protocol, LocalPort, RemotePort

This command returns the protocol, local port number, and remote port associated with the specified firewall rule.

Retrieving Addresses

To retrieve the remote address and local address for a given rule, utilize the Get-NetFirewallAddressFilter cmdlet. This can be done as follows:

Get-NetFirewallRule -DisplayName "My Example Rule" | Get-NetFirewallAddressFilter | Select-Object Name, RemoteAddress, LocalAddress

This command will return the remote address and local address for the specified firewall rule.

screen shot of retrieving the local address and the remote address

Image courtesy of ITPro Today

Producing a Consolidated Output

If you need to access all information about a firewall rule in one place, consider using a PowerShell script to consolidate the details. Here’s a sample script:

$RuleName = "My Example Rule"
$Rule = Get-NetFirewallRule -DisplayName $RuleName
$PortFilter = $Rule | Get-NetFirewallPortFilter
$AddressFilter = $Rule | Get-NetFirewallAddressFilter
$ConsolidatedInfo = [PSCustomObject]@{
    Name          = $Rule.DisplayName
    Direction     = $Rule.Direction
    Action        = $Rule.Action
    Protocol      = $PortFilter.Protocol
    LocalPort     = $PortFilter.LocalPort
    RemotePort    = $PortFilter.RemotePort
    LocalAddress  = $AddressFilter.LocalAddress
    RemoteAddress = $AddressFilter.RemoteAddress
}

Write-Host $ConsolidatedInfo

This script collects and displays all relevant information about the specified firewall rule in a single output.

This script produces a consolidated output of the firewall rule information

Image courtesy of ITPro Today

Managing Windows Firewall Rules with PowerShell: Part 1 – Beyond the GUI

brick wall being built to separate two laptops

Image courtesy of ITPro Today

PowerShell offers a more efficient way to manage Windows firewall rules compared to the GUI. For example, four primary cmdlets can be used:

  • Get-NetFirewallRule: Displays firewall rules
  • Set-NetFirewallRule: Edits a firewall rule
  • Remove-NetFirewallRule: Deletes a firewall rule
  • New-NetFirewallRule: Creates a firewall rule

For detailed command syntax and usage, refer to the Get-NetFirewallRule and New-NetFirewallRule cmdlets.

How to Create a Firewall Rule

To create a new firewall rule, you can use the following command:

New-NetFirewallRule -DisplayName "My Example Rule" -Direction Inbound -Program "C:\\Temp\\MyApp.exe" -Action Allow -Profile Domain,Private -Protocol TCP -LocalPort 8080

screen shot showing how to create a new firewall

Image courtesy of ITPro Today

You can verify the creation of the rule using:

Get-NetFirewallRule -DisplayName "My Example Rule"

How to Modify a Firewall Rule

To modify an existing firewall rule, use the following command:

Set-NetFirewallRule -DisplayName "My Example Rule" -Profile Domain

screenshot showing modification of firewall rule

Image courtesy of ITPro Today

How to Delete a Firewall Rule

To delete a firewall rule, use the command:

Remove-NetFirewallRule -DisplayName "My Example Rule"

screenshot showing how to delete a firewall rule

Image courtesy of ITPro Today

By utilizing these commands and techniques, you can effectively manage Windows firewall rules using PowerShell.

For more information and to explore our services, visit Gopher Security.

Edward Zhou
Edward Zhou

CEO & Founder

 

CEO & Founder of Gopher Security, leading the development of Post-Quantum cybersecurity technologies and solutions..

Related Articles

Ransomware Attacks Target Russian Vodka and Healthcare Sectors

The Novabev Group, parent company of the Beluga vodka brand, experienced a ransomware attack on July 14, 2025, causing significant disruptions. The attack affected WineLab, the company's liquor store chain, leading to a three-day closure of over 2,000 locations in Russia. The company reported that the attack crippled its IT infrastructure, particularly point-of-sale systems and online services. Novabev Group stated, "The company maintains a principled position of rejecting any interaction with cybercriminals and refuses to fulfill their demands."

By Alan V Gutnov July 19, 2025 3 min read
Read full article

Retail Sector Faces Surge in Ransomware Attacks: A 2025 Analysis

Publicly disclosed ransomware attacks on the retail sector globally surged by 58% in Q2 2025 compared to Q1, with UK-based firms being particularly targeted, according to a report by BlackFog. This spike in attacks follows high-profile breaches affecting retailers like Marks & Spencer (M&S), The Co-op, and Harrods, attributed to the threat actor known as Scattered Spider.

By Alan V Gutnov July 19, 2025 2 min read
Read full article

AI-Driven Lcryx Ransomware Emerges in Cryptomining Botnet

A cryptomining botnet active since 2019 has incorporated a likely AI-generated ransomware known as Lcryx into its operations. Recent analysis by the FortiCNAPP team at FortiGuard Labs identified the first documented incident linking H2miner and Lcryx ransomware. This investigation focused on a cluster of virtual private servers (VPS) utilized for mining Monero.

By Edward Zhou July 19, 2025 3 min read
Read full article

Preventing ClickFix Attacks: Safeguarding Against Human Error

ClickFix is an emerging social engineering technique utilized by threat actors to exploit human error. This technique involves misleading users into executing malicious commands under the guise of providing "quick fixes" for common computer issues. Threat actors use familiar platforms and deceptive prompts to encourage victims to paste and run harmful scripts.

By Alan V Gutnov July 19, 2025 3 min read
Read full article