{"id":1647,"date":"2014-04-08T00:01:00","date_gmt":"2014-04-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/04\/08\/windows-server-2012-r2-network-cmdlets-part-2\/"},"modified":"2014-04-08T00:01:00","modified_gmt":"2014-04-08T00:01:00","slug":"windows-server-2012-r2-network-cmdlets-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/windows-server-2012-r2-network-cmdlets-part-2\/","title":{"rendered":"Windows Server 2012 R2 Network Cmdlets: Part 2"},"content":{"rendered":"<p><b>Summary<\/b>: Use Windows PowerShell to enable, disable, and configure firewall rules in Windows Server&nbsp;2012&nbsp;R2.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy!<\/p>\n<p>I see there are some new Windows PowerShell cmdlets for managing the built-in Windows Server&nbsp;2012&nbsp;R2 firewall. Could you lend a friend a hand and give me some help in learning how to use them?<\/p>\n<p>&mdash;JD<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello JD,<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here, delving further into the wonderful world of Windows PowerShell and the new network cmdlets. This is the second part in a series called Windows PowerShell Network Week. You also might enjoy reading <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/windows-server-2012-r2-network-cmdlets-part-1\/\" target=\"_blank\">Windows Server 2012 R2 Network Cmdlets: Part&nbsp;1<\/a>.<\/p>\n<p>Lend a hand? More than happy to! If there is anything I can do to help take away some stress from a fellow IT pro through Windows PowerShell, I&rsquo;m quite happy to oblige.<\/p>\n<p>So first, let&rsquo;s do something fairly simple. We&rsquo;ll pull up a list of the firewall rules with the <b>Get-NetFirewallRule<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallRule<\/p>\n<p>&hellip;.and as I watch twelve hundred lines of raw data fly by, I realize that wasn&rsquo;t such a bright idea. So we&rsquo;ll pipe the output to <b>Format-Table<\/b> and make this a bit more viewable:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2630.1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2630.1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallRule | Format-Table<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5518.2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5518.2.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I initially decided to filter on only those that were <b>Enabled<\/b>. But seeing True and False in the output, I &ldquo;assumed&rdquo; it was Boolean. So I tried this line:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallRule | Where { $_.Enabled &ndash;eq $TRUE }<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7506.3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7506.3.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>As you can see, that didn&rsquo;t work very well. So turn the brain back on and run the output through <b>Get-Member<\/b> to see what the output actually is. I gave this a quick shot. Perhaps a string with the word <b>True<\/b>? Naaaaaahhhhh&hellip;<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallRule | Where { $_.Enabled &ndash;eq &lsquo;TRUE&rsquo; }<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1643.4.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1643.4.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Victory!<\/p>\n<p>So how about getting those rules grouped by type? We have <b>Domain<\/b>, <b>Public<\/b>, and <b>Private<\/b> profiles. How would you know if somebody created a custom profile? We can access that information with the <b>Get-NetFirewallProfile<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallProfile | Format-Table<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5773.5.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5773.5.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>We can even take this and filter on only <b>Private<\/b> and show the firewall rules within that criteria:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallProfile &ndash;name &lsquo;Private&rsquo; | Get-NetfirewallRule<\/p>\n<p>But working with the firewall isn&rsquo;t only about if auditing has been enabled (although that is certainly handy to know). Sometimes you have to quickly enable some built in firewall rules. For example, if your server running Hyper-V wasn&rsquo;t enabled, you could first find all the rules based on Hyper-V:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallRule &ndash;displaygroup *Hyper-V*<\/p>\n<p>This would show all rules within that <b>DisplayGroup<\/b> where <b>Hyper-V<\/b> was anywhere in the name. If you want to ensure those rules were enabled in general, you could pipe them to the <b>Set-NetFirewallRule<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallRule &ndash;displaygroup *Hyper-V* | Set-NetFirewallRule &ndash;enabled True<\/p>\n<p>Now here&rsquo;s why I love Windows PowerShell for working with the firewall. Netsh.exe worked. But Windows PowerShell works EASILY!<\/p>\n<p>Here&rsquo;s one task I am forever doing computers running SQL Server after an installation because Port 1433 needs to be opened in my labs. I used to go into the GUI and &ldquo;clickety clickety clickety&hellip;oops&hellip;clickety clickety&hellip;&rdquo; But in Windows Server&nbsp;2012&nbsp;R2, I can simply use the <b>Net-NetFirewallRule<\/b> cmdlet.<\/p>\n<p>We&rsquo;re going to enable Port 1433 as an inbound rule, and name it <b>SQL Inbound<\/b>. Don&rsquo;t blink because it will be done before you know it.<\/p>\n<p style=\"margin-left:30px\">New-NetFirewallRule &ndash;Rule &lsquo;Sql Inbound&rsquo; &ndash;Displayname &lsquo;Sql Inbound&rsquo; &ndash;protocol TCP &ndash;localport 1433 &ndash;enabled True<\/p>\n<p>This was cool. But of course, I can easily disable it by doing this:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallRule &lsquo;SQL Inbound&rsquo; | Disable-NetFirewallRule<\/p>\n<p>I could even easily remove it in this manner:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallRule &lsquo;SQL Inbound&rsquo; | Remove-NetFirewallRule<\/p>\n<p>Let&rsquo;s imagine a scenario where the rule exists, but you don&rsquo;t know the name. I would say that&rsquo;s more typical if you&rsquo;re a consultant walking in. Somebody says, &ldquo;Show me any firewall rules that exist that enable Port 1433, and do it quickly!&rdquo;<\/p>\n<p>If you run the <b>Get-NetFirewallPortFilter<\/b> cmdlet, you can see all of the ports that are already defined in the Windows Server&nbsp;2012&nbsp;R2 firewall:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallPortFilter<\/p>\n<p>You can take this output, and filter it down to what you want:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallPortFilter &ndash;Protocol TCP | Where { $_.localport &ndash;eq &lsquo;1433&rsquo; }<\/p>\n<p>Then pipe that into the <b>Get-NetFirewallRule<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">Get-NetFirewallPortFilter &ndash;Protocol TCP | Where { $_.localport &ndash;eq &lsquo;1433&rsquo; } | Get-NetFirewallRule<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3125.6.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3125.6.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now you&rsquo;re cooking!<\/p>\n<p>Pop back in tomorrow, JD, and I&rsquo;ll throw more hot cmdlets on the grill!<\/p>\n<p>I invite you to follow The Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send an email to The Scripting Guys at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then remember eat your cmdlets each and every day with a taste dash of creativity.<\/p>\n<p><b>Sean Kearney<\/b>, Windows PowerShell MVP, Honorary Scripting Guy<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Windows PowerShell to enable, disable, and configure firewall rules in Windows Server&nbsp;2012&nbsp;R2. &nbsp;Hey, Scripting Guy! I see there are some new Windows PowerShell cmdlets for managing the built-in Windows Server&nbsp;2012&nbsp;R2 firewall. Could you lend a friend a hand and give me some help in learning how to use them? &mdash;JD &nbsp;Hello JD, Honorary [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,37,154,45],"class_list":["post-1647","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-networking","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use Windows PowerShell to enable, disable, and configure firewall rules in Windows Server&nbsp;2012&nbsp;R2. &nbsp;Hey, Scripting Guy! I see there are some new Windows PowerShell cmdlets for managing the built-in Windows Server&nbsp;2012&nbsp;R2 firewall. Could you lend a friend a hand and give me some help in learning how to use them? &mdash;JD &nbsp;Hello JD, Honorary [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=1647"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1647\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=1647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}