Skip to content

Instantly share code, notes, and snippets.

@rsmnarts
Last active September 3, 2025 12:54
Show Gist options
  • Select an option

  • Save rsmnarts/cdc2c2c0b35dd0988d255364891e099e to your computer and use it in GitHub Desktop.

Select an option

Save rsmnarts/cdc2c2c0b35dd0988d255364891e099e to your computer and use it in GitHub Desktop.
Network Monitor Script for Auto-Sthudown (Windows)
# =============================================================================
# Network Monitor Script for Auto-Action
#
# Description: This script monitors network activity and performs a configured
# action (Hibernate, Shutdown, Sleep, etc.) when the download
# is complete.
#
# IMPORTANT: Run this script in PowerShell as an ADMINISTRATOR for actions
# like Shutdown, Hibernate, or Sleep to work correctly.
# =============================================================================
# -----------------------------------------------------------------------------
# SETTINGS - PLEASE CHANGE THE VALUES BELOW
# -----------------------------------------------------------------------------
# 1. Your network adapter name (find with `Get-NetAdapter`). Examples: "Wi-Fi", "Ethernet"
$networkAdapterName = "Wi-Fi"
# 2. The final action to perform when the download is complete.
# Valid options are: "Hibernate", "Shutdown", "Sleep", "Restart", "Lock", "Nothing"
$finalAction = "Shutdown"
# 3. Speed threshold (in KiloBytes/sec) to be considered "downloading".
$downloadThresholdKBps = 300
# 4. Speed threshold (in KiloBytes/sec) to be considered "idle".
$idleThresholdKBps = 50
# 5. How long (in seconds) the network must be idle before triggering the action.
# 300 seconds = 5 minutes
$idleDurationSeconds = 300
# 6. How often (in seconds) the script checks the network speed.
$checkIntervalSeconds = 2
# -----------------------------------------------------------------------------
# END OF SETTINGS - DO NOT CHANGE THE CODE BELOW
# -----------------------------------------------------------------------------
# Validate adapter
$adapter = Get-NetAdapter -Name $networkAdapterName -ErrorAction SilentlyContinue
if (-not $adapter) {
Write-Host "ERROR: Adapter '$networkAdapterName' not found." -ForegroundColor Red
return
}
Write-Host "Monitoring adapter: '$networkAdapterName'. Press Ctrl+C to stop." -ForegroundColor Cyan
Write-Host "Action on completion: '$finalAction'." -ForegroundColor Cyan
Write-Host "Waiting for download activity to exceed $downloadThresholdKBps KB/s..." -ForegroundColor Green
$downloadDetected = $false
$idleTimeCounter = 0
# Main monitoring loop
while ($true) {
$initialBytes = (Get-NetAdapterStatistics -Name $networkAdapterName).ReceivedBytes
Start-Sleep -Seconds $checkIntervalSeconds
$finalBytes = (Get-NetAdapterStatistics -Name $networkAdapterName).ReceivedBytes
$bytesPerSecond = ($finalBytes - $initialBytes) / $checkIntervalSeconds
$speedKBps = [math]::Round($bytesPerSecond / 1024, 2)
# Detection logic
if ($downloadDetected) {
if ($speedKBps -lt $idleThresholdKBps) {
$idleTimeCounter += $checkIntervalSeconds
} else {
$idleTimeCounter = 0
}
if ($idleTimeCounter -ge $idleDurationSeconds) {
# Clear the status line before taking the final action
Write-Host "`rDownload complete. Preparing to execute action: '$finalAction'... " -ForegroundColor Magenta
# --- EXECUTE FINAL ACTION ---
switch ($finalAction) {
"Hibernate" {
Write-Host "Hibernating computer now..." -ForegroundColor Red
shutdown /h
}
"Shutdown" {
Write-Host "Shutting down computer now..." -ForegroundColor Red
shutdown /s /t 0
}
"Sleep" {
Write-Host "Putting computer to sleep now..." -ForegroundColor Red
rundll32.exe powrprof.dll,SetSuspendState 0,1,0
}
"Restart" {
Write-Host "Restarting computer now..." -ForegroundColor Red
shutdown /r /t 0
}
"Lock" {
Write-Host "Locking workstation now..." -ForegroundColor Red
rundll32.exe user32.dll,LockWorkStation
}
"Nothing" {
Write-Host "Action is 'Nothing'. Script will now exit." -ForegroundColor Green
}
Default {
Write-Host "WARNING: Invalid action '$finalAction' configured. Doing nothing." -ForegroundColor Yellow
}
}
break # Exit the loop
}
} else {
if ($speedKBps -gt $downloadThresholdKBps) {
$downloadDetected = $true
Write-Host "`rDownload activity detected! Monitoring for completion... " -ForegroundColor Green
Start-Sleep -Seconds 2
}
}
# --- SINGLE-LINE OUTPUT SECTION ---
$statusText = ""
if (-not $downloadDetected) {
$statusText = "Status: Waiting for download..."
} elseif ($idleTimeCounter -gt 0) {
$statusText = "Status: Network Idle (Timer: $($idleTimeCounter)/$($idleDurationSeconds)s)..."
} else {
$statusText = "Status: Downloading..."
}
$outputLine = "`rSpeed: $($speedKBps) KB/s | $statusText" + (" " * 20)
Write-Host $outputLine -NoNewline
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment