Half Diminished
Half Diminished

Reputation: 193

Pipeline Input/Output

I want to list the files of C: drive. First of all, I want to get the device ID from logical disk wmi object, and list it.

Below command returns:

Get-WmiObject -class Win32_logicaldisk


DeviceID     : C:  
DriveType    : 3  
ProviderName :  
FreeSpace    : 940371968  
Size         : 125809192960  
VolumeName   :  

But this command:

Get-WmiObject -class Win32_logicaldisk | select deviceid | Get-ChildItem -path {$_}

gives below error:

Get-ChildItem : Cannot find drive. A drive with the name '@{deviceid=C' does not exist. At line:1 char:60
+ Get-WmiObject -class Win32_logicaldisk | select deviceid | Get-ChildItem -path { ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (@{deviceid=C:String) [Get-ChildItem], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem -path accepts pipeline input, how we can solve this ?

Upvotes: 1

Views: 561

Answers (2)

xXhRQ8sD2L7Z
xXhRQ8sD2L7Z

Reputation: 1716

Your Select is returning an Object with a property named DeviceID.

Use -ExpandProperty to get the property value, then pipe that:

Get-WmiObject -class Win32_logicaldisk | select -expandproperty deviceid | Get-ChildItem -path {$_}

Upvotes: 3

David
David

Reputation: 621

You could also just select the property in the Object that gets returned. In this case, $_.DeviceID

Get-WmiObject -class Win32_logicaldisk | select deviceid | Get-ChildItem -path {$_.DeviceID}

Upvotes: 2

Related Questions