title: How can you check for existing firewall rules using Powershell
body: <p>So, I've got this script:</p>
  
  <pre><code>function Add-FirewallRule {
     param( 
        $name,
        $tcpPorts,
        $appName = $null,
        $serviceName = $null
     )
      $fw = New-Object -ComObject hnetcfg.fwpolicy2 
      $rule = New-Object -ComObject HNetCfg.FWRule
  
      $rule.Name = $name
      if ($appName -ne $null) { $rule.ApplicationName = $appName }
      if ($serviceName -ne $null) { $rule.serviceName = $serviceName }
      $rule.Protocol = 6 #NET_FW_IP_PROTOCOL_TCP
      $rule.LocalPorts = $tcpPorts
      $rule.Enabled = $true
      $rule.Grouping = "@firewallapi.dll,-23255"
      $rule.Profiles = 7 # all
      $rule.Action = 1 # NET_FW_ACTION_ALLOW
      $rule.EdgeTraversal = $false
      if(*here*)
      {
      $fw.Rules.Add($rule)
      }
  
  }
  </code></pre>
  
  <p>and I want to be able to put something in the if() that will check and see if the rule already exists before it adds it.  I'm not terribly familiar with powershell, so go easy on me :P</p>