<#
I had a need to be able to turn Windows Defender firewall on/off at will and it was getting to be a pain. This script will give you a tray icon you can use. Click on, click off. In case you forget, it includes a timer that will turn it back on 10 minutes after you disable it in case you forget.
SS - NovaWright Studios
#>
# =========================================================================
# CONFIGURATION SETTINGS
# =========================================================================
# Set to $true to show the system tray icon
# Set to $false to hide the icon completely (runs silently in background)
$showTrayIcon = $true
# =========================================================================
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Ensure script runs with Administrator privileges
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
# Create System Tray Icon
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
# Set Tray Icon Visibility based on configuration above
$notifyIcon.Visible = $showTrayIcon
# Initialize Background Timer (10 minutes = 600,000 milliseconds)
$autoEnableTimer = New-Object System.Windows.Forms.Timer
$autoEnableTimer.Interval = 600000
# Helper function to query live state and update Icon + Tooltip
function Update-FirewallState {
$isEnabled = (Get-NetFirewallProfile -Profile Domain).Enabled
if ($isEnabled) {
$notifyIcon.Icon = [System.Drawing.SystemIcons]::Shield
$notifyIcon.Text = "Firewall: ENABLED (Click to disable for 10m)"
} else {
$notifyIcon.Icon = [System.Drawing.SystemIcons]::Warning
$notifyIcon.Text = "Firewall: DISABLED (Auto-enables in 10m)"
}
}
# Function to turn firewall ON
function Enable-Firewall ([string]$source = "manual") {
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
$autoEnableTimer.Stop()
Update-FirewallState
if ($showTrayIcon) {
if ($source -eq "timer") {
$notifyIcon.ShowBalloonTip(4000, "Windows Firewall Restored", "10-minute timer expired. Firewall auto-enabled.", [System.Windows.Forms.ToolTipIcon]::Info)
} else {
$notifyIcon.ShowBalloonTip(3000, "Windows Firewall Enabled", "Firewall ENABLED. Protection active.", [System.Windows.Forms.ToolTipIcon]::Info)
}
}
}
# Function to turn firewall OFF and start timer
function Disable-Firewall {
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False
$autoEnableTimer.Start()
Update-FirewallState
if ($showTrayIcon) {
$notifyIcon.ShowBalloonTip(4000, "Windows Firewall Disabled", "Firewall DISABLED. Auto-enables in 10m.", [System.Windows.Forms.ToolTipIcon]::Warning)
}
}
# Timer Event
$autoEnableTimer.add_Tick({
Enable-Firewall -source "timer"
})
# Dedicated Click Event Handler
$notifyIcon.add_MouseClick({
param($sender, $e)
if ($e.Button -eq [System.Windows.Forms.MouseButtons]::Left) {
$currentState = (Get-NetFirewallProfile -Profile Domain).Enabled
if ($currentState) {
Disable-Firewall
} else {
Enable-Firewall -source "manual"
}
}
})
# Add Right-Click Context Menu to Exit
$contextMenu = New-Object System.Windows.Forms.ContextMenuStrip
$exitItem = $contextMenu.Items.Add("Exit")
$exitItem.add_Click({
$autoEnableTimer.Stop()
$notifyIcon.Visible = $false
[System.Windows.Forms.Application]::Exit()
})
$notifyIcon.ContextMenuStrip = $contextMenu
# Initial state load
Update-FirewallState
# Keep script running in background
[System.Windows.Forms.Application]::Run()