Monthly Archive

Categories

Disk identification

A recent question on the forums regarded using the Win32_LogicalDisk class to retrieve disk size and free space data.  A filter based on the disk letter was being used. The main error with the code was that the filter was being expressed as

 

"DeviceId='E'"

rather than

"DeviceId='E:"

 

The colon is necessary as its part of the DeviceId data – if you are in doubt about the form of the data required by the filter then examine the full output of the class to see an example.

 

There were a couple of other basic issues.

 

Firstly always output objects.

 

Secondly use the size constants MB, GB etc rather than calculating refresh each time.

 

The final modified code looks like this

$computername = $env:COMPUTERNAME
$partition = 'C:'
$description = 'backup_server'

Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceId='$partition'"     -ComputerName $computername |
select PSComputerName,
@{Name='Partition'; Expression={$_.DeviceId}},
@{Name='Description'; Expression={$description}},
@{Name='Size(GB)';Expression={[math]::Round(($_.Size / 1GB), 2)}},
@{Name='FreeSpace(GB)';Expression={[math]::Round(($_.FreeSpace / 1GB), 2)}}

Comments are closed.