-
-
Save junecastillote/60ef3aa9a72e687ae073025c17822e4a to your computer and use it in GitHub Desktop.
Get Disk Space PowerShell Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[String[]] | |
$ComputerName = $env:COMPUTERNAME | |
) | |
# Drive type lookup table | |
$driveType = @{ | |
0 = 'Unknown' | |
1 = 'No Root Directory' | |
2 = 'Removable Disk' | |
3 = 'Local Disk' | |
4 = 'Network Drive' | |
5 = 'Compact Disc' | |
6 = 'RAM Disk' | |
} | |
$result = [System.Collections.ArrayList]@() | |
foreach ($computer in $ComputerName) { | |
try { | |
if ($localLogicalDisks = Get-CimInstance -Class Win32_LogicalDisk -ComputerName $computer -Property * -ErrorAction Stop) { | |
$result.AddRange($localLogicalDisks) | |
if (($localLogicalDisks.DriveType) -notcontains 4) { | |
if ($mappedLogicalDisks = Get-CimInstance -Class Win32_MappedLogicalDisk -ComputerName $computer -Property * -ErrorAction Stop) { | |
$result.AddRange($mappedLogicalDisks) | |
} | |
} | |
} | |
} | |
catch { | |
"[ERROR][$computer] : $($_.exception.message)" | Out-Default | |
} | |
} | |
if ($result) { | |
$result | ForEach-Object { | |
[PSCustomObject]@{ | |
ComputerName = $_.SystemName | |
DeviceID = $_.DeviceID | |
VolumeName = $_.VolumeName | |
DriveType = $( | |
if ($_.DriveType) { | |
$driveType[$([int]$_.DriveType)] | |
} | |
else { | |
$driveType[4] | |
} | |
) | |
SizeGB = $([System.Math]::Round(($_.Size / 1GB), 2)) | |
UsedSpaceGB = $([System.Math]::Round((($_.Size - $_.FreeSpace) / 1GB), 2)) | |
FreeSpaceGB = $([System.Math]::Round(($_.FreeSpace / 1GB), 2)) | |
FreeSpacePercent = $([System.Math]::Round((($_.FreeSpace / $_.Size) * 100), 2)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment