Powershell Needful Things put that in your pipeline

4Aug/1046

Check free space on volume mount points

Wow! It’s been a while since I have posted any scripts! This is mainly due to the fact that I am rather busy at work, and also working hard at completing my MCITP.

A while back a client of mine, asked if there was an easy way to use one computer to check the free space of mount points. This was a real problem for them, as the administrators would come in every morning and manually logon to each server, and use disk management to check the free space.

I was certain that there had to be a WMI object for mount points, so after a little digging, I came up with the following script:

$TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
$FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
$FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}

function get-mountpoints {
$volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize
}

$servers = (Get-Content .\servers.txt)

foreach ($server in $servers){
get-mountpoints
}

The script is written to collect server names from a text file, but you could use any other method to supply you server names.

Hope this helps someone else!

Comments (46) Trackbacks (1)
  1. hi, very nice script. How can I get it to email the results as well ?

  2. Hi there,

    Thanks for using the script! I would suggest export-csv and then send-mailmessage.

    I hope this helps

  3. Rather than dividing by 1073741824 to convert to GB, you could divide by 1GB, e.i. “[math]::round(($_.Capacity/1GB)”. It’s a lot easier to read and remember.

    • Hi Robert,

      Thanks for the reply. I have used 1GB before in scripts, and I cannot remember why I used the exact amount for a gigabyte in this case. I seem to recall that it had to be more specific. In any event, I would have to test to see if I get the same result with both.

      The real reason for posting this script was to show how to isolate the volume mount points (or volumes with no drive letter assigned).

      Thanks for the feedback, and thanks for using the script!

      Jean

  4. Jean,

    This is the script i was looking for and works fine.
    Only problem i faced while testing is it just picks only server which is at the bottom from the servers.txt file.

    And another problem i encountered is cannot export using Export-CSV, i used Out-File.

    Below is ur script which i am using it, i don’t understand where i am doing wrong.

    $TotalGB = @{Name=”Capacity(GB)”;expression={[math]::round(($_.Capacity/ 1073741824),2)}}
    $FreeGB = @{Name=”FreeSpace(GB)”;expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
    $FreePerc = @{Name=”Free(%)”;expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}

    function get-mountpoints {
    $volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
    $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize | Out-File MountPointDB.csv
    }

    $servers = (Get-Content .\servers.txt)

    foreach ($server in $servers){
    get-mountpoints
    }

    Hope to hear back from you soon. Please send me an email if u modify the script @ Kadar_Shahid@hotmail.com.

    Thanks
    Shahid

    • Shahid,

      Sorry for the late reply, I have been out of town.

      The reason your export-csv fails is the format table in the pipeline.

      Try replacing the line with something like this:

      $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Out-File MountPointDB.csv

      Let me know how you get on.

      Jean

  5. Jean,

    I tried the out-file as suggested on May 25.

    When i do this I am only getting the results of the last system scanned.

    • Jesse, Please email me a copy of your script. You may be using it against the wrong variable.

      • Hi, I found this script which works great except that I only get the last server in the list. I have a list of 12 or more servers but only the last one shows in the csv file. any ideas would be greatly helpful. here is the full script

        $TotalGB = @{Name=”Capacity(GB)”;expression={[math]::round(($_.Capacity/ 1073741824),2)}}
        $FreeGB = @{Name=”FreeSpace(GB)”;expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
        $FreePerc = @{Name=”Free(%)”;expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}

        function get-mountpoints {
        $volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
        $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize | Out-File MountPoint.csv
        }

        $servers = (Get-Content .\servers.txt)

        foreach ($server in $servers){
        get-mountpoints
        }

        Thanks!

        • Doug,

          Thanks for using the script, please try the following function instead:

          function get-mountpoints {
          $volumes += Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
          $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize | Out-File MountPoint.csv
          }

  6. $TotalGB = @{Name=”Capacity(GB)”;expression={[math]::round(($_.Capacity/ 1073741824),2)}}
    $FreeGB = @{Name=”FreeSpace(GB)”;expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
    $FreePerc = @{Name=”Free(%)”;expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}

    function get-mountpoints {
    $volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
    $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize | Out-File MountPointDB.csv
    }

    $servers = (Get-Content .\servers.txt)

    foreach ($server in $servers){
    get-mountpoints
    }

    I got the output “MountPointDB.csv” 0 bytes

    if I use

    $volumes | Select SystemName, Label, DriveLetter, $TotalGB, $FreeGB, $FreePerc | Export-Csv MountPointDB.csv

    I got the output “MountPointDB.csv” 0 bytes

    if use:
    if I use

    $volumes | Select SystemName, Label, DriveLetter, $TotalGB, $FreeGB, $FreePerc >> c:\MountPointDB.csv

    I go a txt file with 5 divisions but I tried to open with excel format and I got all information in single columma.

    I really need to have 5 columms.
    Thanks

    • Jose,

      I dont have a server at the moment where I can test the script, but please check my reply to Doug above, and let me know if that helps you at all.

      Thanks

      Jean

  7. Hi, I found this script which works great except that I only get the last server in the list. I have a list of 12 or more servers but only the last one shows in the csv file. any ideas would be greatly helpful. here is the full script

    $TotalGB = @{Name=”Capacity(GB)”;expression={[math]::round(($_.Capacity/ 1073741824),2)}}
    $FreeGB = @{Name=”FreeSpace(GB)”;expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
    $FreePerc = @{Name=”Free(%)”;expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}

    function get-mountpoints {
    $volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
    $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize | Out-File MountPoint.csv
    }

    $servers = (Get-Content .\servers.txt)

    foreach ($server in $servers){
    get-mountpoints
    }

    Thanks!

    • Hi Jean Louw

      I found another solution and it is working fine for me, your script stay the way it is. I created another script to run yours and get csv file report with all my servers.

      $AppName = “SPACE-REPORT.ps1”
      $AppVer = “v2.0 [Script created 26th October 2012]”

      ##########################################################################################
      #Display script name and version
      ##########################################################################################
      Write-host $AppName -NoNewLine -foregroundcolor Green
      Write-Host “: ” $AppVer -foregroundcolor Green

      .\Mountpointspacetest.ps1 | EXPORT-CSV -PATH C:\REPORT\MountSpace-Report.csv

      • Hi…

        I am not able to epxport data’s in excel format, can you please help me on this.

        Thanks
        Kumar

        • Kumar,

          This script contains a format table at the end. You cannot send format table to export-csv. Please remove the format table before you export to CSV.

          I hope this helps.

          Jean

    • Try
      function get-mountpoints ($server)
      {
      $volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
      $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize | Out-File MountPoint.csv
      }

      $servers = (Get-Content .\servers.txt)

      foreach ($sevr in $servers){
      get-mountpoints $sevr
      }

  8. I am struggling with extracting only the numeric value of the %FreeSpace from the script. Can you help me and tell me how to do that? I need to perform other actions based on the value of the %FreeSpace. Thank you.

    • Hi there,

      Sorry for the late rely, I have been extremely busy with clients at the moment. Please could you explain to me in more detail what you need from the script?

      Thanks

      Jean

  9. I need out put below format

    Mount point Name TotalSpace_MB FreeSpace_MB PercentFree Alert

    Example instead of drive space it should show mount pointname.

    Drive TotalSpace_MB FreeSpace_MB PercentFree Alert
    —– ————- ———— ———– ——————————
    C 21093 2916 13
    D 257644 254567 98
    N 307195 211024 68
    O 207150 194354 93
    P 155362 63136 40
    R 310733 15342 4 <============ Critical !!!
    S 131068 131003 99

  10. I have tried working for individaul server not multiple servers(dynamically servers has to pick and result has to come in csv format)

    Get-WmiObject Win32_Volume -Filter “DriveType=’3′” | ForEach {
    New-Object PSObject -Property @{
    Name = $_.Name
    Label = $_.Label
    FreeSpace_GB = ([Math]::Round($_.FreeSpace /1GB,2))
    TotalSize_GB = ([Math]::Round($_.Capacity /1GB,2))
    }
    }

  11. Great script, thanks a lot. Of course I am using only the central two lines

    $volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
    $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize

    And then I go through the results in $volume in a Loop and collect everything for Display. I fear those who used a csv-export inside the Loop here, are overwriting the Outputfile, making it to Display only the last server’s figures.

    Rosario

  12. Jean,
    First, Thank you for the great script. The math was a huge help.
    I have tweaked your script a little and I thought I would repost. Again all credit to you and thank you for your help. Below I have adjusted the following – Output now sends to true CSV file via the Export-CSV command. As a result Format-Table is not used and the Select command becomes Select-Object to support the CSV format. I have also added the append switch to the output and added a DEL command to remove any old MountPoint.CSV files before run. This allows it to be quickly configured to act as a running log or a new run each time. Thanks again!

    $TotalGB = @{Name=”Capacity(GB)”;expression={[math]::round(($_.Capacity/ 1073741824),2)}}
    $FreeGB = @{Name=”FreeSpace(GB)”;expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
    $FreePerc = @{Name=”Free(%)”;expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity /

    1073741824)) * 100),0)}}

    DEL .\MountPoint.csv

    function get-mountpoints {

    $volumes += Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
    $volumes | Select-object SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Export-Csv .\MountPoint.csv

    -Append
    }

    $servers = (Get-Content .\servers.txt)

    foreach ($server in $servers){
    get-mountpoints
    }

    • Matthew,

      I am glad the script helped you, and thanks for the update! Sorry it takes so long to approve posts, the number of spam messages in between valid comments are so many.

      Thanks again for posting!

      Jean

  13. Here is a version that will output the stats to a nice HTML page and email you the results as well.

    Cheers!

    Ed McKinzie
    http://edmckinzie.wordpress.com

    #Delete and then Create a new HTML file to contain the stats
    remove-item “C:\MOUNT_POINT_FREE_SPACE.html”;
    $file = New-Item -type file “C:\MOUNT_POINT_FREE_SPACE.html”;

    $servers = “Server1″,”Server2″,”Server3” # Note you can also use the get-content feature here

    #Configure the HTML Properties, including Style, Body, and Title
    $a = $a + “File Server – Mount Point Free Space”
    $a = $a + “BODY{color:white;font-family:verdana;font-size:8pt;font-color:white;background-color:black}table{border-style:solid;border-width:thin;border-color:white;width:100%;}th{font-size:8pt;text-align:left;}td{font-size:8pt;background-color:#000000;}”
    $a = $a + “Exchange Mailbox Server – Mount Point Free Space”
    $a = $a + “”

    #Format the output
    $TotalGB = @{Name=”Capacity(GB)”;expression={[math]::round(($_.Capacity/ 1073741824),2)}}
    $FreeGB = @{Name=”FreeSpace(GB)”;expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
    $FreePerc = @{Name=”Free(%)”;expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}

    #Create a loop to iterate through each of the servers
    Foreach ($server in $servers){

    $volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null} | `
    Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Sort “FreeSpace(GB)” |
    ConvertTo-HTML -head $a | Out-File $file -Append #Note the append…..which increments the file

    }

    #Format the Body of the Email Message
    $Message_Formatting = “Hello, Attached are the Mount Point Sizes for each of the Servers.If you have any questions, contact at .”

    #Email The results:
    Send-MailMessage -To “” -From “” `
    -Subject “Mount Point Size Status” -SmtpServer “” -Body ($Message_Formatting)-BodyAsHtml -Attachments $file

  14. Thx for the script, but when i run the script it just picks the last name from server list.. any help is much appreciated

  15. this is really a very nice script and have been chasing google for 2 days to get mounted volumes size and freespace.. and finally got your script the way i wanted.. thank you so much…

  16. Hi Jean,

    Thanks a lot for the script, I modified it a bit to send a mail report of all the mount point details of various servers. I was wondering if we can add colors in a row based on threshold value for any columns selected. For example:

    SystemName Label Capacity(GB) FreeSpace(GB) Free(%)
    Server1 P:\System 15.99 1.5 1%
    Server1 U:\MSSQL\Backup 67.99 28.36 42

    In above example I want 1st row to be highlighted where I am considering Free(%) column’s min threshold set to 10

    Awaiting your reply. TIA

    Regards,
    Sajal

    • Sajal,

      Yes, adding colour is very possible, but I would need time to write the code which I dont exactly have much of right now 🙁

      You are welcome to modify the script to suit this requirement if you want.

      Jean

  17. Thanks for this awesome script!

    I’m trying to figure out a way to capture the freespace value to a variable for use in an if statement to send an alert email if drive space is low. This seems like it should be simple enough but I’ve been unsuccessful thus far.

    This is basically what I want to accomplish but I can’t seem to get the value into the variable.

    if ($FreeGB -lt 100.00) {$mail = $true}

  18. $TotalGB = @{Name=”Capacity(GB)”;expression={[math]::round(($_.Capacity/ 1073741824),2)}}
    $FreeGB = @{Name=”FreeSpace(GB)”;expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
    $FreePerc = @{Name=”Free(%)”;expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}

    function get-mountpoints {
    $volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
    $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize
    }

    $servers = (Get-Content .\servers.txt)

    foreach ($server in $servers){
    get-mountpoints
    }

    In the above query, can someone please help me to get the server names listed in the output. I want server name instead of System Name.
    Kindly reply asap.

  19. Thanks it was very helpful.
    To fix the only last server details I modified the line to write the complete output to the .csv file as this:

    function get-mountpoints {
    $volumes += Get-WmiObject -computer $server win32_volume -Filter “DriveType != 5” #exclude CD Drives
    $volumes | Select Name,SystemName, Label, $TotalGB, $FreeGB, $FreePerc,$DriveType |ConvertTo-CSV -NoTypeInformation -Delimiter “;”| out-file $out_file -append
    }

  20. Hey Jean,
    I have an inquiry about your script. For finding the mount points, why would you choose to ignore volumes which do not have a drive letter assigned to them?
    Isn’t my C:\ also a mount point?

    • Hi there,

      This script is used mostly by Exchange admins to monitor the mount points where Exchange databases are stored.

      You can adapt the script to show all drives.

      Jean

  21. Thanks! Very Useful.

  22. Hi Jean,

    Can I implement this script in Linux environment?

    Thanks.

  23. I’m late to the game, but this is awesome! Thanks for the script!

  24. Your script is wonderful. I used your original script with one modification to get the result into a file

    ForEach ($server in $Servers) {
    get-mountpoints >> .\mountpoints.txt
    }

    It works.


Leave a comment