找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 13535|回复: 0

[转载代码] [PowerShell每日技巧]硬盘空间GB和使用率(20140314)

[复制链接]
发表于 2014-3-14 22:21:41 | 显示全部楼层 |阅读模式
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:
  1. $Freespace =
  2. @{
  3.   Expression = {[int]($_.Freespace/1GB)}
  4.   Name = 'Free Space (GB)'
  5. }

  6. $PercentFree =
  7. @{
  8.   Expression = {[int]($_.Freespace*100/$_.Size)}
  9.   Name = 'Free (%)'
  10. }

  11. 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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 23:45 , Processed in 0.016269 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表