0

I have a PSObject which looks like that:

Name                 : Full Name
givenname            : Full
surname              : Name
emailaddress         : email@domain.com
Direct               : xxx
Mobile               : xxx
Prop1                : TRUE
Prop2                : TRUE
Prop3                : TRUE
Prop4                : TRUE
Prop5                : TRUE
Prop6                : TRUE
Prop7                :
Prop8                : 

I want to get all the property names that have a TRUE value.

I can get this info on that way:

$Props = $Object | Get-Member -MemberType NoteProperty |
         Select -ExpandProperty Name
$temp = @()
foreach ($prop in $Props) {
    if ($Object.$Prop -eq $true) {
        $temp += $prop
    }
}

But I wonder if there's a better/easier method.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
JustCurious
  • 183
  • 1
  • 11
  • This Is not the same question, nor answer the question, I need to get only the true values without using loop, that was the question from the first place(see my example) I looking for other methods... thanks – JustCurious Feb 28 '16 at 14:05
  • 2
    The first answer in the linked question is probably the best answer. But you still have to add an if or where-test to find the ones with true-value. Adding the test itself is not worth having a 99% duplicate question. – Frode F. Feb 28 '16 at 14:24
  • 1
    @JustCurious As a matter of fact the question *is* the same: how to enumerate the properties of an object. And the accepted answer is, at least to my knowledge, the best way to do it. – Ansgar Wiechers Feb 28 '16 at 14:27
  • ok guys, that's all I wanted to know, if this is the best approach or if there's another method, if it's the best you know, it's enough. thanks – JustCurious Feb 28 '16 at 14:45
  • 1
    $Object.PSObject.Properties | ? { $_.Value -eq $true } | % { $_.Name } – Marc Kellerman Feb 28 '16 at 16:03
  • @MarcKellerman thanks, you can add it as answer... – JustCurious Feb 29 '16 at 10:10
  • The approach is essentially the same, just using `ForEach-Object` and `Where-Object` instead of `foreach` and `if`. – Ansgar Wiechers Feb 29 '16 at 11:08
  • @JustCurious I can't, the question has been closed. – Marc Kellerman Feb 29 '16 at 16:13

0 Answers0