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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay