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  Ramon Tan 1 week, 5 days ago.

  • Author
    Posts
  • #102092

    Ramon Tan
    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

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

      Ramon Tan
      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

    random commandline
    Participant

    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

      Ramon Tan
      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

    Olaf Soyk
    Participant

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

    Select-Object -property DeviceID

    you do

    Select-Object -ExpandProperty DeviceID
    • #102146

      Ramon Tan
      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.

You must be logged in to reply to this topic.