VS Add inbound firewall rule programatically

Claire DOVE 1 Reputation point

I copied the code provided for the question 'Add firewall rule programatically' here: add-firewall-rule-programaticallyand have got it to function without errors. Unfortunately, being rather dense, I don't understand what it does. Where is the executable that needs to be authorized referenced (if it is referenced)?

What I want to do is create a new rule to allow my program to write and read files on another computer on a LAN. At the moment the user has to go all through the complicated process of creating a new rule via Windows Defender.

Writing the rule programatically would be part of the existing installation program which already requires administrator rights. Is this possible?

Can anyone help please?

Windows 10 Security
Windows 10 Security
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Security: The precautions taken to guard against crime, attack, sabotage, espionage, or another threat.
2,748 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Claire DOVE 1 Reputation point

    I have found another way to do this. I create a bat file that uses netsh:

    netsh advfirewall firewall add rule name="My New Rule" dir=in action=allow program="C:\Program Files (x86)\My Program\My Program.exe" profile=private enable=yes  

    and I run it from my VS app which requires administrator rights.

    0 comments No comments

  2. Limitless Technology 39,341 Reputation points


    Initially, those scripts where no app exe is specified corresponds to rules that affect only the port. For example, do not allow communication on port, or allow communication.

    For more complex rules, such as specific Exe files you can use the New-NetFirewallRule cmdlet, like explained in the next article, in specific the Example 7, to associate rule with specific app.



    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments