{"id":5264,"date":"2018-03-07T23:56:42","date_gmt":"2018-03-07T22:56:42","guid":{"rendered":"https:\/\/michlstechblog.info\/blog\/?p=5264"},"modified":"2018-03-08T10:13:59","modified_gmt":"2018-03-08T09:13:59","slug":"windows-execute-a-script-at-shutdown","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/windows-execute-a-script-at-shutdown\/","title":{"rendered":"Windows: Execute a Script at shutdown"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_5264 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_5264')){$('.twoclick_social_bookmarks_post_5264').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Windows%3A%20Execute%20a%20Script%20at%20shutdown\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Alike%20system%20startup%20it%20is%20not%20possible%20to%20execute%20a%20script%20at%20system%20shutdown%20with%20the%20task%20scheduler.%20It%20must%20be%20configured%20with%20a%20group%20policy.%20You%20can%20choose%20the%20GUI%20way%20are%20set%20it%20by%20script%3A%0D%0A%0D%0ALet%20us%20assume%20the%20Script%20is%20C%3A%5Cscripts%5CshutdownEv.cmd.%0D%0A%20%28more%26hellip%3B%29\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/windows-execute-a-script-at-shutdown\\\/\",\"post_id\":5264,\"post_title_referrer_track\":\"Windows%3A+Execute+a+Script+at+shutdown\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>like system startup it is not possible to execute a script at system shutdown with the task scheduler. It must be configured with a group policy. You can choose the GUI way are set it by script:<\/p>\n<p>Let us assume the Script is C:\\scripts\\shutdownEv.cmd.<br \/>\n<!--more--><br \/>\nAdjust the following template, replace Scriptname 2 times, and add the keys to the registry by saving it as file shutdown.reg&#8230;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nWindows Registry Editor Version 5.00\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\Scripts\\Shutdown]\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\Scripts\\Shutdown&#92;&#48;]\r\n&quot;GPO-ID&quot;=&quot;LocalGPO&quot;\r\n&quot;SOM-ID&quot;=&quot;Local&quot;\r\n&quot;FileSysPath&quot;=&quot;C:\\\\Windows\\\\System32\\\\GroupPolicy\\\\Machine&quot;\r\n&quot;DisplayName&quot;=&quot;Local Group Policy&quot;\r\n&quot;GPOName&quot;=&quot;Local Group Policy&quot;\r\n&quot;PSScriptOrder&quot;=dword:00000001\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\Scripts\\Shutdown&#92;&#48;&#92;&#48;]\r\n&quot;Script&quot;=&quot;c:\\\\Scripts\\\\shutdownEv.cmd&quot;\r\n&quot;Parameters&quot;=&quot;&quot;\r\n&quot;IsPowershell&quot;=dword:00000000\r\n&quot;ExecTime&quot;=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\State\\Machine\\Scripts\\Shutdown]\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\State\\Machine\\Scripts\\Shutdown&#92;&#48;]\r\n&quot;GPO-ID&quot;=&quot;LocalGPO&quot;\r\n&quot;SOM-ID&quot;=&quot;Local&quot;\r\n&quot;FileSysPath&quot;=&quot;C:\\\\Windows\\\\System32\\\\GroupPolicy\\\\Machine&quot;\r\n&quot;DisplayName&quot;=&quot;Local Group Policy&quot;\r\n&quot;GPOName&quot;=&quot;Local Group Policy&quot;\r\n&quot;PSScriptOrder&quot;=dword:00000001\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\State\\Machine\\Scripts\\Shutdown&#92;&#48;&#92;&#48;]\r\n&quot;Script&quot;=&quot;c:\\\\Scripts\\\\shutdownEv.cmd&quot;\r\n&quot;Parameters&quot;=&quot;&quot;\r\n&quot;ExecTime&quot;=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00\r\n<\/pre>\n<p>&#8230;. Ensure that there is not another GPO at HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\Scripts\\Shutdown\\<strong>0<\/strong>: configured. Then you have to use 1 or 2&#8230;. The shutdown key only exists if scripts are already configured.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nreg query &quot;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\Scripts\\Shutdown&quot;\r\nERROR: The system was unable to find the specified registry key or value.\r\n<\/pre>\n<p>Ok, no scripts are in place. Add the registry keys:<\/p>\n<p>regedit \/u shutdown.reg<\/p>\n<p>Also a ini File &#8220;<strong>C:\\Windows\\System32\\GroupPolicy\\Machine\\Scripts\\scripts.ini<\/strong>&#8221; is requiered. And like the registry keys. Check the connect of the file if there are already scripts defined. <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;Shutdown]\r\n0CmdLine=c:\\Scripts\\shutdownEv.cmd\r\n0Parameters=\r\n<\/pre>\n<p>Same procedure for a powershell script. The Registry File shutdownps.reg<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nWindows Registry Editor Version 5.00\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\Scripts\\Shutdown]\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\Scripts\\Shutdown&#92;&#48;]\r\n&quot;GPO-ID&quot;=&quot;LocalGPO&quot;\r\n&quot;SOM-ID&quot;=&quot;Local&quot;\r\n&quot;FileSysPath&quot;=&quot;C:\\\\Windows\\\\System32\\\\GroupPolicy\\\\Machine&quot;\r\n&quot;DisplayName&quot;=&quot;Local Group Policy&quot;\r\n&quot;GPOName&quot;=&quot;Local Group Policy&quot;\r\n&quot;PSScriptOrder&quot;=dword:00000001\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\Scripts\\Shutdown&#92;&#48;&#92;&#48;]\r\n&quot;Script&quot;=&quot;c:\\\\Scripts\\\\shutdownEV.ps1&quot;\r\n&quot;Parameters&quot;=&quot;&quot;\r\n&quot;IsPowershell&quot;=dword:00000001\r\n&quot;ExecTime&quot;=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\State\\Machine\\Scripts\\Shutdown]\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\State\\Machine\\Scripts\\Shutdown&#92;&#48;]\r\n&quot;GPO-ID&quot;=&quot;LocalGPO&quot;\r\n&quot;SOM-ID&quot;=&quot;Local&quot;\r\n&quot;FileSysPath&quot;=&quot;C:\\\\Windows\\\\System32\\\\GroupPolicy\\\\Machine&quot;\r\n&quot;DisplayName&quot;=&quot;Local Group Policy&quot;\r\n&quot;GPOName&quot;=&quot;Local Group Policy&quot;\r\n&quot;PSScriptOrder&quot;=dword:00000001\r\n\r\n&#x5B;HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\State\\Machine\\Scripts\\Shutdown&#92;&#48;&#92;&#48;]\r\n&quot;Script&quot;=&quot;c:\\\\Scripts\\\\shutdownEV.ps1&quot;\r\n&quot;Parameters&quot;=&quot;&quot;\r\n&quot;ExecTime&quot;=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00\r\n<\/pre>\n<p>and the corresponding ini file &#8220;<strong>C:\\Windows\\System32\\GroupPolicy\\Machine\\Scripts\\psscripts.ini<\/strong>&#8221;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;Shutdown]\r\n0CmdLine=c:\\Scripts\\shutdownEV.ps1\r\n0Parameters=\r\n<\/pre>\n<p>Note: The ini file is not necessary for executing the script at shutdown. It is required that the policy is also shown by the Group Policy Object Editor.<\/p>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, like system startup it is not possible to execute a script at system shutdown with the task scheduler. It must be configured with a group policy. You can choose the GUI way are set it by script: Let us assume the Script is C:\\scripts\\shutdownEv.cmd.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1004,1150,1149,133,72,20],"class_list":["post-5264","post","type-post","status-publish","format-standard","hentry","category-windowsknowhow","tag-at-shutdown","tag-batch","tag-execute","tag-powershell","tag-script","tag-windows-2"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/5264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=5264"}],"version-history":[{"count":11,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/5264\/revisions"}],"predecessor-version":[{"id":5273,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/5264\/revisions\/5273"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=5264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=5264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=5264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}