Share via


Get-WmiObject taking too much time to get executed

Question

Wednesday, April 25, 2018 7:01 AM

Query to get all installed application is taking too much time

Get-WmiObject -Class win32_product -ComputerName $Computer | Where-Object -FilterScript {$_.Name -match $ApplicationName}

All replies (4)

Wednesday, April 25, 2018 12:21 PM | 1 vote

Yes.  That command can be very slow as it validates every program it finds.  Use a filter to return just one application.

\(ツ)_/


Wednesday, April 25, 2018 12:29 PM

Hi niravath.pintu,

Get-WmiObject takes a long time, how to fix? Is a question that comes up regularly.

When you are query this command on external computers, for each computer is WMI creating a DCOM link, that takes time.

Use Invoke-Commandinstead, for better performance. If you do not want to use Invoke-Command, then always remote use CIM (Get-CimInstance) instead of WMI to gain speed.

LINKS:

CIM vs. WMI CmdLets – Speed comparison

Use PowerShell to Run WMI Commands on Remote Computers

Sincerely, Martien van Dijk. Please remember to mark the replies as answers if they help and unmark them if they provide no help. Check out My Blog!


Wednesday, April 25, 2018 12:30 PM

Neither Invoke-Command nor Get-CimInstance will provide noticaeable performance improvements with the class.

\(ツ)_/


Wednesday, April 25, 2018 12:56 PM

Win32_product returns every application, which takes a lot of time.  Using sql-like filters:

# 42 sec locally
Get-WmiObject win32_product -ComputerName $Computer -filter "name like '%$ApplicationName%'"

or

# 26 sec locally
Get-WmiObject win32_product -ComputerName $Computer -filter "name = '$ApplicationName'"

# 1 min 26 sec locally
Get-WmiObject win32_product -ComputerName $Computer |
  Where Name -match $ApplicationName

EDIT:
This class is particularly slow:  https://stackoverflow.com/questions/25083520/wmi-select-from-win32-product-takes-a-long-time