{"id":4757,"date":"2012-10-28T00:01:00","date_gmt":"2012-10-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/10\/28\/weekend-scripter-playing-around-with-windows-firewall\/"},"modified":"2012-10-28T00:01:00","modified_gmt":"2012-10-28T00:01:00","slug":"weekend-scripter-playing-around-with-windows-firewall","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-playing-around-with-windows-firewall\/","title":{"rendered":"Weekend Scripter: Playing Around with Windows Firewall"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to work with Windows Firewall on Windows 8&nbsp;and Windows Server&nbsp;2012.\nMicrosoft Scripting Guy, Ed Wilson, is here. The Scripting Wife returned home from the <a href=\"http:\/\/powershellsaturday.com\/003\/\" target=\"_blank\">Windows PowerShell Saturday #003 event<\/a> in Atlanta, Georgia, last night, and&nbsp;she was&nbsp;exhausted&mdash;happy, but exhausted. First of all we had pre-ordered a Surface for her that was delivered after she left for Alpharetta, but&nbsp; PowerShell MVP Jim Christopher came to the rescue. Jim was kind enough to stop by the house and pick up the Surface and hand delivered it to the Scripting Wife Friday evening. Speaking of rescue and speaking, (pun intended) I want to heartily thank Glenn Sizemore and Robert Cain for delivering the two sessions that I had planned on presenting. I hear both gentlemen did a wonderful job. The event was packed, and Mark Schill and the crew did a fantastic job organizing a flawless Windows PowerShell Saturday. Well done.\nIf you follow the Scripting Wife on Facebook or Twitter, you know I have not been able to eat or drink anything hot for a week&mdash;that means no hot tea&mdash;and it has been driving me crazy. So, I am sitting on the lanai sipping a cool glass of water&mdash;not quite the same as English Breakfast Tea&mdash;not by a long shot.\nAnyway, one of the things I have wanted to play with is the cmdlets for Windows Firewall. With nothing on the agenda but a murder mystery, written by my good friend and mentor <a href=\"http:\/\/elizabethterrell.com\/\" target=\"_blank\">Jaden Terrell<\/a>, and a glass of cool water, today is the day.<\/p>\n<h2>Windows&nbsp;8 Firewall cmdlets&mdash;a quick look<\/h2>\n<p>I am a huge fan of Windows Firewall because it works well, provides a measure of in-depth security, and comes with the operating system. In fact, I rarely find firewall-related issues, and, therefore, I do not turn it off&mdash;in fact, I leave it running on both the desktop and the server.\nThe first thing to do when working with the firewall is to determine the network connection profile because this determines the way the firewall policies work. In Windows&nbsp;8 and Windows Server&nbsp;2012, the <b>Get-NetConnectionProfile<\/b> cmdlet is extremely useful for this task. I first enumerate my network adapters, find the ones that are up, and then get the network connection profile. The commands are shown here.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b> &nbsp;&nbsp;I use the error action of 0 to remove errors about connection profiles for virtual adapters that are not connected to a network but are considered to be up. I also use the simple <b>Where-Object<\/b> syntax (<b>?<\/b> is an alias for the <b>Where-Object<\/b> cmdlet). Keep in mind when working with Windows PowerShell cmdlets that you have tab completion, and it greatly simplifies typing commands.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-NetAdapter | ? status -EQ &#8216;up&#8217; | Get-NetConnectionProfile &ndash;ea 0<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Unidentified network<\/p>\n<p style=\"padding-left: 30px\">InterfaceAlias&nbsp;&nbsp; : vEthernet (InternalSwitch)<\/p>\n<p style=\"padding-left: 30px\">InterfaceIndex&nbsp;&nbsp; : 19<\/p>\n<p style=\"padding-left: 30px\">NetworkCategory&nbsp; : Public<\/p>\n<p style=\"padding-left: 30px\">IPv4Connectivity : NoTraffic<\/p>\n<p style=\"padding-left: 30px\">IPv6Connectivity : NoTraffic<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : iammred.net<\/p>\n<p style=\"padding-left: 30px\">InterfaceAlias&nbsp;&nbsp; : vEthernet (External Switch)<\/p>\n<p style=\"padding-left: 30px\">InterfaceIndex&nbsp;&nbsp; : 23<\/p>\n<p style=\"padding-left: 30px\">NetworkCategory&nbsp; : DomainAuthenticated<\/p>\n<p style=\"padding-left: 30px\">IPv4Connectivity : Internet<\/p>\n<p style=\"padding-left: 30px\">IPv6Connectivity : LocalNetwork\nTo find the names and the status of the various Windows Firewall profiles, I use the <b>Get-NetFirewallProfile<\/b> cmdlet. I pipe the results to the <b>Format-Table<\/b> cmdlet (<b>ft<\/b> is the alias), and I choose only the name and the enabled properties. I then use the <b>autosize<\/b><i> <\/i>switch to tighten up the display. The command and results are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-NetFirewallProfile | ft name, enabled -auto<\/p>\n<p style=\"padding-left: 30px\">name&nbsp;&nbsp;&nbsp; Enabled<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp; &#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Domain&nbsp;&nbsp;&nbsp;&nbsp; True<\/p>\n<p style=\"padding-left: 30px\">Private&nbsp;&nbsp;&nbsp; True<\/p>\n<p style=\"padding-left: 30px\">Public&nbsp;&nbsp;&nbsp;&nbsp; True\nNow for the first frustration: Except for the <i>public<\/i> network category, the value of the network category and the name of the firewall profile do not match up, and, therefore, it prevents piping. In this case, I cannot use <b>Get-NetworkAdapter<\/b> to get my network adapters, pipe it to the <b>Get-NetConnectionProfile<\/b> cmdlet, and then pipe it to the <b>Get-NetFireWallProfile<\/b> cmdlet. However, I can focus on the details of a specific firewall profile. Here are the details of the public network profile.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; Get-NetFirewallProfile public<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Public<\/p>\n<p style=\"padding-left: 30px\">Enabled&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : True<\/p>\n<p style=\"padding-left: 30px\">DefaultInboundAction&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotConfigured<\/p>\n<p style=\"padding-left: 30px\">DefaultOutboundAction&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotConfigured<\/p>\n<p style=\"padding-left: 30px\">AllowInboundRules&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotConfigured<\/p>\n<p style=\"padding-left: 30px\">AllowLocalFirewallRules&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotConfigured<\/p>\n<p style=\"padding-left: 30px\">AllowLocalIPsecRules&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotConfigured<\/p>\n<p style=\"padding-left: 30px\">AllowUserApps&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotConfigured<\/p>\n<p style=\"padding-left: 30px\">AllowUserPorts&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotConfigured<\/p>\n<p style=\"padding-left: 30px\">AllowUnicastResponseToMulticast : NotConfigured<\/p>\n<p style=\"padding-left: 30px\">NotifyOnListen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : True<\/p>\n<p style=\"padding-left: 30px\">EnableStealthModeForIPsec&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotConfigured<\/p>\n<p style=\"padding-left: 30px\">LogFileName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : %systemroot%system32LogFilesFirewallpfirewall<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; log<\/p>\n<p style=\"padding-left: 30px\">LogMaxSizeKilobytes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 4096<\/p>\n<p style=\"padding-left: 30px\">LogAllowed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : False<\/p>\n<p style=\"padding-left: 30px\">LogBlocked&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : False<\/p>\n<p style=\"padding-left: 30px\">LogIgnored&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : NotConfigured<\/p>\n<p style=\"padding-left: 30px\">DisabledInterfaceAliases&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;: {NotConfigured}\n<br \/> The cool thing is that the <b>Get-NetFirewallProfile<\/b> cmdlet accepts an array for the profile name. Therefore, I can use a command something like the one appearing here.<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallProfile domain,private,public\nEven better, the <b>Get-NetFirewallProfile<\/b> cmdlet accepts wildcards. Therefore, I can use a command something like this one.<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallProfile d*,p*\nOne problem with the firewall cmdlets is that they are all part of the massive NetSecurity module&mdash;a module that supplies 84 cmdlets and functions. I found this out by using the command shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; (gcm -Module netsecurity).count<\/p>\n<p style=\"padding-left: 30px\">84\nFurther, there are no aliases for any of the commands in the NetSecurity module. This is revealed by the command shown here.<\/p>\n<p style=\"padding-left: 30px\">gcm -Module netsecurity | % {gal -Definition $_.name -ea 0}\nBesides no aliases for the firewall cmdlets, all of the names are pretty long, and due to the naming convention, tab expansion for the cmdlet names is not very efficient either. The 27 cmdlets appear here (sorted by name because the verb and noun are not exposed through <b>Get-Command<\/b>).<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; gcm -noun *fire* | sort name | select name<\/p>\n<p style=\"padding-left: 30px\">Name<\/p>\n<p style=\"padding-left: 30px\">&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Copy-NetFirewallRule<\/p>\n<p style=\"padding-left: 30px\">Disable-NetFirewallRule<\/p>\n<p style=\"padding-left: 30px\">Enable-NetFirewallRule<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallAddressFilter<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallApplicationFilter<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallInterfaceFilter<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallInterfaceTypeFilter<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallPortFilter<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallProfile<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallRule<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallSecurityFilter<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallServiceFilter<\/p>\n<p style=\"padding-left: 30px\">Get-NetFirewallSetting<\/p>\n<p style=\"padding-left: 30px\">New-NetFirewallRule<\/p>\n<p style=\"padding-left: 30px\">Remove-NetFirewallRule<\/p>\n<p style=\"padding-left: 30px\">Rename-NetFirewallRule<\/p>\n<p style=\"padding-left: 30px\">Set-NetFirewallAddressFilter<\/p>\n<p style=\"padding-left: 30px\">Set-NetFirewallApplicationFilter<\/p>\n<p style=\"padding-left: 30px\">Set-NetFirewallInterfaceFilter<\/p>\n<p style=\"padding-left: 30px\">Set-NetFirewallInterfaceTypeFilter<\/p>\n<p style=\"padding-left: 30px\">Set-NetFirewallPortFilter<\/p>\n<p style=\"padding-left: 30px\">Set-NetFirewallProfile<\/p>\n<p style=\"padding-left: 30px\">Set-NetFirewallRule<\/p>\n<p style=\"padding-left: 30px\">Set-NetFirewallSecurityFilter<\/p>\n<p style=\"padding-left: 30px\">Set-NetFirewallServiceFilter<\/p>\n<p style=\"padding-left: 30px\">Set-NetFirewallSetting<\/p>\n<p style=\"padding-left: 30px\">Show-NetFirewallRule\nBecause of the naming convention, when using tab expansion, I have to type <b>NetFirewall<\/b>, and then a letter, such as <b>P<\/b> or <b>R<\/b> or <b>S<\/b>, to get close to the actual function name. If I type <b>NetF<\/b> and press tab, I have to cycle through the commands to find the function name I&#8217;m looking for.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b> &nbsp;&nbsp;If your duties require you to spend much time working with the firewall cmdlets, I recommend that you create your own series of aliases for the functions with which you regularly work. Store these aliases in a module that you can load on demand or in your profile, if you wish to have them at hand.\nThat is a quick overview of the firewall functions. Hope all is well with you, and happy scripting.\nI invite you to follow me 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 email to me at <a href=\"http:\/\/blogs.technet.commailto: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, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to work with Windows Firewall on Windows 8&nbsp;and Windows Server&nbsp;2012. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife returned home from the Windows PowerShell Saturday #003 event in Atlanta, Georgia, last night, and&nbsp;she was&nbsp;exhausted&mdash;happy, but exhausted. First of all we had [&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":[161,362,3,63,61,367,45],"class_list":["post-4757","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-firewall","tag-powershell-3","tag-scripting-guy","tag-security","tag-weekend-scripter","tag-windows-8","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to work with Windows Firewall on Windows 8&nbsp;and Windows Server&nbsp;2012. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife returned home from the Windows PowerShell Saturday #003 event in Atlanta, Georgia, last night, and&nbsp;she was&nbsp;exhausted&mdash;happy, but exhausted. First of all we had [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4757","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=4757"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4757\/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=4757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}