Skip to content

Commit a5bb506

Browse files
authored
Update Harden-Windows.ps1
Improved handle port blocks by Firewall Added error handling for services Added warning of port already disabled by Firewall And added Minor Improvements for the code
1 parent 58619fb commit a5bb506

File tree

1 file changed

+49
-23
lines changed

1 file changed

+49
-23
lines changed

Harden-Windows.ps1

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,59 @@
1-
# === Run as Administrator ===
1+
# === Run this script as Administrator ===
2+
3+
# ----------------------------------------
4+
# 🚫 Block inbound dangerous or unneeded ports
5+
# ----------------------------------------
6+
$portsToBlock = @(
7+
"135", "137", "139", "445", "5040", "1462", "7680",
8+
"26822", "32683", "33683", "49664", "49665", "49666", "49667", "49670"
9+
)
210

3-
# Block dangerous inbound ports
4-
$portsToBlock = "135", "139", "445", "5040", "1462", "49664-49669"
511
foreach ($port in $portsToBlock) {
6-
New-NetFirewallRule -DisplayName "Block Port $port" `
7-
-Direction Inbound `
8-
-LocalPort $port `
9-
-Protocol TCP `
10-
-Action Block `
11-
-Profile Any
12+
$ruleName = "Block TCP Port $port"
13+
Write-Host "🔒 Blocking port: $port"
14+
15+
if (-not (Get-NetFirewallRule -DisplayName $ruleName -ErrorAction SilentlyContinue)) {
16+
New-NetFirewallRule -DisplayName $ruleName `
17+
-Direction Inbound `
18+
-LocalPort $port `
19+
-Protocol TCP `
20+
-Action Block `
21+
-Profile Any
22+
Write-Host "✅ Port $port blocked"
23+
} else {
24+
Write-Host "⚠️ Rule already exists for port $port (skipping)"
25+
}
1226
}
1327

14-
# Disable dangerous or unneeded services
28+
# ----------------------------------------
29+
# ❌ Disable dangerous or unneeded services
30+
# ----------------------------------------
1531
$servicesToDisable = @(
1632
"RemoteRegistry",
17-
"TermService", # Remote Desktop
18-
"LanmanServer", # File Sharing (SMB)
19-
"LanmanWorkstation", # SMB Client
20-
"SSDPDiscovery",
21-
"upnphost", # UPnP
22-
"FDResPub", # Function Discovery
23-
"Spooler" # Printer (optional, comment out if needed)
33+
"TermService", # Remote Desktop
34+
"LanmanServer", # SMB File Sharing
35+
"LanmanWorkstation", # SMB Client
36+
"SSDPDiscovery", # Might not exist
37+
"upnphost", # UPnP
38+
"FDResPub", # Function Discovery
39+
"Spooler" # Printer service (optional)
2440
)
2541

2642
foreach ($svc in $servicesToDisable) {
27-
Write-Host "Disabling service: $svc"
28-
Stop-Service -Name $svc -ErrorAction SilentlyContinue
29-
Set-Service -Name $svc -StartupType Disabled
43+
$service = Get-Service -Name $svc -ErrorAction SilentlyContinue
44+
if ($service) {
45+
Write-Host "🛑 Disabling service: $svc"
46+
Stop-Service -Name $svc -ErrorAction SilentlyContinue
47+
Set-Service -Name $svc -StartupType Disabled
48+
} else {
49+
Write-Host "⚠️ Service not found: $svc (skipping)"
50+
}
3051
}
3152

32-
# OPTIONAL: Disable IPv6 (uncomment if you want)
33-
<#
53+
# ----------------------------------------
54+
# 🧪 OPTIONAL: Disable IPv6 (Uncomment if needed)
55+
# ----------------------------------------
56+
<#
3457
Write-Host "Disabling IPv6..."
3558
New-ItemProperty `
3659
-Path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters" `
@@ -40,4 +63,7 @@ New-ItemProperty `
4063
-Force
4164
#>
4265

43-
Write-Host "`n✅ System hardening complete. Please restart your computer to apply all changes."
66+
# ----------------------------------------
67+
# ✅ Done
68+
# ----------------------------------------
69+
Write-Host "`n🎉 System hardening complete. Please restart your PC to apply all changes." -ForegroundColor Green

0 commit comments

Comments
 (0)