Search code examples
powershellmathget-wmiobject

Powershell: Displaying two different text message depending on result


Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'" | 
Select-Object -Property DeviceID, DriveType, VolumeName, 
@{N='FreeSpaceGB';E={"{0:N2}" -f ($_.FreeSpace /1GB)}},
@{N="Capacity";E={"{0:N2}" -f ($_.Size/1GB)}},
@{N="PercentageFree";E={"{0:N0}" -f ($_.Freespace*100/$_.Size)}}

I have this and everything work perfectly but now i want to show a message saying, "warning low disk space" if the result of the 'PercentageFree' section is below 15% Brand new to this and had a look around but i'm still not sure what sort of lingo i should be using to research these things.

Thanks


Solution

  • Add another column to the end of the Select-Object

    @{N="Status";E={if ($_.Freespace*100/$_.Size -lt 15) { "Low Space" } else { "OK" } }}