Get disk space – %Free column

Welcome Forums General PowerShell Q&A Get disk space – %Free column

Viewing 3 reply threads
  • Author
    Posts
    • #222822
      Participant
      Topics: 9
      Replies: 12
      Points: 138
      Rank: Participant

      Hello,

      Using the following script, i could get the space and usage columns.

      what should i add to it to get a %free column instead of calculate it manually?

      Get-WmiObject Win32_LogicalDisk -ComputerName ComputerName -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, @{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}}
      
    • #222828
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      What do you mean with “calculate it manually”? You just have to add another calculated property … !?

      Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName ComputerName -Filter DriveType=3 | 
          Select-Object DeviceID, 
              @{'Name' = 'Size (GB)'; 'Expression' = { [string]::Format('{0:N0}', [math]::truncate($_.size / 1GB)) } }, 
              @{'Name' = 'Freespace (GB)'; 'Expression' = { [string]::Format('{0:N0}', [math]::truncate($_.freespace / 1GB))}},
              @{'Name' = 'Freespace (%)'; 'Expression' = { [string]::Format('{0:N0}', [math]::truncate( $_.freespace / $_.Size * 100 ))}}
    • #222843
      Participant
      Topics: 9
      Replies: 12
      Points: 138
      Rank: Participant

      I meant that when there is no % column i just did – free space*100/total space.

      thanks for your answer.

    • #222891
      Participant
      Topics: 12
      Replies: 1623
      Points: 2,565
      Helping Hand
      Rank: Community Hero

      Percentage can also be done with string format directly:

      Get-CimInstance -ClassName Win32_LogicalDisk  -Filter DriveType=3 | 
          Select-Object DeviceID, 
              @{'Name' = 'Size (GB)'; 'Expression' = { [string]::Format('{0:N0}', [math]::truncate($_.size / 1GB)) } }, 
              @{'Name' = 'Freespace (GB)'; 'Expression' = { [string]::Format('{0:N0}', [math]::truncate($_.freespace / 1GB))}},
              @{'Name' = 'Freespace (%)'; 'Expression' = { [string]::Format('{0:P}',  $_.freespace / $_.Size )}}
      

      The way that Olaf is presenting is better as it’s still a numeric value and this is a string. You can’t do comparison if you store it as a string

Viewing 3 reply threads
  • You must be logged in to reply to this topic.