Как перезагрузить или выключить компьютер с помощью Python

How to Restart or Shut Down a Computer Using Python?

My son is growing up, so I decided to launch a series of articles dedicated to small computer pranks using Python 🙂 I deliberately kept the writing style fun… so, dear readers, don’t judge me too harshly 🙂 I did my best! So, let’s go…


Hey, future keyboard masters! Today, I’ll show you how to make your computer (or someone else’s, but of course, we don’t recommend that 😉) suddenly shut down or restart using Python. It’s not exactly elite hacker magic, but it’s something! And it looks cool if you’re just starting out and want to show off a little.

Why would you need this?
Imagine setting a timer so your PC shuts down just as your mom comes in to check if you’re still gaming past midnight. Or maybe you want to “accidentally” reboot your friend’s PC right in the middle of their CS:GO match.

Jokes aside, these are basic commands that can actually be useful in serious projects too.

Code to shut down the PC
Let’s start simple. Open your Python editor, copy, and run this:

import os # Shut down the PC immediately
os.system("shutdown /s /t 0")

Once you run it—boom, your computer shuts down. Magic, right? 🙂

How to restart the PC?
Easy! Just do this:

import os # Restart the PC
os.system("shutdown /r /t 0")

Run it, and your computer will restart instantly—like it just decided it needed a little break. 🙂

How to cancel a scheduled shutdown?
Sometimes, you prank a friend, and they unexpectedly get mad. You need a way to fix the situation:

import os # Cancel a scheduled shutdown or restart
os.system("shutdown /a")

This command cancels any countdown timer set by the shutdown command. Handy, right?

But what if you want to do it “like a pro”?
No problem! You can use another Python module—subprocess. It does the same thing but looks a bit more professional:

import subprocess # Shut down the PC
subprocess.run(["shutdown", "/s", "/t", "0"]) # Restart the PC
subprocess.run(["shutdown", "/r", "/t", "0"])

Important Warning!
Please do not try running these commands on someone else’s computer without permission. That’s not a joke—it’s illegal mischief that can have serious consequences. But on your own PC? Go for it!

What’s next?
Now you know how to control your system with Python. Next time, I’ll show you how to “break” your dad’s keyboard or trigger a fake Blue Screen of Death. Just kidding (or am I?). 😏

Stay tuned, keep learning, and may your scripts always run smoothly. See you in the world of coding, future hacker-prankster! 👾

As always, feel free to send any questions via email or Telegram. 🙂

Support the Blog!

Running a blog takes a lot of effort, time, and passion. Your donations help improve the content, inspire new ideas, and keep the project going.
If you’ve enjoyed the blog’s materials, any support would mean the world to me. Thank you for being here! ❤️

PayPal Logo Donate via PayPal

Revolut Logo Donate via Revolut

Leave a Reply

Your email address will not be published. Required fields are marked *