{"id":50663,"date":"2010-04-13T00:01:00","date_gmt":"2010-04-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/04\/13\/hey-scripting-guy-how-can-i-be-notified-when-a-usb-drive-is-plugged-into-my-computer\/"},"modified":"2010-04-13T00:01:00","modified_gmt":"2010-04-13T00:01:00","slug":"hey-scripting-guy-how-can-i-be-notified-when-a-usb-drive-is-plugged-into-my-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-be-notified-when-a-usb-drive-is-plugged-into-my-computer\/","title":{"rendered":"Hey, Scripting Guy! How Can I Be Notified When a USB Drive Is Plugged into My Computer?"},"content":{"rendered":"<p><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><\/p>\n<p>&nbsp;\n<img decoding=\"async\" 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\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I would like to be notified when I plug in a USB drive into my computer. I have some cool ideas for using that, but I do not know how to detect when a USB drive is plugged in. I know I can run a script that loops through looking for all drives, and compares them with a previous collection of drives and then looks for differences. I also know I can use the <strong>Compare-Object<\/strong> cmdlet to make the comparison, so it would not be too terribly hard, but it seems inefficient. Is there a better way to do this?<\/p>\n<p class=\"MsoNormal\">&#8212; SP<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" 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\">Hello SP, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. We are getting closer and closer to the <a href=\"http:\/\/bit.ly\/2010sgall\">2010 Scripting Games<\/a>. They begin on April 26 and will run for two weeks. <a href=\"http:\/\/bit.ly\/2010sgregister\">Registration<\/a> has been open for a week or so, and we encourage you to sign up. It takes less than a minute because you can use your Windows Live ID. Craig has rounded up some really cool prizes (<i>Scripting Editor<\/i>: I don&rsquo;t know who this Craig guy is, but he sounds really awesome), and some of the Windows PowerShell user groups are even offering additional prizes for their members who participate. <\/p>\n<p class=\"MsoNormal\">SP, what you need to do is investigate WMI events. We have lots of Hey, Scripting Guy! Blog posts that talk about using <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/tags\/events+and+monitoring\/VBScript\/default.aspx\"><font face=\"Segoe\">WMI events from within VBScript<\/font><\/a>. We also have several Hey, Scripting Guy! posts that deal with <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/tags\/events+and+monitoring\/Windows+PowerShell\/default.aspx\"><font face=\"Segoe\">WMI events from Windows PowerShell<\/font><\/a>. <\/p>\n<p class=\"MsoNormal\">There are two types of WMI event classes that can be used. The first are intrinsic event classes, which are WMI classes that are designed to monitor for events. All these classes do is monitor for WMI events. An example of an intrinsic WMI event class is <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394374(VS.85).aspx\"><font face=\"Segoe\">Win32_ProcessStartTrace<\/font><\/a>. Because this is an intrinsic WMI class, you cannot use the <b>Get-WmiObject<\/b> (<b>gwmi<\/b> alias) cmdlet and pass the WMI class to the <b>Get-Member<\/b> (<b>gm<\/b> alias) cmdlet and see its properties and methods. An error is generated that states that no object has been supplied to the <b>Get-Member<\/b> cmdlet. This is true because the <b>Win32_ProcessStartTrace<\/b> WMI class is created when a new process is generated and WMI is configured to watch for new processes to start up. <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; gwmi win32_processStartTrace | gm<br \/>Get-Member : No object has been specified to the Get-Member cmdlet.<br \/>At line:1 char:34<br \/>+ gwmi win32_processStartTrace | gm &lt;&lt;&lt;&lt;<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: CloseError: (:) [Get-Member], InvalidOperationException<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>+ FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To use a &ldquo;regular&rdquo; WMI class to monitor for WMI events, it is necessary to use a generic WMI event class such as <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394649(VS.85).aspx\"><font face=\"Segoe\">__InstanceCreationEvent<\/font><\/a>. The thing that is cool about using the <b>__InstanceCreationEvent<\/b> WMI class is that it returns a complete copy of the WMI object that created the event. This allows you to obtain a lot of very useful information about the event. To use the <b>__InstanceCreationEvent<\/b> WMI class, it is necessary to compose an &ldquo;event query.&rdquo; You can do this by typing directly into the <b>&ndash;query<\/b> property of the Windows PowerShell console. The following is a single-line command that is wrapped onto two lines. To avoid confusion, I did not break it into two separate lines, nor did I close and open extra quotation marks and use line continuation. If you cut and paste the command, please ensure you have one line of code by either pasting it in Notepad and cleaning it up, or in the Windows PowerShell ISE before running the command:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Register-WmiEvent -Query &#8220;Select * from __InstanceCreationEvent within 5 where targetinstance isa &#8216;win32_logicaldisk'&#8221; -SourceIdentifier disk -Timeout 1000<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When you run the command, nothing is returned and nothing appears to have happened. When a USB drive is plugged into the computer, still nothing appears to happen. However, if you want to retrieve the event that was returned, use the <b>Get-Event<\/b> cmdlet as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-Event -SourceIdentifier disk<\/p>\n<p>ComputerName<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>RunspaceId<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 1564db50-15ae-4970-8ad3-d759d3092d96<br \/>EventIdentifier<span>&nbsp; <\/span>: 1<br \/>Sender<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: System.Management.ManagementEventWatcher<br \/>SourceEventArgs<span>&nbsp; <\/span>: System.Management.EventArrivedEventArgs<br \/>SourceArgs<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {System.Management.ManagementEventWatcher, System.Management.EventArrivedEventArgs}<br \/>SourceIdentifier : disk<br \/>TimeGenerated<span>&nbsp;&nbsp;&nbsp; <\/span>: 4\/9\/2010 12:10:16 PM<br \/>MessageData<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The information that is returned by the <b>Get-Event<\/b> cmdlet is les\ns than illuminating. But this is only because it returns several different objects. To find information about the newly inserted drive, store the object in a variable:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$a = Get-Event -SourceIdentifier disk<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Once you have the object stored in a variable, it is easy to query the <b>TargetInstance<\/b> from the <b>NewEvent<\/b> object. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$a.SourceEventArgs.NewEvent.TargetInstance<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When you run these commands, you receive the output shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of output of running the commands\" alt=\"Image of output of running the commands\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/april\/hey0413\/hsg-04-13-10-01.jpg\" width=\"800\" height=\"527\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">SP, that is all there is to using WMI events. WMI Week will continue tomorrow. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! I would like to be notified when I plug in a USB drive into my computer. I have some cool ideas for using that, but I do not know how to detect when a USB drive is plugged in. I know I can run a script that loops through looking for [&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,3,4,45,6],"class_list":["post-50663","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-events-and-monitoring","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! I would like to be notified when I plug in a USB drive into my computer. I have some cool ideas for using that, but I do not know how to detect when a USB drive is plugged in. I know I can run a script that loops through looking for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50663","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=50663"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50663\/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=50663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=50663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=50663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}