Simple question about Filter or Where

Welcome Forums General PowerShell Q&A Simple question about Filter or Where

Viewing 4 reply threads
  • Author
    Posts
    • #188851
      Participant
      Topics: 3
      Replies: 4
      Points: 29
      Rank: Member

      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!

      Paul

    • #188863
      Participant
      Topics: 3
      Replies: 68
      Points: 366
      Helping Hand
      Rank: Contributor

      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

      UserMayChangePassword='True'

      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).

    • #189025
      Participant
      Topics: 2
      Replies: 510
      Points: 1,301
      Helping Hand
      Rank: Community Hero

      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. 🙂

      For more on operators (and Where-Object in great detail) I'd recommend PSKoans' AboutAssignmentAndArithmetic and AboutWhereObject topics.

    • #189031
      Participant
      Topics: 3
      Replies: 4
      Points: 29
      Rank: Member

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

      Thanks,

      Paul.

    • #189082
      js
      Participant
      Topics: 27
      Replies: 716
      Points: 1,879
      Helping Hand
      Rank: Community Hero

      The longer form:

      Where-Object { $_.UserMayChangePassword -eq $true } 
      
Viewing 4 reply threads
  • You must be logged in to reply to this topic.