windows disk space utilization with Powershell script

 


To get windows disk space utilization for single server.

The script calculates the disk utilization, free space, and percent free for each disk on each server



$disks = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3"

foreach ($disk in $disks) {

    $diskName = $disk.DeviceID

    $diskSize = $disk.Size / 1GB

    $diskFree = $disk.FreeSpace / 1GB

    $diskUsed = $diskSize - $diskFree

    $diskUtilization = $diskUsed / $diskSize * 100

    $diskPercentFree = $diskFree / $diskSize * 100

    $diskFreeFormatted = "{0:N2}GB" -f $diskFree

    $diskSizeFormatted = "{0:N2}GB" -f $diskSize

    Write-Host ("Disk {0}: Utilization = {1:N2}%, Free = {2} ({3:N2}%), Size = {4}" -f $diskName, $diskUtilization, $diskFreeFormatted, $diskPercentFree, $diskSizeFormatted)

}






For Multiple servers


$servers = "server1", "server2", "server3"
foreach ($server in $servers) {
    Write-Host "Server: $server"
    $disks = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $server
    foreach ($disk in $disks) {
        $diskName = $disk.DeviceID
        $diskSize = $disk.Size / 1GB
        $diskFree = $disk.FreeSpace / 1GB
        $diskUsed = $diskSize - $diskFree
        $diskUtilization = $diskUsed / $diskSize * 100
        $diskPercentFree = $diskFree / $diskSize * 100
        $diskFreeFormatted = "{0:N2}GB" -f $diskFree
        $diskSizeFormatted = "{0:N2}GB" -f $diskSize
        Write-Host ("Disk {0}: Utilization = {1:N2}%, Free = {2} ({3:N2}%), Size = {4}" -f $diskName, $diskUtilization, $diskFreeFormatted, $diskPercentFree, $diskSizeFormatted)
    }
}



Comments

Popular posts from this blog

Interview Questions on AOAG

Database Administrator Designations

Interview questions for Junior SQL server DBA.