Export/Import separate Firewall rules through Powershell
Source:
https://gallery.technet.microsoft.com/scriptcenter/Powershell-to-export-and-23287694/view/Reviews
See attached scripts:
Export-FirewallRules.ps1 &
Import-FirewallRules.ps1.
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
If you get "Export-FirewallRules.ps1 cannot be loaded. The file Export-FirewallRules.ps1 is not digitally signed.", goto
SelfsignPowershellScripts
If you want to limit the rules to be imported, simply edit the .csv or .json file.
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
If you get "Import-FirewallRules.ps1 cannot be loaded. The file Import-FirewallRules.ps1 is not digitally signed.", goto
SelfsignPowershellScripts.