How do I get the VALUE of a Property (in an object)??

Welcome Forums General PowerShell Q&A How do I get the VALUE of a Property (in an object)??

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
6 months, 1 week ago.

  • Author
    Posts
  • #102092

    Participant
    Points: 194
    Rank: Participant

    I have the following chain of commands:

    Get-WmiObject win32_logicaldisk | Where-Object VolumeName -eq "DDRIVE" | Select-Object -property DeviceID

    When executed, it produces 3 lines of output (actually 4, including the first line of blanks):

    DeviceID
    ——–
    D:

    What cmdlet, function, or ??? can I use to extract just the value of the DeviceID property, which in this case is D: ?
    This is so I can assign it to a variable which will be concatenated to a specific folder to form a complete path name.
    Get-ItemPropertyValue can't seem to do the job.
    Any advice, tips or pointers would be highly appreciated.

  • #102094

    Participant
    Points: 0
    Rank: Member
     Get-WmiObject win32_logicaldisk | Where-Object VolumeName -eq "DDRIVE" | % { write-output $_.DeviceID } 
    • #102142

      Participant
      Points: 194
      Rank: Participant

      Many thanks, Mr/Ms Bakhtyar ... your solution not only worked, but it made me realize why Get-ItemPropertyValue (which I was struggling to "make it work") does not work.
      Best,

  • #102095

    Participant
    Points: 10
    Rank: Member

    If I am not mistaken, all volume names are unique unless blank.

    (Get-CimInstance -ClassName win32_logicaldisk -Filter "VolumeName='DDRIVE'").DeviceID
    -OR-
    (Get-WmiObject -Class win32_logicaldisk -Filter "VolumeName='DDRIVE'").DeviceID
    
    • #102143

      Participant
      Points: 194
      Rank: Participant

      Yes, you are right. Many thanks for giving 2 ways of doing the same thing.
      While I do not yet know the finer differences between WmiObject and CmiInstance, I hope to read about them in order to appreciate which one is more appropriate under certain specific conditions, and other finer aspects as efficiency, etc..
      My sincerest thanks for your technical tip, it has enhanced my learning process.

  • #102098

    Participant
    Points: 269
    Helping Hand
    Rank: Contributor

    .... or the pure Powershell way ...
    instead of

    Select-Object -property DeviceID

    you do

    Select-Object -ExpandProperty DeviceID
    • #102146

      Participant
      Points: 194
      Rank: Participant

      Thank you! It works perfectly. The documentation on the -ExpandProperty parameter is limited, so I do not claim to understand what it is really doing behind the scenes. All I know is that in the object-oriented world of Poweershell (which is great!) there are other "new properties" – e.g., AliasProperty,NoteProperty – that have been added to the original Property concept in object-oriented approaches. Would be grateful if you can point me to references/sites/docs that explain the -ExpandProperty in more detail. Thanks again.

The topic ‘How do I get the VALUE of a Property (in an object)??’ is closed to new replies.