Below is the code for checking the disk space:
$disks = Get-WmiObject -ComputerName WDSMS01 -Namespace root\cimv2 -Query "Select * from Win32_LogicalDisk where DriveType=3"
$disks.DeviceID
$dsql01 = @()
foreach($db in $disks)
{
$dsql = New-Object PSObject
$dsql | Add-Member -MemberType NoteProperty -Name "Drive Name" -value $db.DeviceID
$dsql | Add-Member -MemberType NoteProperty -Name "Total Space(GB)" -value ($db.Size/1GB)
$dsql | Add-Member -MemberType NoteProperty -Name "Free Space(GB)" -value ($db.FreeSpace/1GB)
$dsql01 += $dsql
}
$dsql01
Is it possible to limit the decimals by 2 digits? Also, add another column that computes free space in % format.
Thanks.
This depends a bit on what exactly you need. You can use
[Math]::Roundto round off everything after two digits:but that can leave you with numbers that have less than two digits if they are followed by zeroes, e.g.
14.6which doesn’t look as nice in a table.The other option is to format the numbers:
which will always use the same number of decimal digits, but results in a string, so performing further calculations can be hindered by that.
As for your second question, that’s actually trivial, just add another
Add-Membercall:Also you can (in PowerShell v2) put all those in a single
New-Objectcall:And while we’re at it,
foreachloops are so not PowerShell-y. Let’s rewrite that with a pipeline: