Performance of CIM_DataFile and Win32_Directory enumeration depends on the number of files or directories that are being enumerated, and, hence, can be very slow and can take hours.
In following example, we run the Get-CimInstance cmdlet to enumerate all the instances of the class CIM_DataFile. On a brand-new virtual machine, the operation took 38minutes.
|
How performance can be improved
Here are few alternatives that can improve performance of your PowerShell script if it includes CIM_DataFile or Win32_Directory instanceenumeration. These methods can be used on any other class with a large number of instances.
Alternative 1: Enumerating instances using a WQL query
You can use a WQL query to narrow down enumeration to single instances. When you use WQL query, the Get-CimInstance cmdlet executes faster.
For example, the following query returns result immediately:
|
If WQL query uses the LIKE operator, expect the query to be as slow as Get-CimInstance without a WQL query.
|
Alternative 2: Enumerating instances using Get-CimInstance with local instance
This method can only be used when the instance key property value is known.
|
Summary
When you’re enumerating instances in a class with large number of instances:
- Use WQL queries if enumeration can be scoped down.
- Run Get-CimInstance with the local instance if the key property value is known.
- Avoid using queries with the LIKE operator if possible.
- If you need full enumeration, plan for the time overhead that’s added to your script.
Important links:
Link to related post:
Thanks,
Milena Natanov [MSFT]
Standards-Based Management