What happens when a PC is turning off?

There are a lot of things happening during a shutdown. Here are just a few examples:

  • Check to see if any user applications havent closed (like an unsaved document) and prompt the user if necessary
  • Stop background services
  • Wait for the termination signal from open running services and applications
  • Flushes cache to disk
  • Write log files
  • All users are logged out
  • End the shell
  • Start installing Windows updates if necessary and tell the OS to finish updates on boot if necessary
  • Send the ACPI shutdown signal (this is what turns the machine off)

Edit: Feel free to add more in the comments if you can think of other items.

The system registry is (maybe?) written to the disk. Back on XP I noticed that if you made any changes to the registry then pulled the power, the changes would not be saved. I'm not quite sure about this one, just pointing it out.

Here's an excerpt from a document that Microsoft released.

• System session shutdown. This phase includes the pre-shutdown notification and shutdown notification subphases:

• Pre-shutdown notification. Windows serially shuts down all services that registered to receive pre-shutdown notifications. Ordered services—services that have set up the shutdown order of dependent services—are shut down before non-ordered services.

• Shutdown notification. All services that registered to receive shutdown notifications are shut down in parallel. If all services have not exited after 20 seconds (in Windows Vista) or 12 seconds (in Windows 7 client operating systems), the system continues the shutdown. Processes and services that do not shut down in a timely manner are left running as the system shuts down.

• Kernel shutdown. The remainder of the system, such as all devices and drivers, are shut down during the kernel shutdown phase.

Basically what you're waiting on is each individual service to clean up and exit. Each service is given 12 seconds to exit before it's killed.

Half of the shutdown time is dedicated to shutting down system services. If you're really interesting in seeing what time is dedicated to what during your shutdown, Windows includes a tool for tracing shutdown time.

xbootmgr -trace shutdown -numRuns 3 -resultPath %systemdrive%\traces -postBootDelay 180 -traceFlags base

And to make sense of the generated file (be sure to run in %systemdrive%\traces)

xperf -i trace.etl -o summary.xml -a shutdown

Sources: Update: Links from Microsoft Servers no longer work