This PowerShell script searches for all `.exe` (executable) files in...
August 31, 2025 at 04:25 PM
This PowerShell script searches for all .exe
(executable) files in a specified drive and creates Windows Firewall rules to block their outbound network connections. Here’s a step-by-step explanation of what the code does:
-
Define the drive path:
$drivePath = "E:\"
- The variable
$drivePath
is set to"E:\"
, which means the script will operate on theE:
drive. You can adjust this to the specific drive or folder you want to target.
- The variable
-
Retrieve all
.exe
files:$exeFiles = Get-ChildItem -Path $drivePath -Recurse -Include *.exe
- The
Get-ChildItem
command (similar tols
ordir
in other systems) recursively (-Recurse
) searches for all files ending with.exe
(-Include *.exe
) starting from the root of theE:
drive and stores the list of files in the$exeFiles
variable.
- The
-
Iterate over each
.exe
file:foreach ($exe in $exeFiles) {
- The
foreach
loop iterates over each.exe
file found in the previous step.
- The
-
Create a firewall rule for each
.exe
:New-NetFirewallRule -DisplayName "Block_$($exe.Name)" -Direction Outbound -Program $exe.FullName -Action Block
- Inside the loop:
- The
New-NetFirewallRule
command is used to create a new Windows Firewall rule. - Each rule is named
Block_<exe_name>
(e.g.,Block_example.exe
) using the-DisplayName
parameter. - The
-Direction Outbound
parameter specifies that the rule applies to outbound connections initiated by the.exe
file. - The
-Program $exe.FullName
parameter specifies the full path of the.exe
file for which the rule applies. - The
-Action Block
parameter sets the rule to block traffic.
- The
- Inside the loop:
Summary:
This script creates firewall rules to block outbound network connections for every .exe
file found on the E:
drive. It’s commonly used for security purposes to prevent certain executables from accessing the network. Be cautious when running such scripts to ensure you're not unintentionally blocking critical programs.
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node