'Stop Powershell Script if disk size is less than X
I wonder if you could help me out guys. I need to stop a powershell script if the disk size of partition E is less than 10GB, and to continue if it´s more than 10GB.
So far i managed to get my disk size listed with this.
Get-WmiObject -Class win32_logicaldisk | Format-Table DeviceId,@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
And i get this result:
| DeviceId | Freespace |
|---|---|
| A | 0 |
| C | 77.9 |
| D | 0 |
| E | 34.05 |
So, i want to stop the powershell script if E unit has less than 10GB. How can i do it?
Thanks in advance
Solution 1:[1]
If you want to put the Freespace of E in a variable you can do this :
$VarSpace = $(Get-WmiObject -Class win32_logicaldisk | Where-Object -Property Name -eq C:).FreeSpace/1GB
then you can do a simple if for check :
if ($VarSpace -le 10){ <Something for stopping you script like exit> }
Solution 2:[2]
You can use the Get-Volume cmdlet for that or Get-CimInstancerather than the old Get-WmiObject:
$freeOnE = (Get-CimInstance -ClassName win32_logicaldisk | Where-Object {$_.DeviceID -eq 'E:'}).FreeSpace / 1GB
or
$freeOnE = (Get-Volume -DriveLetter E).SizeRemaining / 1GB
Then exit your PowerShell session if this value is below 10Gb
if ($freeOnE -lt 10) { exit }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gmngeager35 |
| Solution 2 | Theo |
