#Python #Snippets February 03, 2024

Python: Auto Shutdown Windows PC

This is set to auto shutdown your Windows PC in 2 hours import pygetwindow as gw
import os
from datetime import datetime, timedelta
import time

# Get all open windows
windows = gw.getAllWindows()

# Close each window
for window in windows:
    window.close()

# Calculate the target time 2 hours from now
target_time = datetime.now() + timedelta(hours=2)

# Calculate the time difference in seconds
time_difference = (target_time - datetime.now()).total_seconds()

# Delay until the target time
time.sleep(time_difference)

# Shutdown the PC
os.system("shutdown /s /t 0")

SEARCH