ThisIsLegend1016
ThisIsLegend1016

Reputation: 105

Disk Space Check in Powershell

I am currently checking disk space size using power shell but was wondering how I can amend the below to add a percentage of space left?

Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1MB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1MB,2)}}

I have tried to do it by changing the math but no luck.

gwmi win32_logicaldisk  -Computername PCNAME| Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1MB,2)}},@{n="FreeSpace" (%);e={[math]::Round($_.FreeSpace/$_.Size*100,2)}}|Out-File c:\PMC\Disk\OutPut\Newcastle.txt

This just returns an error.

Upvotes: 0

Views: 12752

Answers (2)

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

Here's part of a script I have written that may help you:

If (Test-Path 'C:')
{
    $CDisk = GWMI Win32_LogicalDisk -Filter "DeviceID='C:'"
    $CDisk = @{'Size'      = [Math]::Round($CDisk.Size / 1GB);
               'FreeSpace' = [Math]::Round($CDisk.FreeSpace / 1GB)}
    $CDisk.Add('Usage', ($CDisk.Size - $CDisk.FreeSpace))
    $CDisk.Add('PercentUsage', [Math]::Round(($CDisk.Usage / $CDisk.Size) * 100))

    "C: drive free space: $($CDisk.FreeSpace)GB"
    "C: drive capacity:   $($CDisk.Size)GB"
    '--------------------------------'
    "Disk usage:          $($CDisk.Usage)GB ($($CDisk.PercentUsage)%)"
}

Upvotes: 0

guiwhatsthat
guiwhatsthat

Reputation: 2434

Here is an example how to do it:

gwmi Win32_LogicalDisk -Filter "DeviceID='C:'" | select Name, FileSystem,FreeSpace,BlockSize,Size | % {$_.BlockSize=(($_.FreeSpace)/($_.Size))*100;$_.FreeSpace=($_.FreeSpace/1GB);$_.Size=($_.Size/1GB);$_}| Format-Table Name, @{n='FS';e={$_.FileSystem}},@{n='Free, Gb';e={'{0:N2}'-f
$_.FreeSpace}}, @{n='Free,%';e={'{0:N2}'-f $_.BlockSize}} -AutoSize

output:

Name FS   Free, Gb Free,%
---- --   -------- ------
C:   NTFS 593.59   31.88 

Upvotes: 2

Related Questions