How to get the list of hard disk on the computer using
PowerShell?
To get the list of drives we can use the wmiobject to get
the details
PowerShell Code:
Get-WmiObject -Class win32_logicaldisk
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace :
43298725888
Size :
255073447936
VolumeName : OSDisk
We can select the information that we want to display by using the Select-object
PowerShell Code:
Get-WmiObject -Class win32_logicaldisk |
select DeviceID,FreeSpace, Size
DeviceID FreeSpace Size
--------
--------- ----
C:
43306131456 255073447936
H: 1010439315456
2582761172992
X: 2223348092928
3221088104448
Y: 1535164190720
3221088104448
Z:
17846333440 85896196096
We can modify the result to display it in MB or GB along
with the decimal digit
PowerShell Code:
$Drive = Get-WmiObject -Class win32_logicaldisk |
select DeviceID, FreeSpace, Size
echo "Drive
Letter `t Free_Space `t Full_Space"
echo "==========================================="
foreach($Drive
in $Drive)
{
$DriveName=$Drive.DeviceID
$DriveFreeSpace="{0:N2}" -f ($Drive.FreeSpace / 1GB) + " GB"
$DriveSize="{0:N2}" -f ($Drive.Size / 1GB) + " GB"
echo "$DriveName `t`t`t`t $DriveFreeSpace`t`t$DriveSize"
|Format-Table
}
Drive Letter Free_Space Full_Space
===========================================
C: 40.33 GB 237.56 GB
H: 940.99 GB 2,405.38 GB
X: 2,070.65
GB 2,999.87 GB
Y: 1,429.73
GB 2,999.87 GB
Z: 16.62 GB 80.00 GB
You could use the invoke command to get the same details from the remote computer
Invoke-Command -ComputerName
Client7-1 -ScriptBlock
{
$Drive = Get-WmiObject -Class win32_logicaldisk |
select DeviceID, FreeSpace, Size
echo "Drive
Letter `t Free_Space `t Full_Space"
echo "==========================================="
foreach($Drive
in $Drive)
{
$DriveName=$Drive.DeviceID
$DriveFreeSpace="{0:N2}" -f ($Drive.FreeSpace / 1GB) + " GB"
$DriveSize="{0:N2}" -f ($Drive.Size / 1GB) + " GB"
echo "$DriveName `t`t`t`t $DriveFreeSpace`t`t$DriveSize"
|Format-Table
}
}
==============================================================
Clean and Alternative code to view the Hard disk and the free space
Clean and Alternative code to view the Hard disk and the free space
Get-WmiObject -class Win32_LogicalDisk -computername
localhost |
Sort-Object -property
DeviceID |
Format-Table -property
@{label ='Drive';expression={$_.DeviceID}}, @{label='FreeSpace(MB)';expression={$_.FreeSpace
/ 1MB -as [int]}}, @{label='Size(GB)';expression={$_.Size / 1GB -as [int]}}, @{label='%Free';expression={$_.FreeSpace
/ $_.Size * 100 -as [int]}}
This Code
“@{label='FreeSpace(MB)';expression={$_.FreeSpace / 1MB -as [int]}}”
Will give the column name
as for the freespace value and it converts to MB
Drive FreeSpace(MB) Size(GB) %Free
----- ------------- -------- -----
C:
39898 238 16
H:
953496 2405 39
X:
2116792 3000 69
Y:
1459925 3000 48
Z:
16941 80 21
No comments:
Post a Comment