Tag: Windows Firewall

  • Create a New Firewall Rule with PowerShell

    Create a New Firewall Rule with PowerShell

    Create a New Firewall Rule with PowerShell

    This is a short post to share a PowerShell script that will create a new Windows Firewall Rule on a local computer.

    PowerShell Create New Firewall Rule

    We have to ensure that we run PowerShell as Administrator for this to work.

    # New firewall rule (run powershell as administrator)
    if (-not( Get-NetFirewallRule -DisplayName “Allow Inbound SQL (1433)” -ErrorAction SilentlyContinue)) { 
    New-NetFirewallRule `
        -DisplayName “Allow Inbound SQL - 1433” `
        -Direction Inbound `
        -Protocol TCP `
        -LocalPort 1433 `
        -Action Allow }
    PowerShell New Firewall Rule

    The above is creating a new firewall rule so incoming SQL Server (1433) traffic is allowed to happen on this computer.

    Other similar/useful posts I have on this include the following:
    # How to Check Windows Firewall Status with PowerShell
    # How to Enable Windows Firewall with PowerShell
    # How to Disable Windows Firewall with PowerShell

  • How to Check Windows Firewall Status with PowerShell

    How to Check Windows Firewall Status with PowerShell

    This post will help you to check your local Windows Firewall status using PowerShell (whether it’s off or on for each Profile).

    PowerShell commands follow standards and use verb-noun pairs for cmdlets. The verb at the start of the command describes the action the cmdlet performs, and the noun part is the action being performed. Here’s a list of Common Verbs, another Microsoft Docs link. The place of truth as I call it.

    In this demo, we’re running Get-NetFirewallProfile with Format-Table
    We’re getting the Firewall Profile status, and also formatting it into a table after a pipe ‘|’ –

    # get local firewall status powershell
    Get-NetFirewallProfile | Format-Table Name, Enabled
    Get-NetFirewallProfile

    The Windows Firewall on this machine is enabled, for all Profiles <- have a look at this MS Docs link to know more about Windows Firewall Profiles.

    Internal follow-up links:
    # How To Disable Windows Firewall With PowerShell
    # How To Enable Windows Firewall With PowerShell

  • How to Enable Windows Firewall with PowerShell

    How to Enable Windows Firewall with PowerShell

    This post is contains a demo on how to enable Windows Firewall with PowerShell in Windows Server.

    This might be needed if you have discovered Windows Firewall is disabled on your or multiple computers, or you might want to re-enable it after disabling this for a test (see my other post on disabling instead of enabling).

    Enabling Windows Firewall with PowerShell can be done by going through the following steps:

    # Understand Windows Firewall Profiles
    # Get-NetFirewallProfile PowerShell
    # Set-NetFirewallProfile PowerShell

    Understand Windows Firewall Profiles

    Before running any command or script on your machine, it’s important for us to understand everything that it’s doing. In the command below we are disabling all 3 profiles:

    # Domain – For when you are connected to a Domain Controller (computer connected to AD).
    # Private – For your home or private networks.
    # Public – For public WIFI locations such as coffee shops etc.

    For more information on this, see this link – Microsoft Docs: Windows Firewall Profiles

    Also remember, all of this can be viewed and changed via GUI to help with understandings >

    Enable Windows Firewall

    Get-NetFirewallProfile PowerShell

    PowerShell commands follow standards and use verb-noun pairs for cmdlets. The verb at the start of the command describes the action the cmdlet performs, and the noun part is the action being performed. Here’s a list of Common Verbs, another Microsoft Docs link. The place of truth as I call it.

    In this demo, we’re running Get-NetFirewallProfile with Format-Table
    We’re getting the Firewall Profile status, and also formatting it into a table after a pipe ‘|’ –

    # get local firewall status
    Get-NetFirewallProfile | Format-Table Name, Enabled
    Get-NetFirewallProfile

    In the example above, the Windows Firewall is disabled, showing as ‘False‘.

    Set-NetFirewallProfile PowerShell

    We’re switching the cmdlet we ran above from Get to Set here (following on from verb-noun cmdlet descriptions).

    To be able to run this; we have to open PowerShell or Windows Terminal as Administrator.

    Set-NetFirewallProfile is being executed below, which enables all Profiles of the Local Windows Firewall –

    # disable local firewall
    Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
    
    # get local firewall status
    Get-NetFirewallProfile | Format-Table Name, Enabled
    Set-NetFirewallProfile

    I followed the change by another run of Get-NetFirewallProfile to verify the change.

  • How to Disable Windows Firewall with PowerShell

    How to Disable Windows Firewall with PowerShell

    This post contains a demo on how to disable Windows Firewall with PowerShell on a Windows computer.

    Disabling the Local Firewall on Windows is not a recommended move, and it will ultimately make your computer less secure. Sometimes it’s necessary though, for example, during troubleshooting a difficult issue you might want to disable it for a quick test to rule the FW out as being the root cause. But in this example, you will have already tried to ensure the Firewall Rule exists before turning it off completely.

    Anyway, disabling Windows Firewall with PowerShell can be done by going through the following steps:

    # Understanding Firewall Profiles in Windows
    # Get Status of Windows Firewall with PowerShell
    # Disable Windows Firewall with PowerShell

    Understanding Firewall Profiles in Windows

    Before running any command or script on your machine, it’s important for us to understand everything that it’s doing. In the command below we are disabling all 3 profiles:

    # Domain – For when you are connected to a Domain Controller (computer connected to AD).
    # Private – For your home or private networks.
    # Public – For public WIFI locations such as coffee shops etc.

    For more information on this, see this link – Microsoft Docs: Windows Firewall Profiles

    Also remember, all of this can be viewed and changed via GUI to help with understandings >

    Disable Windows Firewall via GUI

    Get Status of Windows Firewall with PowerShell

    PowerShell commands follow standards and use verb-noun pairs for cmdlets. The verb at the start of the command describes the action the cmdlet performs, and the noun part is the action being performed. Here’s a list of Common Verbs, another Microsoft Docs link. The place of truth as I call it.

    In this demo, we’re running Get-NetFirewallProfile with Format-Table
    We’re getting the Firewall Profile status, and also formatting it into a table after a pipe ‘|’ –

    # get local firewall status
    Get-NetFirewallProfile | Format-Table Name, Enabled
    Get-NetFirewallProfile

    Disable Windows Firewall with PowerShell

    We’re switching the cmdlet we ran above from Get to Set here (following on from verb-noun cmdlet descriptions).

    To be able to run this; we have to open PowerShell or Windows Terminal as Administrator.

    Set-NetFirewallProfile is being executed below, which disables all Profiles of the Local Windows Firewall –

    # disable local firewall ps1
    Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False
    
    # get local firewall status ps1
    Get-NetFirewallProfile | Format-Table Name, Enabled
    Set-NetFirewallProfile

    I followed the change by another run of Get-NetFirewallProfile to verify the change.

    If this is a temporary change for you, to re-enable the Windows Firewall, amend the ‘-Enabled False‘ statement to ‘-Enabled True‘.