Export/Import specific Firewall rules through Powershell
Source: https://gallery.technet.microsoft.com/scriptcenter/Powershell-to-export-and-23287694/view/Reviews
Find the ExportImportFirewallRules.zip over there (including script to remove Firewall rules).
As with registry actions, first take a backup (on both source and target machine!). You can backup/export the complete Firewall set as follows:
right-click the top most item in the TOC of the Firewall (= Windows Defender Firewall with Advanced Security on Local Computer)
choose Export Policy…
save the .wfw file (e.g. AllFirewallRulesMachineName.wfw)
If necessary, activate the execution policy in Powershell (run as Administrator):
set-executionpolicy unrestricted
Export the desired rule(s). The export script can take three parameters:
- -Name: display name of the rule(s) to be processed - wildcard * can be used - default is *
- -CSVFile: output file - default is FirewallRules.csv
- -JSON: use JSON instead of CSV (default is $FALSE)
Example:
.\Export-FirewallRules.ps1 '*Remote*' RemoteRules.json -json
Be aware that the same name can occur multiple times! E.g.: outbound 'Spotify Music' rule for the UDP protocol, and outbound 'Spotify Music' rule for the TCP protocol. You can of course rename the rules beforehand to make them unique, if necessary.
The import script takes two parameters:
- -CSVFile: input file - default is FirewallRules.csv
- -JSON: use JSON instead of CSV (default is $FALSE)
Import the rule(s) as follows:
Example
.\Import-FirewallRules.ps1 RemoteRules.json -json