Bart's Weblog

Just a blog…

PowerShell: How to retrieve disk size & free disk space for a list of computers (input file)

Posted by bartvdw on 1919/0606/2008

Note: works only with V2 which is currently in CTP stage!

The command below will show you a very nice overview of disk size & free space for a list of computers (input file). The great thing about PowerShell, from my point of view, is “piping” commands into 1 big command which eliminates the need to create a script for certain operations. Secondly in V2 (CTP), the “gridview” option is amazing!

Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name=”size(GB)”;Expression={“{0:N1}” -f($_.size/1gb)}},@{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} | Out-GridView

About these ads

39 Responses to “PowerShell: How to retrieve disk size & free disk space for a list of computers (input file)”

  1. ghanshyam said

    wow! Thats a awesome script

  2. abdul said

    thanks for the script…works great…how do i get this to output to a file and send an email to myself?

    I tried to pipeline to csv or txt…but it failed. shouldnt it just be this:

    Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name=”size(GB)”;Expression={“{0:N1}” -f($_.size/1gb)}},@{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} | Export-CSV drivesizes.csv

    $msg = new-object Net.Mail.MailMessage
    $att = new-object Net.Mail.Attachment($filename)
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)

    $msg.From = “admin”
    $msg.To.Add(”admin”)
    $msg.Subject = “drive size”
    $msg.Body = “The drive size file is attached”
    $msg.Attachments.Add($att)

    $smtp.Send($msg

    Am I missing something? any help will be appreciated…thanks.

    • Addy said

      try Export-csv -path .\drivesizes.csv

    • Addy said

      you can also try. Using the -append argument will write to the same file in case you are going through an array of computers and need info in the same file.
      Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name=”size(GB)”;Expression={“{0:N1}” -f($_.size/1gb)}},@{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} | out-file -filepath .\drivesizes.csv -append

  3. Ray said

    Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer (Get-Content c:\computers.txt) | It’s not working for me… what am I doing wrong. I have version 2.

    Select SystemName,DeviceID,VolumeName,@{Name=”size(GB)”;Expression={”{0:N1}” -f($_.size/1gb)}},@{Name=”freespace(GB)”;Expression={”{0:N1}” -f($_.freespace/1gb)}} | Out-GridView
    Unexpected token ‘:N1′ in expression or statement.
    At line:1 char:169

    Unexpected token ‘:N1′ in expression or statement.
    At line:1 char:231

    Encountered end of line while processing a string token.
    At line:1 char:274

  4. Ray said

    Figured it out. The script didn’t copy over properly.

  5. Victor said

    Hi I have this error…………where I am wrong?

    PS C:\Documents and Settings\MSIP35> Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name=”size(GB)”;Expression={”{0:N1}” -f($_.size/1gb)}},@{Name=”freespace(GB)”;Expression={”{0:N1}” -f($_.freespace/1gb)}} | Out-GridView
    Unexpected token ‘:N1′ in expression or statement.
    At line:1 char:177

    Unexpected token ‘:N1′ in expression or statement.
    At line:1 char:239

    Encountered end of line while processing a string token.
    At line:1 char:282

  6. David said

    Nice little script!

    I had to change all the quotes into “straight quotes” after copying/pasting into my PowerShell window in order for it to work.

    Then I added a calculated field to the grid output to show the percent free on each HD. The calculation works correctly, but displays as a long decimal ( .6934270627776, for example). How do I get that to show as 69 or 69%?

    I also made a couple of minor edits for cosmetic changes to the grid.
    Here’s what I currently have:
    Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,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:N1}” -f($_.freespace/1gb) / ($_.size/1gb)}} | Out-GridView -Title “Drive Space”

  7. 2David:
    it’s siple, only multiplay on 100
    exaple:
    -f(100 * ($_.freespace/1gb) / ($_.size/1gb))

    2bartvdw:
    thanx for your post

  8. Pimp said

    Here is the command with the percent calculated and formatted correctly in the gridview window.

    Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer localhost | Select SystemName,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))}} | Out-GridView -Title “Drive Space”

  9. Aboufares said

    Instead og 1gb use 2^30

  10. Echo said

    could someone please let me know how to add the values of the different drives it polls and report the total disk space on the system
    e.g. SystemName Dev ID Size(gb) TotalSize
    server x c 10gb 50GB
    server x e 15gb
    server x f 25gb

  11. Andy said

    I added [decimal] so the numbers sort correctly


    Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer localhost | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={[decimal]("{0:N1}" -f($_.size/1gb))}},@{Name="Free Space(GB)";Expression={[decimal]("{0:N1}" -f($_.freespace/1gb))}},@{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} | Out-GridView -Title "Drive Space"

  12. Stanley said

    This is a great script, however I can not get it to parse my text file. If I give it one server name at a time it works without any problems. I get this error message when attempting to give the script a text file.

    Get-WmiObject : Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty. Supply an argument
    that is not null or empty and then try the command again.
    At line:1 char:95
    + Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer <<<< (Get-Content c:\
    scripts\servers.txt) | Select SystemName,DeviceID,VolumeName,@{Name="size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@
    {Name="freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | Out-GridView
    + CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    • Bruce said

      you need a loop to process each computer. You will get a grid for each server, unless you drop the output into a hash table then output it.

      ##
      $servers = Get-Content C:\scripts\data\servers.txt

      foreach ($server in $servers){
      Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ `
      | Select SystemName,DeviceID,VolumeName,`
      @{Name=”Size(GB)”;Expression={[decimal](“{0:N1}” -f($_.size/1gb))}},`
      @{Name=”Free Space(GB)”;Expression={[decimal](“{0:N1}” -f($_.freespace/1gb))}},`
      @{Name=”Free Space(%)”;Expression={“{0:P2}” -f(($_.freespace/1gb) / ($_.size/1gb))}}
      }}
      ##

      • Bruce said

        $servers = Get-Content C:\scripts\data\servers.txt

        foreach ($server in $servers){
        Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer $server `
        | Select SystemName,DeviceID,VolumeName,`
        @{Name=”Size(GB)”;Expression={[decimal](“{0:N1}” -f($_.size/1gb))}},`
        @{Name=”Free Space(GB)”;Expression={[decimal](“{0:N1}” -f($_.freespace/1gb))}},`
        @{Name=”Free Space(%)”;Expression={“{0:P2}” -f(($_.freespace/1gb) / ($_.size/1gb))}}
        }

        doh – forgot to put the $server variable in the gwmi portion.

  13. [...] This post has the command I needed. [...]

  14. jay said

    Thank you very much this really helped.!!

  15. Tom Purl said

    Awesome script. Thanks for sharing!

  16. Marcel said

    Thanks for sharing!

    There is one thing that keeps bothering me, and that is the fact that sorting by Free Space (%) does not work like expected. When using sort “Free Space(%)” the output is like 88.23 %, 89.36 %, 9.01 %, 9.94 %, 90.07 %.

    Of course the 9.01%, the lowest of them all, should be on top of the list. How can I make Powershell sort numbers as numbers and not as text?

  17. Jay said

    Modified to show Used space instead of Free Space.

    Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer (Get-Content c:\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name=”Size(GB)”;Expression={[decimal](“{0:N1}” -f($_.size/1gb))}},@{Name=”Used Space(GB)”;Expression={[decimal](“{0:N1}” -f(($_.size/1gb)-($_.freespace/1gb)))}},@{Name=”Used Space(%)”;Expression={“{0:P2}” -f((($_.size/1gb) – ($_.freespace/1gb)) / ($_.size/1gb))}} | Out-GridView -Title “Drive Space”

  18. Roberto said

    When I copy and paste your script:

    Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name=”size(GB)”;Expression={“{0:N1}” -f($_.size/1gb)}},@{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} | Out-GridView

    I get this error:

    Unexpected token ‘:N1′ in expression or statement.
    At line:1 char:180
    + Get-WMIObject Win32_LogicalDisk -filter “DriveType=3? -computer (Get-Content c:\scripts\computers.txt) | Select Syste
    mName,DeviceID,VolumeName,@{Name=”size(GB)”;Expression={“{0:N1 <<<>

    And not the information I need

    • Johnny said

      Roberto, Im sure you have figured it out by now but I also ran into the same issue.

      Solution: Replace all the speech marks again. Its a copy and paste issue.

      Good Luck and many Thanks,

      A

  19. Paul LaVigne said

    Excellent script (once I figured out how to handle the unexpected token error by changing all the quotes to straight quotes)

    Question: How would I format the output to keep all of the drives for a particular server name on the same line (row), instead of creating a new line for each drive?

  20. Mayank said

    Me too getting this error….

  21. Mail Man said

    There is mistake in quotation mark. You should replace them with normal quotation mark sign “

  22. meda said

    Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer $VM | Select SystemName,DeviceID,VolumeName,@{Name=”Size(GB)”;Expression={[decimal](“{0:N1}” -f($_.size/1gb))}},@{Name=”Free Space(GB)”;Expression={[decimal](“{0:N1}” -f($_.freespace/1gb))}},@{Name=”Free Space(%)”;Expression={“{0:P2}” -f(($_.freespace/1gb) / ($_.size/1gb))}} | ft -auto

    this worked great . it misses the mount point volumes.

  23. Adelo said

    This is awesome but is there anyway to get the % of space utilized or % of free space?

  24. Anyway we can export this to a CSV? I’m having a time getting it to do that. Kind of a beginner with ps scripting, any help greatly appreciated.

  25. rsr72 said

    Nice!

  26. Jon said

    Copy and pasted the above command, but it can not seem to find that text file. Any ideas?

    • Jon said

      Get-Content : Cannot find path ‘C:\Scripts\computers.txt’ because it does not exist.
      At C:\Scripts\Disk_Size_Script.ps1:3 char:27
      + $strComputer = get-content <<<< "computers.txt"
      + CategoryInfo : ObjectNotFound: (C:\Scripts\computers.txt:String) [Get-Content], ItemNotFoundException
      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

  27. Octive said

    Here is what I came up with,
    I opted for the grid view and creating a .csv file to help with my reporting.

    $servers = Get-Content C:\drivesize\computers.txt

    $DriveSize = foreach($server in $servers){
    Get-WMIObject Win32_LogicalDisk -filter “DriveType=3″ -computer $server `
    | Select SystemName,DeviceID,VolumeName,`
    @{Name=”Size(GB)”;Expression={[decimal](“{0:N1}” -f($_.size/1gb))}},`
    @{Name=”Used Space(GB)”;Expression={[decimal](“{0:N1}” -f(($_.size/1gb)-($_.freespace/1gb)))}},`
    @{Name=”Used Space(%)”;Expression={“{0:P2}” -f((($_.size/1gb) – ($_.freespace/1gb)) / ($_.size/1gb))}}
    }
    $DriveSize | Out-GridView -Title ‘Drive Size’
    $DriveSize | Export-CSV -Path “c:\drivesize\drive.csv” -NoTypeInformation

  28. Sayed said

    Thank you so much everyone on this thread. damn good script. it made my worked really easy.

  29. Jose said

    You can use this scritp:
    code:
    Mountpointspacetest.ps1

    # ———-start of mountpointspacetest.ps1—————————————————————-

    $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

    $volumes | Select SystemName, Label, DriveLetter, $TotalGB, $FreeGB, $FreePerc

    }

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

    foreach ($server in $servers){

    get-mountpoints

    }

    # ———-end of mountpointspacetest.ps1—————————————————————-

    Servers.txt

    # ———-start of servers.txt—————————————————————-

    ECEVS01

    spevs011

    frxevs01

    # ———-end of servers.txt—————————————————————-

    create severs. txt file.

    Run this scritp from powershell script like:
    SP1>\Mountpointspacetest.ps1 | EXPORT-CSV -PATH C:\REPORT\MountSpace-Report.csv.

  30. Jerry said

    Great Script!
    For the error regarding the quotes, I simply removed the quotes from “DriveType=3″ and everything became happy Big thanks also to those also showing the variations! very useful and much appreciated!
    Cheers,
    Jerry

  31. Jig said

    I would like to add OS version and make/model of the server in the same output. Anyway to accomplish that?

  32. Wim said

    Hello i’m new in powershell can you help me with 2 questions.
    1) I want to run the PS only for drive F:\ (how to get this done?)
    2) I want to run de PS from de scheduler how to do this.
    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: