Get free disk space in percent from command line
This command can be run from a CMD prompt to get the percentage of free disk space on the C:\ drive:
1 |
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -executionpolicy bypass -Command "& {[int][math]::Truncate((Get-WmiObject -Namespace root\CIMV2 -Class Win32_LogicalDisk -Filter """name = 'c:'""").FreeSpace[0] * 100 / (Get-WmiObject -Namespace root\CIMV2 -Class Win32_LogicalDisk -Filter """name = 'c:'""").size[0])}" |
You can even run this in a task sequence command line step and save the value in a task sequence variable.
1 |
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -executionpolicy bypass -Command "& {(New-Object -COMObject Microsoft.SMS.TSEnvironment).Value('FreeSpacePercentage') = [int][math]::Truncate((Get-WmiObject -Namespace root\CIMV2 -Class Win32_LogicalDisk -Filter """name = 'c:'""").FreeSpace[0] * 100 / (Get-WmiObject -Namespace root\CIMV2 -Class Win32_LogicalDisk -Filter """name = 'c:'""").size[0])}" |
This is useful if you want to create a condition on a subsequent task sequence step used for preparing a computer for BitLocker.
The BDEHDCFG.exe tool needs at least 10% free disk space, in order to create a boot partition as shown below:
I used this to enable Bitlocker on computer clients in production which for one reason or another hadn’t been encrypted with BitLocker.