Taken from Set On Lid Close Power Option. There is also a script or two on this page, but the method reproduced below is the best, IMHO.
You can set it through powercfg
commands.
The pre-configured schemes have the following GUIDs:
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver)
I'll use the Balanced scheme for my examples, but you would use the GUID provided by:
powercfg -GETACTIVESCHEME
You can find the GUIDs for subgroups and power settings as well as the index values for each power setting by running a query command with your scheme GUID:
powercfg -Q 381b4222-f694-41f0-9685-ff5bb260df2e
Looking through the output, you will discover that the subgroup GUID you want is:
Subgroup GUID: 4f971e89-eebd-4455-a8de-9e59040e7347 (Power buttons and lid)
and the power setting:
Power Setting GUID: 5ca83367-6e45-459f-a27b-476b1d01c936 (Lid close action)
with index options:
Possible Setting Index: 000
Possible Setting Friendly Name: Do nothing
Possible Setting Index: 001
Possible Setting Friendly Name: Sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 003
Possible Setting Friendly Name: Shut down
So in order to configure your system to Shut down when the lid is closed, you would run:
powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3
powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3
AC for the "Plugged In" action and DC for the "On Battery" action.
Hope this helps.
Please note that I have not block quoted, as it messed up the rather long code lines. So for the sake of formatting, I have left it unquoted.