{"id":69423,"date":"2005-07-11T22:41:00","date_gmt":"2005-07-11T22:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/07\/11\/how-can-i-monitor-changes-to-a-registry-key\/"},"modified":"2005-07-11T22:41:00","modified_gmt":"2005-07-11T22:41:00","slug":"how-can-i-monitor-changes-to-a-registry-key","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-monitor-changes-to-a-registry-key\/","title":{"rendered":"How Can I Monitor Changes to a Registry Key?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! Is it possible to use a script to monitor changes to a registry key? I\u2019d like to be notified any time someone makes changes to the HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run key.<BR><BR>&#8212; SB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, SB. And by the way, thanks for wording the question the way you did: it\u2019s actually pretty easy to receive notification any time someone makes a change to a specific registry key. Had you gone on to ask, \u201cAnd then can you tell me who made the change and what change they made?\u201d well, in that case we\u2019d have a problem on our hands (and we\u2019ll explain why in a moment). But you didn\u2019t ask that, so for now we\u2019ll just forget we even mentioned it.<\/P>\n<P>Let\u2019s take a look at a script that monitors the HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run registry key and then echoes back a message any time that key is changed:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\default&#8221;)<\/p>\n<p>Set colEvents = objWMIService.ExecNotificationQuery _\n    (&#8220;SELECT * FROM RegistryKeyChangeEvent WHERE Hive=&#8217;HKEY_LOCAL_MACHINE&#8217; AND &#8221; &amp; _\n        &#8220;KeyPath=&#8217;SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run'&#8221;) <\/p>\n<p>Do\n    Set objLatestEvent = colEvents.NextEvent\n    Wscript.Echo Now &amp; &#8220;: The registry has been modified.&#8221;\nLoop\n<\/PRE>\n<P>We begin by connecting to the WMI service; more specifically, we connect to the <B>root\\default<\/B> namespace, which is where the registry event classes live. We then use the <B>ExecNotificationQuery<\/B> method to issue the following query:<\/P><PRE class=\"codeSample\">(&#8220;SELECT * FROM RegistryKeyChangeEvent WHERE Hive=&#8217;HKEY_LOCAL_MACHINE&#8217; AND &#8221; &amp; _\n        &#8220;KeyPath=&#8217;SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run'&#8221;)\n<\/PRE>\n<P>All we\u2019re doing here is requesting notification any time an instance of the <B>RegistryKeyChangeEvent<\/B> class is created; as you might have guessed, an instance of this class is created any time a specified registry key is changed (for example a new value is added or an existing value is modified or deleted). <\/P>\n<P>Sounds good, but how do we specify the registry key? That\u2019s easy; we just need to configure the following two properties of the RegistryKeyChangeEvent class:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Hive<\/B>. This is the registry hive (location) where the key lives. We want to monitor a key that lives in HKEY_LOCAL_MACHINE, so we set the value of the Hive property to HKEY_LOCAL_MACHINE. If we wanted to monitor a registry key that lives in HKEY_CURRENT_USER, we\u2019d set the value accordingly. Note that we do not have to define and use constants when configuring the Hive property; just type in the actual name of the registry hive.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>KeyPath<\/B>. This is the path within the hive that leads to our registry key. Because the \\ is a reserved character in WMI, notice that we need to escape each \\ with a second \\. Thus, a key path like Software\\Microsoft\\Windows\\CurrentVersion\\Run must be written out as Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>After issuing the query we set up a Do Loop that will simply run forever, dutifully waiting for the next instance of the RegistryKeyChangeEvent class to appear. To make the script wait for such an appearance we use this line of code:<\/P><PRE class=\"codeSample\">Set objLatestEvent = colEvents.NextEvent\n<\/PRE>\n<P>This line of code causes the script to \u201cblock,\u201d which means the script will just sit there and wait for the next event to occur (as you well know, the event we are monitoring for is new instances of the RegistryKeyChangeEvent class). When such an event <I>does<\/I> occur, we simply echo the current date and time and the fact that the registry has been modified in some way. We then loop around and wait for the <I>next<\/I> such occurrence. (Press Ctrl+C to get out of the loop and end the script.)<\/P>\n<P>Why don\u2019t we do something a bit more specific than merely note that the registry has been modified in some way? Well, mainly because we <I>can\u2019t<\/I> do anything more specific: details such as what was changed and who changed it are not captured by the registry event provider. We could determine some of this information by grabbing the initial state of the registry key and then, when an event occurs, comparing the new state to the initial state. We\u2019d then have to set up a procedure that allows us to continue comparing the latest state of the registry key to the most recent previous state. But that\u2019s something we\u2019ll have to let you work out for yourself. (Unless, of course, we hear otherwise. After all, we live to serve!)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is it possible to use a script to monitor changes to a registry key? I\u2019d like to be notified any time someone makes changes to the HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run key.&#8212; SB Hey, SB. And by the way, thanks for wording the question the way you did: it\u2019s actually pretty easy to receive notification any [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[42,26,3,4,5],"class_list":["post-69423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-events-and-monitoring","tag-registry","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is it possible to use a script to monitor changes to a registry key? I\u2019d like to be notified any time someone makes changes to the HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run key.&#8212; SB Hey, SB. And by the way, thanks for wording the question the way you did: it\u2019s actually pretty easy to receive notification any [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=69423"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69423\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=69423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}