Author Posts

June 8, 2018 at 10:34 am

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):


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.

June 8, 2018 at 10:56 am

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

June 8, 2018 at 11:07 am

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

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

June 8, 2018 at 11:36 am

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

Select-Object -property DeviceID

you do

Select-Object -ExpandProperty DeviceID

June 8, 2018 at 7:26 pm

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.

June 8, 2018 at 7:31 pm

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.

June 8, 2018 at 8:06 pm

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.