PowerShell for Windows Admins

Jul 14 2015   12:05PM GMT

Disk identification

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell
WMI

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)}}

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: