Check if Enough Free Space in Remote Computer with PowerShell

Guillermo Musumeci
1 min readJan 22, 2021

I’m currently writing lots of building blocks of code to deploy Infrastructure as Code (IaC).

The following code can check if there enough space (in this case, 10 GB of space) on the C drive of the computer kopidc01:

$computer = "kopidc01"
$RequiredSpaceGB = 10
$FreeSpaceGB = [int]((Get-CimInstance -ComputerName $computer CIM_LogicalDisk -Filter "DeviceId='C:'").FreeSpace/1024/1024/1024)
if($FreeSpaceGB -lt $RequiredSpaceGB){
write-host "Not enough space: $RequiredSpaceGB GB required!"
}

To customize our script, the relevant info is contained on the RequiredSpaceGB variable, used to set the required space in GB, and DeviceID which we can update for to use a different hard drive.

If you want to compare with MB instead of GB, remove one of the /1024 like this:

$FreeSpaceMB = [int]((Get-CimInstance -ComputerName $computer CIM_LogicalDisk -Filter "DeviceId='C:'").FreeSpace/1024/1024)

And that’s all folks. If you liked this story, please show your support by 👏 this story. Thank you for reading!

--

--

Guillermo Musumeci
Guillermo Musumeci

Written by Guillermo Musumeci

Certified AWS, Azure & GCP Architect | HashiCorp Ambassador | Terraform SME | KopiCloud Founder | ex-AWS | Entrepreneur | Book Author | Husband & Dad of ✌