When a cmdlet returns raw data, you may want to convert the data into a better format. For example, WMI can report the free space of a drive but reports bytes.
You can then use Select-Object and provide hash tables to take the raw data and convert it to anything you want. This sample illustrates how to convert Freespace into GB and also calculate the percentage of free space:- $Freespace =
- @{
- Expression = {[int]($_.Freespace/1GB)}
- Name = 'Free Space (GB)'
- }
-
- $PercentFree =
- @{
- Expression = {[int]($_.Freespace*100/$_.Size)}
- Name = 'Free (%)'
- }
-
- Get-WmiObject -Class Win32_LogicalDisk | Select-Object -Property DeviceID, VolumeName, $Freespace, $PercentFree
复制代码 Here is the result without hash tables:
PS C:\> Get-WmiObject -Class Win32_LogicalDisk | Select-Object -Property DeviceID, VolumeName, Freespace
DeviceID VolumeName Freespace
-------- ---------- ---------
C: BatHome 114592428032
|
And this is the result using hash tables:
PS C:\> Get-WmiObject -Class Win32_LogicalDisk | Select-Object -Property DeviceID, VolumeName, $Freespace, $PercentFree
DeviceID VolumeName Free Space (GB) Free (%)
-------- ---------- --------------- --------
C: BatHome 107 45
|
http://powershell.com/cs/blogs/tips/archive/2014/03/14/drive-data-in-gb-and-percent.aspx |