PowerShell: How to Use Get-CimInstance with Filter


You can use the Get-CimInstance cmdlet in PowerShell to get the CIM instances of a class from a CIM server.

Often you may want to filter the results from the Get-CimInstance cmdlet to only display specific instances.

You can use the following methods to do so:

Method 1: Use Get-CimInstance and Filter Based on One Pattern

Get-CimInstance -ClassName Win32_Process -Filter "Name like '%services%'"

This particular example will retrieve all instances that contain “services” anywhere in the name.

Method 2: Use Get-CimInstance and Filter Based on Multiple Patterns

Get-CimInstance -ClassName Win32_Process -Filter "Name like '%services%' OR Name like '%Mfe%'"

This particular example will retrieve all instances that contain either “services” or “Mfe” anywhere in the name.

The following example shows how to use each of these methods in practice.

Example 1: Use Get-CimInstance and Filter Based on One Pattern

We can use the following syntax to retrieve all CIM instances that contain “services” anywhere in the name:

Get-CimInstance -ClassName Win32_Process -Filter "Name like '%services%'"

The following screenshot shows how to use this syntax in practice:

PowerShell Get-CimInstance filter

Notice that this returns only the instances that contain “services” in the name.

Example 2: Use Get-CimInstance and Filter Based on Multiple Patterns

We can use the following syntax to retrieve all CIM instances that contain either “services” or “Mfe” anywhere in the name:

Get-CimInstance -ClassName Win32_Process -Filter "Name like '%services%' OR Name like '%Mfe%'" 

The following screenshot shows how to use this syntax in practice:

Get-CimInstance filter for multiple patterns

Notice that this returns only the instances that contain “services” or “Mfe” in the name.

Note that you can use as many OR statements as you’d like within the -Filter string to filter using even more patterns.

Note: You can find the complete documentation for the Get-CimInstance cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: Use Get-Content and Skip First N Lines
PowerShell: How to Use Get-ChildItem with Filter
PowerShell: Use Get-ChildItem with Multiple Filters

Leave a Reply