Question

I am trying to make a list of drive letters but include only the ones which the driveletter and the filesystem are not null. An example of what I have now is

$winvolume = Get-WmiObject -computername $a -class win32_volume | Select-Object -Property driveletter, filesystem, capacity, freespace

foreach ($i in $winvolume.driveletter) { 
    if ($i -ne $null){ $drive = $i + ',' + $drive } 
}

this outputs the format correctly, but only checks if the driveletter is null or not, how can I also check against $winvolume.filesystem before listing these drive letters?

Thanks!

Was it helpful?

Solution

If I understand your question correctly, just loop over $winvolume instead, that will give you access to both fields;

foreach($i in $winvolume) {
   if ($i.filesystem -ne $null -and $i.driveletter -ne $null) {
      $drive = $i.driveletter + ',' + $drive 
   }
}

OTHER TIPS

An alternative would be to only retrieve the volumes matching your criteria in the first place, like:

$winvolume = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter IS NOT NULL AND FileSystem IS NOT NULL"

$drive = ($winvolume | Select-Object -ExpandProperty DriveLetter) -join ","

Or combined:

$drive = (Get-WmiObject -Class Win32_Volume -Filter "DriveLetter IS NOT NULL AND FileSystem IS NOT NULL" | Select-Object -ExpandProperty DriveLetter) -join ","
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top