{"id":290559,"date":"2021-07-01T14:45:00","date_gmt":"2021-07-01T11:45:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-manage-windows-firewall-rules-with-powershell-cloudsavvy-it\/"},"modified":"2021-07-01T14:45:00","modified_gmt":"2021-07-01T11:45:00","slug":"how-to-manage-windows-firewall-rules-with-powershell-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-manage-windows-firewall-rules-with-powershell-cloudsavvy-it\/","title":{"rendered":"#How to Manage Windows Firewall Rules with PowerShell \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3584ac02f08\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3584ac02f08\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-manage-windows-firewall-rules-with-powershell-cloudsavvy-it\/#Loading_the_NetSecurity_Module\" >Loading the NetSecurity Module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-manage-windows-firewall-rules-with-powershell-cloudsavvy-it\/#List_Existing_Firewall_Rules\" >List Existing Firewall Rules<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-manage-windows-firewall-rules-with-powershell-cloudsavvy-it\/#Create_a_New_Firewall_Rule\" >Create a New Firewall Rule<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-manage-windows-firewall-rules-with-powershell-cloudsavvy-it\/#Modify_an_Existing_Firewall_Rule\" >Modify an Existing Firewall Rule<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-manage-windows-firewall-rules-with-powershell-cloudsavvy-it\/#Remove_an_Existing_Firewall_Rule\" >Remove an Existing Firewall Rule<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-manage-windows-firewall-rules-with-powershell-cloudsavvy-it\/#Additional_Functionality\" >Additional Functionality<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-manage-windows-firewall-rules-with-powershell-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Manage Windows Firewall Rules with PowerShell \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-4374\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/23e4a5a4.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Powershell logo\" width=\"1400\" height=\"578\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Windows contains a robust, yet easy to use, advanced firewall, and using PowerShell 7 we can easily configure the firewall from the command line.\u00a0This article covers common commands used in the Windows Firewall and where they may be used.<\/p>\n<p>The module NetSecurity is well <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/?view=win10-ps\">documented<\/a>. Keep in mind that this article only <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lies to the Windows operating system. For other operating systems, there are other command-line tools that can be used to do the same type of functions such as <code>UFW<\/code> or <code>IPTables<\/code> on Linux.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Loading_the_NetSecurity_Module\"><\/span>Loading the <code>NetSecurity<\/code> Module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>NetSecurity<\/code> module, built-in and offered by Microsoft, contains all of the functionality needed to add, remove, and modify firewall rules. To load the module, simply import the module as shown below.<\/p>\n<pre><code>Import-Module -Name 'NetSecurity'&#13;\n<\/code><\/pre>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"List_Existing_Firewall_Rules\"><\/span>List Existing Firewall Rules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The cmdlet, <code>Get-NetFirewallRule<\/code> will show all existing firewall rules. There are many, by default, so to demonstrate, we output the first 10.<\/p>\n<pre><code>Get-NetFirewallRule | Select-Object DisplayName, Enabled, Direction, Action -First 10&#13;\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4270\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/dbfa394e.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"941\" height=\"281\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/code><\/pre>\n<p>There are many properties that are returned by <code>Get-NetFirewallRule<\/code>. Though we list only a properties above, running <code>Get-NetFirewallRule | Select-Object * -First 1<\/code>, will list all available.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4271\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/c532d1db.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"996\" height=\"937\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Create_a_New_Firewall_Rule\"><\/span>Create a New Firewall Rule<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are many different ways to create a new Firewall rule but the command that does this is <code>[Net-NewFirewallRule](&lt;https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/new-netfirewallrule?view=win10-ps&gt;)<\/code>. The basic properties that need to be filled in are:<\/p>\n<ul>\n<li><code>DisplayName<\/code> \u2013 The friendly name of the firewall rule<\/li>\n<li><code>Direction<\/code> \u2013 Whether to block traffic leaving the computer <code>Outbound<\/code> or coming into the computer <code>Inbound<\/code><\/li>\n<li><code>Action<\/code> \u2013 What action to take if the rule is met, <code>Allow<\/code> or <code>Block<\/code><code\/><\/li>\n<\/ul>\n<p><code>$Params = @{ \"DisplayName\" = 'Block WINS' \"Direction\" = 'Inbound' \"Action\" = 'Block' \"RemoteAddress\" = 'WINS' }<\/code><\/p>\n<p>New-NetFirewallRule @Params<br \/>If the <code>Name<\/code> parameter is not used, then a random GUID is used. The <code>DisplayName<\/code> may be human readable but the <code>Name<\/code> itself assigned a random GUID.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Modify_an_Existing_Firewall_Rule\"><\/span>Modify an Existing Firewall Rule<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>What if we want to modify an existing rule without removing and recreating the rule entirely. To do so, we should run the <code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/set-netfirewallrule?view=win10-ps\">Set-NetFirewallRule<\/a><\/code>, and will allow us to modify the firewall rule as necessary.<\/p>\n<pre><code>$Params = @{&#13;\n    \"DisplayName\"   = 'Block WINS'&#13;\n    \"Action\"        = 'Allow'&#13;\n}&#13;\n&#13;\nSet-NetFirewallRule @Params&#13;\n<\/code><\/pre>\n<p>Other useful abilities that the <code>Set-NetFirewallRule<\/code> has is the ability to operate on multiple rules at once. This can be done by locating rules by one of three parameters.<\/p>\n<ul>\n<li><code>Name<\/code>This is the default and if names are set in via the pipeline or a string array then each will acted upon.<\/li>\n<li><code>DisplayName<\/code>Similar to <code>Name<\/code>, multiple pipelined objects or a string array will modify those rules accordingly.<\/li>\n<li><code>DisplayGroup<\/code> or <code>Group<\/code>If rules are grouped together, all of those rules grouped can be acted upon at once.<\/li>\n<\/ul>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Remove_an_Existing_Firewall_Rule\"><\/span>Remove an Existing Firewall Rule<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Finally, we would like to remove the existing rule as it may no longer be needed. To do this, run the command <code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/remove-netfirewallrule?view=win10-ps\">Remove-NetFirewallRule<\/a><\/code>. When you do so, it is often wise to use the <code>WhatIf<\/code> parameter to verify that the rule is the correct one to remove.<\/p>\n<pre><code>Remove-NetFirewallRule -DisplayName \"Block WINS\"&#13;\n<\/code><\/pre>\n<p>It\u2019s important to note that the <code>Remove-NetFirewallRule<\/code> can remove multiple rules at once. An example of this type of functionality is below. The below rule will remove all disabled rules contained within the policy <code>firewall_gpo<\/code> in the <code>ad.local.test<\/code> domain.<\/p>\n<pre><code>Remove-NetFirewallRule -Enabled 'False' -PolicyStore 'ad.local.test\\firewall_gpo'&#13;\n<\/code><\/pre>\n<p>A useful command, but potentially dangerous, is running <code>Remove-NetFirewallFule<\/code> by itself which removes all of the static local firewall rules that have been created. If you have a domain GPO that defines firewall rules, this will remove any that may conflict with those GPO defined rules.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Additional_Functionality\"><\/span>Additional Functionality<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are many other commands available within the <code>NetSecurity<\/code> module. Though we don\u2019t cover them all here, a few notable commands are shown below to demonstrate how extensive the module is.<\/p>\n<ul>\n<li><code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/copy-netfirewallrule?view=win10-ps\">Copy-NetFirewallRule<\/a><\/code>This command will copy an existing firewall rule and all associated filters to the same or different policy store.<\/li>\n<li><code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/disable-netfirewallrule?view=win10-ps\">Disable-NetFirewallRule<\/a><\/code>This will disable a previously enabled firewall rule. The rule will still exist, but not actively modify any network data.If you run this command without any parameters, it will disable all active rules on the target computer. It is advised to always run this command with the <code>WhatIf<\/code> parameter if not targeting a specific rule or set of rules.<\/li>\n<li><code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/enable-netfirewallrule?view=win10-ps\">Enable-NetFirewallRule<\/a><\/code>Like the <code>Disable-NetFirewallRule<\/code>, this command will enable a previously disabled rule or set of rules.If this command is run without any parameters it will enable all previously disabled rules. It is advised to always run this command with the <code>WhatIf<\/code> parameter if not targeting a specific rule or set of rules.<\/li>\n<li><code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/get-netfirewallprofile?view=win10-ps\">Get-NetFirewallProfile<\/a><\/code>This command shows the currently configured options for a specified profile, such as the <code>Domain<\/code>, <code>Private<\/code>, or <code>Public<\/code> profiles.<\/li>\n<li><code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/get-netfirewallsetting?view=win10-ps\">Get-NetFirewallSetting<\/a><\/code>The global firewall settings can be retrieved by using the <code>Get-NetFirewallSetting<\/code> command. These settings include such options as certificate options, packet queueing, or authorization lists.<\/li>\n<li><code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/rename-netfirewallrule?view=win10-ps\">Rename-NetFirewallRule<\/a><\/code>To rename an existing firewall rule, use the <code>Rename-NetFirewallRule<\/code> command. This is useful if a rule was created without a specified name, thereby receiving a random GUID as it\u2019s name, and it is preferred to have a human-readable name assigned.<\/li>\n<li><code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/set-netfirewallprofile?view=win10-ps\">Set-NetFirewallProfile<\/a><\/code>To set specific settings for individual profiles, use the <code>Set-NetFirewallProfile<\/code> command. This allows each profile to have distinct settings.<\/li>\n<li><code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/set-netfirewallsetting?view=win10-ps\">Set-NetFirewallSetting<\/a><\/code>This command configures global firewall behaviors that apply regardless of the network profile currently in use.<\/li>\n<li><code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/netsecurity\/show-netfirewallrule?view=win10-ps\">Show-NetFirewallRule<\/a><\/code>This helper command will show the firewall rules and their associated objects in a formatted list.<\/li>\n<\/ul>\n<p>There is extensive IPSec functionality contained within the module. The commands listed above are those that operate on the standard Windows Firewall settings.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are many available commands for managing the Windows Firewall. This article only touches on a few of them, notably the most important commands to quickly list, create, modify, and remove firewall rules. Even complex firewall configurations can be accomplished strictly through the command line using the <code>NetSecurity<\/code> PowerShell module!\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/4269\/managing-firewall-rules-with-powershell-in-windows\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Manage Windows Firewall Rules with PowerShell \u2013 CloudSavvy IT&#8221; Windows contains a robust, yet easy to use, advanced firewall, and using PowerShell 7 we can easily configure the firewall from the command line.\u00a0This article covers common commands used in the Windows Firewall and where they may be used. The module NetSecurity is well&#8230;<\/p>\n","protected":false},"author":1,"featured_media":290560,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/23e4a5a4.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-290559","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/290559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=290559"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/290559\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/290560"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=290559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=290559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=290559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}