How do I use variables inside condition statements?

Welcome Forums General PowerShell Q&A How do I use variables inside condition statements?

This topic contains 1 reply, has 2 voices, and was last updated by

js
 
Participant
6 months, 1 week ago.

  • Author
    Posts
  • #100446
    GS

    Participant
    Points: 1
    Rank: Member

    Hello,

    I have object and I want to use property of that object inside condition statement. How do I do that. Example is below. Object is called Diff and I want to use "PropertyName" property as a search condition in "Where" clause for second statement. How do I do that? In this specific case it's groups but needs to be dynamic.

    [DBG]: PS C:\gd\Documents\Projects\WHSSF> $diff | fl * -force
    
    
    PropertyName : groups
    RefValue     : ADSyncAdmins
    DiffValue    : {ADSyncOperators, ADSyncAdmins}
    
    [DBG]: PS C:\gd\Documents\Projects\WHSSF> $users | where {$_.groups -eq "ADSyncAdmins"}
    
    
  • #100449
    js

    Participant
    Points: 219
    Helping Hand
    Rank: Participant

    Like this?

    PS /Users/js> $prop = 'name'                                                          
    PS /Users/js> get-process | where {$_.$prop -eq 'trustd'}              
    
     NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
     ------    -----      -----     ------      --  -- -----------
          0     0.00      14.67     560.57    5995   1 trustd
    

The topic ‘How do I use variables inside condition statements?’ is closed to new replies.