DEV Community

redhcp
redhcp

Posted on • Edited on

Powershell % disk space

You can use this command for see space of all disks.

Write-Host "Host: $env:COMPUTERNAME ($(Get-Date -Format “MM/dd/yyyy”)) "  -foregroundcolor "green"
  Get-WMIObject  -Class Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}  `
    | Select-Object @{n="Unit";e={($_.Name)}}, 
                    @{n="Label";e={($_.VolumeName)}}, 
                    @{n='Size (GB)';e={"{0:n2}" -f ($_.size/1gb)}}, 
                    @{n='Free (GB)';e={"{0:n2}" -f ($_.freespace/1gb)}}, 
                    @{n='% Free';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
Enter fullscreen mode Exit fullscreen mode

Note: for remote host use Enter-PSSession <ComputerName> / Exit-PSSession and run script.

or for many hosts can use invoke command adding parameter COMPUTER_NAME .

$machines = @(
    'COMPUTER_NAME_1',
    'COMPUTER_NAME_2'
)
foreach ($machine in $machines) {
    Invoke-Command -ComputerName $machine -ScriptBlock {

         Write-Host "Host: $env:COMPUTERNAME ($(Get-Date -Format MM/dd/yyyy)) "  -foregroundcolor "green"
         (Get-WMIObject  -Class Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}  `
         | Select-Object @{n="Unit";e={($_.Name)}}, 
                    @{n="Label";e={($_.VolumeName)}}, 
                    @{n='Size (GB)';e={"{0:n2}" -f ($_.size/1gb)}}, 
                    @{n='Free (GB)';e={"{0:n2}" -f ($_.freespace/1gb)}}, 
                    @{n='% Free';e={"{0:n2}" -f ($_.freespace/$_.size*100)}}
       )  
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)