Powershell % disk space
redhcp

redhcp @redhcp

About: ☕Coffee - 💻Tech - 🏍Travel Lover - 🕊@Th3Ces4r

Location:
127.0.0.1
Joined:
Nov 11, 2020

Powershell % disk space

Publish Date: Jan 10 '22
1 0

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

Comments 0 total

    Add comment