LightningWar
LightningWar

Reputation: 975

Filter cim instance class by property name

I'm trying to filter results by property Name. I cannot use piping in my script.

Get-CimInstance -ClassName Win32_Product -Property Name -Filter "Microsoft*"

Returns the error: Get-CimInstance : Invalid query.

I'm trying to get output similar to this command:

Get-CimInstance -ClassName Win32_Product | ? {$_.Name -like 'Microsoft*'}

But without piping to Where-Object.

What am I doing wrong?

Upvotes: 2

Views: 7726

Answers (1)

Vivek Kumar Singh
Vivek Kumar Singh

Reputation: 3350

If you look at Get-Help Get-CimInstance -Full, you will find the following -

-Filter [<String>]
    Specifies a where clause to use as a filter. Specify the clause in either the WQL or the CQL query language.

    Note: Do not include the where keyword in the value of the parameter.

    Required?                    false
    Position?                    named
    Default value                none
    Accept pipeline input?       True (ByPropertyName)
    Accept wildcard characters?  false

You don't have to include the Where-Object here, and you need to write your code as a query. The -filter parameter will take the Property(Name in this case) in form of the Windows Query Language. You don't need to define explicitly mention the -Property parameter while making use of the -filter parameter. Furthermore, since you are using WQL, your wildcard search would change from * to %, much like that in SQL. Keeping these points in mind, you can use the below query -

 Get-CimInstance -ClassName Win32_Product -Filter 'Name like "Microsoft%"'

Upvotes: 5

Related Questions