Skip to content

New-FirewallRule

Windows: Creates a new inbound or outbound firewall rule

#Requires -Version 5.1
#Requires -Modules NetSecurity

[CmdletBinding()]
Param
(
    [Parameter(Mandatory = $true)]
    [string]$DisplayName,

    [Parameter(Mandatory = $true)]
    [ValidateSet('Inbound', 'Outbound')]
    [string]$Direction,

    [Parameter(Mandatory = $true)]
    [ValidateSet('Allow', 'Block')]
    [string]$Action,

    [string]$Protocol,

    [string[]]$LocalPort,

    [string]$Program,

    [string]$Description,

    [string]$ComputerName = $env:COMPUTERNAME,

    [pscredential]$Credential
)

Process
{
    try
    {
        $session = $null
        $ruleParams = @{
            'DisplayName' = $DisplayName
            'Direction'   = $Direction
            'Action'      = $Action
            'Enabled'     = 'True'
            'ErrorAction' = 'Stop'
        }

        if (-not [string]::IsNullOrWhiteSpace($Protocol)) { $ruleParams.Add('Protocol', $Protocol) }
        if ($null -ne $LocalPort) { $ruleParams.Add('LocalPort', $LocalPort) }
        if (-not [string]::IsNullOrWhiteSpace($Program)) { $ruleParams.Add('Program', $Program) }
        if (-not [string]::IsNullOrWhiteSpace($Description)) { $ruleParams.Add('Description', $Description) }

        if ($ComputerName -ne $env:COMPUTERNAME)
        {
            $sessionParams = @{
                'ComputerName' = $ComputerName
            }
            if ($null -ne $Credential)
            {
                $sessionParams.Add('Credential', $Credential)
            }
            $session = New-CimSession @sessionParams
            $ruleParams.Add('CimSession', $session)
        }

        Write-Verbose "Creating firewall rule '$DisplayName' on '$ComputerName'..."
        $rule = New-NetFirewallRule @ruleParams

        $result = [PSCustomObject]@{
            DisplayName  = $rule.DisplayName
            Direction    = $rule.Direction
            Action       = $rule.Action
            Enabled      = $rule.Enabled
            ComputerName = $ComputerName
            Timestamp    = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch
    {
        throw
    }
    finally
    {
        if ($null -ne $session)
        {
            Remove-CimSession $session
        }
    }
}

Specifies the friendly name of the firewall rule.

Specifies if the rule applies to Inbound or Outbound traffic.

Specifies whether to Allow or Block the traffic.

Specifies the network protocol (e.g., TCP, UDP, ICMPv4).

Specifies the local port or port range (e.g., "80", "443", "5000-5005").

Specifies the path to the program file to which the rule applies.

Specifies the name of the target computer. Defaults to the local computer.

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.