Where-Object not filtering correctly

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Jeff Taylor Jeff Taylor 7 months, 3 weeks ago.

  • Author
    Posts
  • #37791
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    I put together this one liner trying to only see where ProtectedFromAccidentalDeletion is FALSE:

    Get-ADOrganizationalUnit -Filter * -Properties ProtectedFromAccidentalDeletion | Where-Object {$_.ProtectedFromAccidentalDeletion -eq 'FALSE'}

    but I still see only OU's that have property = TRUE

    What am I missing?

  • #37792
    Profile photo of AK
    AK
    Participant

    Get-ADOrganizationalUnit -Filter * -Properties ProtectedFromAccidentalDeletion | Where-Object {$_.ProtectedFromAccidentalDeletion -eq $false}

  • #37794
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    Ah great thanks AK. Can you briefly explain that character for my edification please? I don't recall reading about it in Month of Lunches

  • #37802
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    'False' is a string, $false is a boolean. (The $ character, in this context, indicates a variable named false.)

    The way the -eq (and other comparison operators) work is that if the two types aren't the same, PowerShell will try to convert the value on the right to the same type as the value on the left. (In this case, it'll try to convert a string into a boolean).

    When it comes to strings, empty strings are treated as $false, and any non-empty string becomes $true. So, while it may look odd:

    $true -eq 'False'
    

    Will report True, because 'False' is a non-empty string.

    In the other direction:

    'False' -eq $true
    

    This will be false, because now the boolean $true is being converted into a string (which happens to just be the word 'True'.)

  • #37846
    Profile photo of AK
    AK
    Participant

    The only thing I'd mention is, if you pipe OU object to get-member, you'll see that ProtectedFromAccidentalDeletion property is boolean, not string, hence $true or $false when comparing values of that property:

    ProtectedFromAccidentalDeletion Property System.Boolean ProtectedFromAccidentalDeletion {get;set;}

  • #37865
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    Dave, got a bit lost on the examples but I appreciate the background and this will probably get clearer with more experience.

    AK I did see that and now in context, I have the background for the understanding. thanks.

  • #37872
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    I have to laugh. I wrote "I don't recall reading about it in Month of Lunches" and of course, I'm reading the last chapter today and there it is pg 299!

You must be logged in to reply to this topic.