Script: *add-firewall-rules.ps1* ======================== This PowerShell script adds firewall rules for the given executable. Administrator rights are required. Parameters ---------- ```powershell PS> ./add-firewall-rules.ps1 [[-PathToExecutables] ] [[-Direction] ] [[-FirewallProfile] ] [] -PathToExecutables Specifies the path to the executables. Required? false Position? 1 Default value Accept pipeline input? false Accept wildcard characters? false -Direction Specifies the direction for the firewall rule. Can be 'Inbound' or 'Outbound'. Default is 'Inbound'. Required? false Position? 2 Default value Inbound Accept pipeline input? false Accept wildcard characters? false -FirewallProfile Required? false Position? 3 Default value @("Domain", "Private") Accept pipeline input? false Accept wildcard characters? false [] This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. ``` Example ------- ```powershell PS> ./add-firewall-rules.ps1 -PathToExecutables C:\MyApp\bin -Direction Outbound -Profile Private ``` Notes ----- Author: Markus Fleschutz | License: CC0 Related Links ------------- https://github.com/fleschutz/PowerShell Script Content -------------- ```powershell <# .SYNOPSIS Adds firewall rules for executables (needs admin rights). .DESCRIPTION This PowerShell script adds firewall rules for the given executable. Administrator rights are required. .PARAMETER PathToExecutables Specifies the path to the executables. .PARAMETER Direction Specifies the direction for the firewall rule. Can be 'Inbound' or 'Outbound'. Default is 'Inbound'. .PARAMETER Profile Specifies the firewall profile. Can be 'Domain', 'Private', or 'Public'. Multiple values can be specified as an array. .EXAMPLE PS> ./add-firewall-rules.ps1 -PathToExecutables C:\MyApp\bin -Direction Outbound -Profile Private .LINK https://github.com/fleschutz/PowerShell .NOTES Author: Markus Fleschutz | License: CC0 #> #Requires -RunAsAdministrator param( [string]$PathToExecutables = "", [string]$Direction = "Inbound", [array]$FirewallProfile = @("Domain", "Private") ) try { if (-not $PathToExecutables) { $PathToExecutables = Read-Host "Enter path to executables" } $AbsPath = Convert-Path -Path $PathToExecutables $Executables = Get-ChildItem -Path $AbsPath -Filter "*.exe" if (-not $Executables) { Write-Warning "No executables found. No Firewall rules have been created." Read-Host "Press Enter to continue..." return } foreach ($exe in $Executables) { $exeName = $exe.Name $exeFullPath = $exe.FullName Write-Output "Adding firewall rule for $exeName" New-NetFirewallRule -DisplayName $exeName -Direction $Direction -Program $exeFullPath -Profile $FirewallProfile -Action Allow } Write-Host -ForegroundColor Green "Done" } catch { Write-Error "Error in line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)" } ``` *(generated by convert-ps2md.ps1 using the comment-based help of add-firewall-rules.ps1 as of 08/15/2024 09:50:44)*