WMI 可以提供硬盘的容量和剩余空间。PowerShell 会用返回这样的友好信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 PS C:\> PS C:\> Get-HardDriveSize DriveLetter Free(GB) Size(GB) Percent ----------- -------- -------- ------- C: 823 ,7 942 ,3 87 ,4 PS C:\> PS C:\> Get-HardDriveSize -ComputerName server2 -Credential server2\TobiasDriveLetter Free(GB) Size(GB) Percent ----------- -------- -------- ------- C: 87 ,3 436 ,9 20 D: 5 ,3 25 21 ,3
以下是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 function Get-HardDriveSize { param ( $ComputerName , $Credential ) $prop1 = @ { Name = 'DriveLetter' Expression = { $_ .DeviceID } } $prop2 = @ { Name = 'Free(GB)' Expression = { [Math ]::Round(($_ .FreeSpace / 1 GB),1 ) } } $prop3 = @ { Name = 'Size(GB)' Expression = { [Math ]::Round(($_ .Size / 1 GB),1 ) } } $prop4 = @ { Name = 'Percent' Expression = { [Math ]::Round(($_ .Freespace * 100 / $_ .Size),1 ) } } Get-CimInstance -ClassName Win32_LogicalDisk @PSBoundParameters -Filter "DriveType=3" | Select-Object -Property $prop1 , $prop2 , $prop3 , $prop4 }