Filter Hard Drives by VolumeName

Welcome Forums General PowerShell Q&A Filter Hard Drives by VolumeName

Viewing 0 reply threads
  • Author
    • #5471
      Topics: 1562
      Replies: 0
      Points: 1
      Rank: Member

      by hibbijibbies at 2013-01-24 12:10:37


      I am fairly sure that I am asking something trivial, but I have tried hours on end without reaching a solution. We need to do capacity planning on our servers and hence need to add the hard drive total space, free space and % used for 2 volumes named ‘VMs’ and ‘B2D’ for each of a number of servers. Ideally I would like to save this data to a SQL DB from which I can run queries on a monthly basis, but this is beyond the scope of the question and totally beyond my comprehension at this stage.

      I can extract the ‘VolumeName’ for one hard drive, but am unable to dot it for more than one hard drive. I need to use the VolumeName as the drive letters are not the same for all servers and the VolumeName is the only common identifier across all servers. So my question is how do I cycle through my servers and only extract the hard drive volumes where they are called either ‘Primary’ and ‘Secondary’, add the data for the 2 volumes together, e.g Total Size, Used, Free, Free % and export the data in a text or .csv file. Hope it is clear what I am trying to achieve Here is my code: It returns an error message.

      gwmi Win32_logicaldisk -Filter "VolumeName -eq {‘Main Disk’ -or ‘Primary’}" -computer MyComputerName `
      | Select SystemName,DeviceID,VolumeName, `
      @{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}, `
      @{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}, `
      @{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} `
      | ft -auto

      by ArtB0514 at 2013-01-24 13:38:34

      Try another tack: use a WQL query instead of a filter. Note: Whenever you have errors in a long pipeline, split it up into logical sections (i.e., collection, processing, presentation) to better debug the process.

      $Query = "SELECT * FROM Win32_LogicalDisk WHERE VolumeName='Main Disk' OR VolumeName='Primary'"
      $Disks = Get-WmiObject -Query $Query -ComputerName $MyComputerName
      $Disks | Format-Table SystemName,DeviceID,VolumeName,
      @{Name="Size(GB)";Expression={"{0:N0}" -f ($_.size/1gb)}},
      @{Name="Free Space(GB)";Expression={"{0:N0}" -f ($_.freespace/1gb)}},
      @{Name="Free Space(%)";Expression={"{0:P2}" -f (($_.freespace/1gb) / ($_.size/1gb))}} -AutoSize

      You can get help on WQL at

      by sunnyc7 at 2013-01-24 14:43:22

      You can try this one.
      [code2=powershell]gwmi Win32_logicaldisk -Filter "DriveType=3" -computer MyComputerName`
      | Select SystemName,DeviceID,VolumeName, `
      @{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}, `
      @{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}, `
      @{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} `
      | ft -auto[/code2]
      I use this for my inventory:
      [code2=powershell]Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer $computer | Select DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Free Space(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}},@{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} | ft[/code2]

      by hibbijibbies at 2013-01-25 00:48:08

      Thanks ArtB0514. That gives me the exact result I am looking for. I am quite new to Powershell and other Microsoft technologies and didn’t even know of the existence of WQL. Thanks for your time and advice. I appreciate it.

Viewing 0 reply threads
  • The topic ‘Filter Hard Drives by VolumeName’ is closed to new replies.