Simple question about Filter or Where

How do I filter the Get-LocalUser output?

I am just learning filtering and/or where-object and need some pointers… what is the right way to do the following?

Get-LocalUser | Select Name, UserMayChangePassword | Where-Object “UserMayChangePassword=‘True’”

Thanks in advance!


You’re almost there, just change your Where-Object statement to this:

Where-Object "UserMayChangePassword" -eq 'True'

In general, when you wrap something in quotes the shell treats it as a single string, so with your statement like this:
Where-Object “UserMayChangePassword=‘True’”
you’re telling the shell to find an object with a property name like


which of course doesn’t work. The property name and value have to be fed to Where-Object as separate pieces of information.

In addition, the syntax for Where-Object requires that you use the logical comparator -eq rather than the arithmetic operator = (simply put, you’re doing logic, not math).

That about covers it, nice job! I’d just like to point out that = isn’t really an arithmetic operator, it’s just an assignment operator. In PS, = is only ever used to assign a value to something. That value may be the result of some arithmetic operation, or it may be any number of other possible values. $value = @{} doesn’t involve any arithmetic, but it does involve assignment. :slight_smile:

For more on operators (and Where-Object in great detail) I’d recommend PSKoansAboutAssignmentAndArithmetic and AboutWhereObject topics.

Thank you so much - I will admit that I do not understand it yet but this helped tremendously.



The longer form:

Where-Object { $_.UserMayChangePassword -eq $true }