$used
doesn't exist as a variable in that scope, so used2
is being set to $null
.
You need to create a variable $used
before calling New-Object
. Also, you're doing far too much formatting in calculating the used percentage - save the formatting until the very end. Below, I've made both changes
$used = "{0:P1}" -f (($disk.size - $disk.FreeSpace) / $disk.size);
$output = New-Object PSObject -Property @{
deviceID = $disk.caption
FileSystem = $disk.fileSystem
FreeSpace = $disk.freeSpace/1GB
Size = $disk.size/1GB
VolumeName = $disk.volumeName
used = $used
used2 = $used
}