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
CodePudding user response:
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> }
CodePudding user response:
You can use the Get-Volume cmdlet for that or Get-CimInstance
rather 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 }