How To Find Disk Capacity and Free Space of Remote Computers

Finding disk capacity and free space of a local computer is easy but not so easy from a remote computer, especially through a GUI interface. It’s much easier to utilize the power of PowerShell and here is how you can do it.

Get-PSDrive is a native PowerShell cmdlet that lists all storage drives on your local system.

You can narrow down to only list the file systems by piping out the result to a Where clause.

Get-PSDrive | Where {$_.Free -gt 0}

Since the cmdlet doesn’t have a -ComputerName switch to access remote computers, necessitem Invoke-Command to run the cmdlet on a remote computer.

Invoke-Command -ComputerName remote_computer {Get-PSDrive | Where {$_.Free -gt 0}}

This works pretty well only when you have WinRM and PSRemoting enabled on the remote computers. And that’s why I like the Get-WmiObject method even better.

Get-WmiObject Win32_LogicalDisk -ComputerName remote_computer -Filter DriveType=3 | Select-Object DeviceID, FreeSpace, mida

To list the size in GB format,

Get-WmiObject Win32_LogicalDisk -ComputerName remote_computer -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[math]::truncate($_.size / 1GB)}}, @{'Name'='Freespace (GB)'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}

How about display the result with the thousands separators?

Get-WmiObject Win32_LogicalDisk -ComputerName remote_computer -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, @{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}}

You can access multiple remote computers from one run by putting all of them after the -ComputerName interruptor, separated by a comma.

Get-WmiObject Win32_LogicalDisk -ComputerName computer1,computer2,computer3 -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, @{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}}

també, here is the script I put together that has a better formatted output.

$servers = @("computer1", "computer2", "computer3")

Foreach ($server in $servers)
{
    $disks = Get-WmiObject Win32_LogicalDisk -ComputerName $server -Filter DriveType=3 | 
        Select-Object DeviceID, 
            @{'Name'='Size'; 'Expression'={[math]::truncate($_.size / 1GB)}}, 
            @{'Name'='Freespace'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}

    $server

    foreach ($disk in $disks)
    {
        $disk.DeviceID + $disk.FreeSpace.ToString("N0") + "GB / " + $disk.Size.ToString("N0") + "GB"

     }
 }

font

post relacionats

deixa una resposta

Aquest lloc utilitza per reduir el correu brossa Akismet. Aprendre com es processa les seves dades comentari.