Monday, April 7, 2014

How to check Disk Space Usage and Free Space Remotely using PowerShell

Scenario :

Two hyper-V Host (clustered) with multiple VMs in it.  I need to check on the disk allocation, disk space usage & disk free space.  With the script below I’m going to share how I did it using PowerShell.  I just run it in one of the Hyper-V Hosts PowerShell.

[Special credit to StackOverflow and BinaryNature.  I’ve made changes to ease the entry of variables]

The credential used in the script must be part of the local administrators group

# ----- Beginning of Script -----

#Define ServerName (Physical/VM) Here

# Change the Variables in PINK and remark out with # if the server or VM is not needed

# $APP2="APPVM2"
$DomainUser = "domain\user1"

# Diskfree Module

function Get-DiskFree
        [string[]]$ComputerName = $env:COMPUTERNAME,
        [System.Management.Automation.Credential()]$Credential =
        function Format-HumanReadable
            param ($size)
            switch ($size)
                {$_ -ge 1PB}{"{0:#.#'P'}" -f ($size / 1PB); break}
                {$_ -ge 1TB}{"{0:#.#'T'}" -f ($size / 1TB); break}
                {$_ -ge 1GB}{"{0:#.#'G'}" -f ($size / 1GB); break}
                {$_ -ge 1MB}{"{0:#.#'M'}" -f ($size / 1MB); break}
                {$_ -ge 1KB}{"{0:#'K'}" -f ($size / 1KB); break}
                default {"{0}" -f ($size) + "B"}
        $wmiq = 'SELECT * FROM Win32_LogicalDisk WHERE Size != Null AND DriveType >= 2'
        foreach ($computer in $ComputerName)
                if ($computer -eq $env:COMPUTERNAME)
                    $disks = Get-WmiObject -Query $wmiq `
                             -ComputerName $computer -ErrorAction Stop
                    $disks = Get-WmiObject -Query $wmiq `
                             -ComputerName $computer -Credential $Credential `
                             -ErrorAction Stop
                if ($Format)
                    # Create array for $disk objects and then populate
                    $diskarray = @()
                    $disks | ForEach-Object { $diskarray += $_ }
                    $diskarray | Select-Object @{n='Name';e={$_.SystemName}},
                        @{n='Size';e={Format-HumanReadable $_.Size}},
                        @{n='Used';e={Format-HumanReadable `
                        @{n='Avail';e={Format-HumanReadable $_.FreeSpace}},
                        /($_.Size) * 100))}},
                    foreach ($disk in $disks)
                        $diskprops = @{'Volume'=$disk.DeviceID;
                                   'Used'=($disk.Size - $disk.FreeSpace);
                        # Create custom PS object and apply type
                        $diskobj = New-Object -TypeName PSObject `
                                   -Property $diskprops
                        Write-Output $diskobj
                # Check for common DCOM errors and display "friendly" output
                switch ($_)
                    { $_.Exception.ErrorCode -eq 0x800706ba } `
                        { $err = 'Unavailable (Host Offline or Firewall)';
                            break; }
                    { $_.CategoryInfo.Reason -eq 'UnauthorizedAccessException' } `
                        { $err = 'Access denied (Check User Permissions)';
                            break; }
                    default { $err = $_.Exception.Message }
                Write-Warning "$computer - $err"
    END {}

$cred = Get-Credential -Credential $DomainUser
$PHY1, PHY2, $AD, $APP1, $APP2, $DB1, $DP1, $DPM1 | Get-DiskFree -Credential $cred -Format | Format-Table -GroupBy Name -AutoSize

# ----- End of Script -----


My method was copy the script above (from Beginning to End of the Script)  into a notepad and make changes only to the PINK.  Please include the “ “ quotes as in the scripts. 

(You can also save it as powershell script with ext of PS1 and run. Since this is a one time, I don’t intend to save it as PS1 file)

After making the necessary changes, I copied the text and launch the PowerShell (need to run as Administrator) from one of the Hosts


…then I paste into the PowerShell screen and press [Enter]


It will display the credential, key in the password :


It will display something like below with the (servername or VM name as I’ve hide) :


Happy trying  and hope the script helps as it helps me.

keywords : checking free space with powershell, powershell scripts, power shell script, checking space usage remotely, windows 2012 check disk space in VM

No comments:

Post a Comment