Search code examples
vbscriptwmi

VBS Invalid Query in Windows 10


Why does the following VBS code work in Windows 7 but gives error on Windows 10:

strComputer="."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'notepad.exe'")
Wscript.Echo colProcesses.Count

The error I'm getting in Windows 10 is:

test_2.vbs(4,1) SWbemObjectSet: Invalid query

What am I doing wrong here?


Solution

  • Try and use the below code. This should work

    strQuery = "select * from win32_process where Name = " & """" & "Notepad.exe" & """"
    strComputer="."
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery(strQuery)
    Wscript.Echo colProcesses.Count