Check Drive Space – Single Server

# Script to check disk space single server # Read server name or IP
$server = Read-Host "Enter Server Name:"
Get-WmiObject -ComputerName $server win32_logicaldisk | Where-Object {$_.DriveType -eq 3} | Select-Object DeviceId,@{N="Size(GB)";E={[Math]::Round($_.Size/(1GB), 2)}},@{N="Free Space(GB)";E={[Math]::Round($_.FreeSpace/(1GB), 2)}},@{N="Free %";E={[Math]::Round(($_.FreeSpace/$_.Size)*100, 2)}}

Here is the output:

DeviceId Size(GB) Free Space(GB) Free %
-------- -------- -------------- ------
C:         111.25          47.19  42.42
D:         931.51         579.75  62.24
E:         238.47          79.66   33.4

The above script checks the drive space of a single server.

Here is the breakout of the script.

$server = Read-Host "Enter Server Name:"

This line prompts for a server hostname or IP Address.

Get-WmiObject -ComputerName $server win32_logocaldisk

The above line of code query for all the Drives on the server $server (variable)

Where-Object {$_DriveType -eq 3}

The above code filters the Drive Type = 3 (Fixed local disk)

2 = “Removable disk”

3 = “Fixed local disk”

4 = “Network disk”

5 = “Compact disk”

Select-Object — > Selects the required fields from the query

@{N="Size(GB)";E={[Math]::Round($_.Size/(1GB), 2)}}

N = Name to Display (you can give anything here)

E = Expression

[Math]::Round – Math function and rounds the expression to 2 digits after . (decimal) .

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit
Share on skype
Share on telegram
Share on whatsapp
Share on email
Share on print

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment