{"id":809288,"date":"2026-05-18T06:56:29","date_gmt":"2026-05-18T06:56:29","guid":{"rendered":"https:\/\/www.ninjaone.com\/?post_type=script_hub&#038;p=809288"},"modified":"2026-05-18T06:56:29","modified_gmt":"2026-05-18T06:56:29","slug":"enable-disable-windows-firewall-profiles-powershell","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/pt-br\/script-hub\/enable-disable-windows-firewall-profiles-powershell\/","title":{"rendered":"Script do PowerShell para ativar ou desativar perfis de firewall do Windows"},"content":{"rendered":"<p>Garantir a seguran\u00e7a de redes e sistemas \u00e9 a base da TI. Uma das principais defesas em um ambiente Windows \u00e9 o Firewall do Windows. Ao controlar o fluxo de tr\u00e1fego de entrada e sa\u00edda, ele atua como um gatekeeper. Este artigo se aprofunda em um <strong>script do PowerShell que <\/strong><strong>fornece<\/strong><strong> um m\u00e9todo simplificado para ativar ou desativar perfis do Firewall do Windows<\/strong>, uma tarefa crucial para os profissionais de TI.<\/p>\n<h2>Hist\u00f3rico<\/h2>\n<p>O script apresentado foi projetado para ativar ou desativar todos os perfis do Firewall do Windows, ou seja, Dom\u00ednio, P\u00fablico e Privado. Esses perfis determinam as configura\u00e7\u00f5es e regras aplicadas com base no tipo de rede ao qual um computador est\u00e1 conectado. Para os provedores de servi\u00e7os gerenciados (MSPs) e profissionais de TI, uma ferramenta que possa alternar rapidamente esses perfis \u00e9 inestim\u00e1vel. Seja para solu\u00e7\u00e3o de problemas, refor\u00e7o de seguran\u00e7a ou configura\u00e7\u00e3o de rede, esse script oferece uma solu\u00e7\u00e3o r\u00e1pida.<\/p>\n<h2>O roteiro<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\">#Requires -Version 5.1\r\n\r\n&lt;#\r\n.SYNOPSIS\r\n    Enable or disable all Windows Firewall profiles(Domain, Public, Private).\r\n.DESCRIPTION\r\n    Enable or disable all Windows Firewall profiles(Domain, Public, Private).\r\n.EXAMPLE\r\n     -Disable\r\n    Disables all Windows Firewall profiles(Domain, Public, Private).\r\n.EXAMPLE\r\n     -Enable\r\n    Enables all Windows Firewall profiles(Domain, Public, Private).\r\n.EXAMPLE\r\n     -Enable -BlockAllInbound\r\n    Enables all Windows Firewall profiles(Domain, Public, Private).\r\n    Blocks all inbound traffic on the Domain, Public, Private profiles\r\n.OUTPUTS\r\n    String[]\r\n.OUTPUTS\r\n    PSCustomObject[]\r\n.NOTES\r\n    Minimum OS Architecture Supported: Windows 10, Windows Server 2016\r\n    Release Notes:\r\n    Initial Release\r\nBy using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https:\/\/www.ninjaone.com\/terms-of-use.\r\n    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. \r\n    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. \r\n    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. \r\n    Warranty Disclaimer: The script is provided \u201cas is\u201d and \u201cas available\u201d, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. \r\n    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. \r\n    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. \r\n    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).\r\n.COMPONENT\r\n    ProtocolSecurity\r\n#&gt;\r\n\r\n[CmdletBinding(DefaultParameterSetName = \"Enable\")]\r\nparam (\r\n    [Parameter(\r\n        Mandatory = $true,\r\n        ParameterSetName = \"Enable\"\r\n    )]\r\n    [Switch]\r\n    $Enable,\r\n    [Parameter(\r\n        Mandatory = $true,\r\n        ParameterSetName = \"Disable\"\r\n    )]\r\n    [Switch]\r\n    $Disable,\r\n    [Parameter(\r\n        ParameterSetName = \"Enable\"\r\n    )]\r\n    [Switch]\r\n    $BlockAllInbound\r\n)\r\n\r\nbegin {\r\n    function Test-IsElevated {\r\n        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()\r\n        $p = New-Object System.Security.Principal.WindowsPrincipal($id)\r\n        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)\r\n    }\r\n}\r\nprocess {\r\n    if (-not $(Test-IsElevated)) {\r\n        Write-Error -Message \"Access Denied. Please run with Administrator privileges.\"\r\n        exit 1\r\n    }\r\n    if ($(Get-Command \"Get-NetFirewallProfile\" -ErrorAction SilentlyContinue).Name -like \"Get-NetFirewallProfile\") {\r\n        # Use Get-NetFirewallProfile if available\r\n        try {\r\n            $NetFirewallSplat = @{\r\n                Profile     = @(\"Domain\", \"Public\", \"Private\")\r\n                Enabled     = $(if ($Enable) { \"True\" }elseif ($Disable) { \"False\" })\r\n                ErrorAction = \"Stop\"\r\n            }\r\n            if ($Enable -and $BlockAllInbound) {\r\n                $NetFirewallSplat.Add('DefaultInboundAction', 'Block')\r\n                $NetFirewallSplat.Add('DefaultOutboundAction', 'Allow')\r\n            }\r\n            Set-NetFirewallProfile @NetFirewallSplat\r\n            \r\n        }\r\n        catch {\r\n            Write-Error $_\r\n            Write-Host \"Failed to turn $(if ($Enable) { \"on\" }elseif ($Disable) { \"off\" }) the firewall.\"\r\n            exit 1\r\n        }\r\n        # Proof of work\r\n        Get-NetFirewallProfile -ErrorAction Stop | Format-Table Name, Enabled        \r\n    }\r\n    else {\r\n        # Fall back onto netsh\r\n        netsh.exe AdvFirewall set AllProfiles state $(if ($Enable) { \"on\" }elseif ($Disable) { \"off\" })\r\n        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n        netsh.exe AdvFirewall set DomainProfile state $(if ($Enable) { \"on\" }elseif ($Disable) { \"off\" })\r\n        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n        netsh.exe AdvFirewall set PrivateProfile state $(if ($Enable) { \"on\" }elseif ($Disable) { \"off\" })\r\n        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n        netsh.exe AdvFirewall set PublicProfile state $(if ($Enable) { \"on\" }elseif ($Disable) { \"off\" })\r\n        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n        \r\n        if ($Enable -and $BlockAllInbound) {\r\n            try {\r\n                netsh.exe AdvFirewall set DomainProfile FirewallPolicy \"BlockInbound,AllowOutbound\"\r\n                if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n                netsh.exe AdvFirewall set PrivateProfile FirewallPolicy \"BlockInbound,AllowOutbound\"\r\n                if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n                netsh.exe AdvFirewall set PublicProfile FirewallPolicy \"BlockInbound,AllowOutbound\"\r\n                if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n            }\r\n            catch {\r\n                Write-Error $_\r\n                Write-Host \"Could not set Block All Inbound Traffic to 1\"\r\n            }\r\n        }\r\n        # Proof of work\r\n        netsh.exe AdvFirewall show AllProfiles state\r\n        if ($LASTEXITCODE -gt 0) { exit $LASTEXITCODE }\r\n    }\r\n}\r\nend {}<\/pre>\n<p>&nbsp;<\/p>\n\n<div class=\"in-context-cta\"><p>Acesse mais de 300 scripts no NinjaOne Dojo<\/p>\n<p><a href=\"https:\/\/www.ninjaone.com\/freetrialform\/\">Obter acesso<\/a><\/p>\n<\/div>\n<h2>Detalhamento<\/h2>\n<p>Em sua ess\u00eancia, o script verifica se h\u00e1 privil\u00e9gios de administrador, o que \u00e9 essencial, pois a altera\u00e7\u00e3o das configura\u00e7\u00f5es do firewall exige direitos elevados. Em seguida, ele verifica a presen\u00e7a do cmdlet Get-NetFirewallProfile, um comando moderno do PowerShell para gerenciar perfis de firewall.<\/p>\n<p>Se esse cmdlet estiver dispon\u00edvel, o script o utilizar\u00e1 para ativar ou desativar os perfis especificados. Uma op\u00e7\u00e3o para bloquear todo o tr\u00e1fego de entrada, embora permita o tr\u00e1fego de sa\u00edda, acrescenta uma camada extra de seguran\u00e7a.<\/p>\n<p>Na aus\u00eancia do cmdlet Get-NetFirewallProfile, o script reverte para a ferramenta de linha de comando netsh.exe mais antiga.<\/p>\n<h2>Casos de uso em potencial<\/h2>\n<p>Considere uma profissional de TI, Jane, em uma grande corpora\u00e7\u00e3o. Eles est\u00e3o introduzindo um novo aplicativo, mas, durante os testes, descobrem que o aplicativo n\u00e3o consegue se comunicar com o servidor. Suspeitando de um problema de firewall, Jane usa esse script para desativar temporariamente os perfis de firewall, testar o aplicativo e, em seguida, reativ\u00e1-los imediatamente. Essa a\u00e7\u00e3o r\u00e1pida ajuda a diagnosticar o problema sem navega\u00e7\u00e3o manual.<\/p>\n<h2>Compara\u00e7\u00f5es<\/h2>\n<p>O script fornece uma abordagem program\u00e1tica para gerenciar perfis de firewall. As alternativas incluem o ajuste manual por meio da GUI do Firewall do Windows ou o uso de GPOs (Group Policy Objects, Objetos de Pol\u00edtica de Grupo) para m\u00e1quinas unidas por dom\u00ednio. No entanto, ambos n\u00e3o t\u00eam o imediatismo desse roteiro.<\/p>\n<h2>Implica\u00e7\u00f5es de seguran\u00e7a<\/h2>\n<p>A capacidade de alternar rapidamente os perfis de firewall \u00e9 uma faca de dois gumes. Desativ\u00e1-los, mesmo que momentaneamente, pode expor os sistemas a amea\u00e7as. \u00c9 fundamental compreender as implica\u00e7\u00f5es de seguran\u00e7a e garantir que os sistemas permane\u00e7am protegidos.<\/p>\n<h2>Recomenda\u00e7\u00f5es<\/h2>\n<ul>\n<li>Teste o script em um ambiente controlado primeiro.<\/li>\n<li>Se voc\u00ea estiver desativando o firewall para diagn\u00f3stico, reative-o imediatamente depois.<\/li>\n<li>Revisar regularmente as regras de firewall para garantir o alinhamento com as pol\u00edticas de seguran\u00e7a.<\/li>\n<\/ul>\n<h2>Considera\u00e7\u00f5es finais<\/h2>\n<p>O gerenciamento dos perfis do Firewall do Windows \u00e9 essencial para a seguran\u00e7a da rede e do sistema. Embora ferramentas como o NinjaOne ofere\u00e7am <a href=\"https:\/\/www.ninjaone.com\/pt-br\/\">solu\u00e7\u00f5es abrangentes de gerenciamento de TI<\/a>, scripts como o discutido s\u00e3o inestim\u00e1veis para tarefas espec\u00edficas. Como sempre, entender seu funcionamento e suas implica\u00e7\u00f5es garante seu uso eficaz e seguro.<\/p>\n","protected":false},"author":35,"featured_media":142468,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","_lmt_disableupdate":"no","_lmt_disable":""},"operating_system":[4212],"use_cases":[12456],"class_list":["post-809288","script_hub","type-script_hub","status-publish","has-post-thumbnail","hentry","script_hub_category-windows"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/script_hub\/809288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/comments?post=809288"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/media\/142468"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/media?parent=809288"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/operating_system?post=809288"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/use_cases?post=809288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}