1

I'm making a script that finds all programs with names similar to "Microsoft Office S*" -or "Microsoft Office P*"

I got the code working while searching for one or the other, but not both at the same time. Some examples below:

WORKS

get-wmiobject Win32_Product | Where Name -like "Microsoft Office S*"| Format-Table IdentifyingNumber, Name, LocalPackage -AutoSize

WORKS

get-wmiobject Win32_Product | Where Name -like "Microsoft Office S*"| Format-Table IdentifyingNumber, Name, LocalPackage -AutoSize

DOESN'T WORK

get-wmiobject Win32_Product | Where Name -like ("Microsoft Office S*" -or "Microsoft Office P*")| Format-Table IdentifyingNumber, Name, LocalPackage -AutoSize

DOESN'T WORK

get-wmiobject Win32_Product | Where Name -like ("Microsoft Office S*") -or Name -like ("Microsoft Office S*")| Format-Table IdentifyingNumber, Name, LocalPackage -AutoSize

3 Answers 3

6

I believe the issue here is not using the Where-Object script block. If you reform your query to the following, your issues should go away.

Get-WmiObject Win32_Product | Where-Object {$_.Name -like "Microsoft Office S*" -or $_.Name -like "Microsoft Office P*"}
Sign up to request clarification or add additional context in comments.

1 Comment

Correct; with a stipulation: the scriptblock form is only necessary prior to v3 or when combining conditions as -and / -or are not parameters of Where-Object
-1

Aside from using a script block and -or with where, a regex like this is convenient:

where name -match 'Microsoft Office S|Microsoft Office P'

Comments

-1

Have a lok at the integrated help :

Get-Help Where

In the available Syntax, you'll find

 Where-Object [-Property] <string> [[-Value] <Object>] -Like [-InputObject <psobject>]  [<CommonParameters>]

In you third sample : "Microsoft Office S*" -or "Microsoft Office P*" evaluates to true, it doesn't work because it's the same as :

Where Name -like $True

Which is always false (except if name value is $True)

For your fourth sample, the get-help shows that the where-object has no 'or' parameter. So it can not work.

When reading the help, keep in mind that the inputObject parameter contains the pipelined psObject.

The proposals solutions are good but I will propose this other form that matches the help :

Where-Object -Property Name -Value '^Microsoft Office S|^Microsoft Office P' -match

If you need a compact One-Liner it's equivalent to :

? Name -match '^Microsoft Office (S|P)'

(? is an alias of where-object)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.